C# 通过属性反射设置属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/390594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:41:26  来源:igfitidea点击:

C# setting property values through reflection with attributes

c#reflection

提问by johnc

I am trying to build an object through an attribute on a classes property that specifies a column in a supplied data row that is the value of the property, as below:

我正在尝试通过 classes 属性上的属性构建一个对象,该属性在提供的数据行中指定一列作为该属性的值,如下所示:

    [StoredDataValue("guid")]
    public string Guid { get; protected set; }

    [StoredDataValue("PrograGuid")]
    public string ProgramGuid { get; protected set; }

In a Build() method on a base object, I am getting the attribute values set on these properties as

在基础对象的 Build() 方法中,我将这些属性上设置的属性值设置为

        MemberInfo info = GetType();
        object[] properties = info.GetCustomAttributes(true);

However, at this point I am realising the limitation in my knowledge.

然而,在这一点上,我意识到我的知识有限。

For a start, I don't appear to be getting back the correct attributes.

首先,我似乎没有找回正确的属性。

And how do I set these properties through reflection, now that I have the attributes? Am I doing / thinking something fundamentally incorrect?

以及如何通过反射设置这些属性,现在我有了这些属性?我在做/思考一些根本不正确的事情吗?

采纳答案by Tamas Czinege

There are a couple of separate issues here

这里有几个单独的问题

  • typeof(MyClass).GetCustomAttributes(bool)(or GetType().GetCustomAttributes(bool)) returns the attributes on the class itself, not the attributes on members. You will have to invoke typeof(MyClass).GetProperties()to get a list of properties in the class, and then check each of them.

  • Once you got the property, I think you should use Attribute.GetCustomAttribute()instead of MemberInfo.GetGustomAttributes()since you exactly know what attribute you are looking for.

  • typeof(MyClass).GetCustomAttributes(bool)(或GetType().GetCustomAttributes(bool)) 返回类本身的属性,而不是成员的属性。您必须调用typeof(MyClass).GetProperties()以获取类中的属性列表,然后检查每个属性。

  • 一旦你获得了属性,我认为你应该使用Attribute.GetCustomAttribute()而不是MemberInfo.GetGustomAttributes()因为你确切地知道你正在寻找什么属性。

Here's a little code snippet to help you start:

这是一个小代码片段,可帮助您开始:

PropertyInfo[] properties = typeof(MyClass).GetProperties();
foreach(PropertyInfo property in properties)
{
    StoredDataValueAttribute attribute =
        Attribute.GetCustomAttribute(property, typeof(StoredDataValueAttribute)) as StoredDataValueAttribute;

    if (attribute != null) // This property has a StoredDataValueAttribute
    {
         property.SetValue(instanceOfMyClass, attribute.DataValue, null); // null means no indexes
    }
}

EDIT: Don't forget that Type.GetProperties()only returns public properties by default. You will have to use Type.GetProperties(BindingFlags)to get other sorts of properties as well.

编辑:不要忘记Type.GetProperties()默认情况下只返回公共属性。您还必须使用Type.GetProperties(BindingFlags)来获取其他类型的属性。