在 C# 类的实例方法中使用 GetValue 方法

库斯克门

我有一个带方法的简单类,ToHtmlString()我想枚举这个对象(引用)的所有私有和实例属性。为什么我不能取值。我看到我需要将对象传递给GetValue方法,以便运行时知道哪个引用反射将获得哪个值,但似乎我不能那样做?GetValue枚举此实例的所有属性时如何使用

    public string ToHtmlString()
    {
        var source = this;
        var html = new StringBuilder();

        foreach (var property in this.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Instance))
        {
            html.AppendLine(
                new GenericItemView(
                    (property.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description,
                    property.GetValue(source).ToString());
        }

        return html.ToString();
    }
米格

看起来你缺少一个结束的“)”和一个 .ToString()

html.AppendLine(new GenericItemView((property.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)?.Description, property.GetValue(source).ToString()).ToString());

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Datasnapshot 的 getValue() 方法为空

子类调用基类构造函数,然后在子类Arduino C ++中使用基类的实例方法

如何在C#中使用子类的实例访问父类的方法

如何在 c# 中使用方法(如果可能)实例化一个类?

Firebase中的getValue()方法是什么?

替代(过时的)GetValue 和 SetValue 方法

无法覆盖 Vaadin ComboBox 的 getValue() 方法

最好使用哪种JSON方法:Get()或GetValue()?

错误-“方法Range.getValue被脚本大量使用”

C#使用类的不同实例调用非静态方法

ag-Grid:框架组件缺少方法getValue()

是否可以为复杂类型覆盖GetValue()方法?

Keycloak示例-未为UserCredentialModel类型定义getValue()方法

AngularJS无法调用未定义的方法“ getValue”

在objective c 类中使用delegate 调用swift 方法

在C#中使用参数隐藏/覆盖基类方法

如何在C#中使用Ajax调用类方法

无法在C#类的F#中使用某些方法

是否可以在C#类中使用Web方法?

如何在类方法中使用实例变量?

将实例方法转换为类方法 C#

C# 反射 GetField().GetValue()

调用没有类实例的C ++类方法?

C#创建类实例以在类中执行方法时

如何在select方法中使用实例名称为类实例成员分配值

C ++ / CLI虚拟方法,不能在派生类中使用虚拟方法

使用类方法的C ++ ISR?

C++) 如何从一个类中使用其他类的方法?

C ++ MFC:尝试使用类实例调用方法时出现调试断言错误