属性上的自定义属性 - 获取属性属性的类型和值

问题描述 投票:0回答:5

我有以下自定义属性,可以应用于属性:

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class IdentifierAttribute : Attribute
{
}

例如:

public class MyClass
{
    [Identifier()]
    public string Name { get; set; }

    public int SomeNumber { get; set; }
    public string SomeOtherProperty { get; set; }
}

还有其他类,可以将 Identifier 属性添加到不同类型的属性中:

public class MyOtherClass
{
    public string Name { get; set; }

    [Identifier()]
    public int SomeNumber { get; set; }

    public string SomeOtherProperty { get; set; }
}

然后我需要能够在我的消费类中获取这些信息。 例如:

public class TestClass<T>
{
    public void GetIDForPassedInObject(T obj)
    {
        var type = obj.GetType();
        //type.GetCustomAttributes(true)???
    }
}

解决这个问题的最佳方法是什么? 我需要获取 [Identifier()] 字段的类型(int、string 等)和实际值,显然是基于类型的。

c# .net custom-attributes
5个回答
53
投票

类似于下面的内容,这将仅使用它遇到的具有该属性的第一个属性,当然您可以将其放置在多个属性上..

    public object GetIDForPassedInObject(T obj)
    {
        var prop = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance)
                   .FirstOrDefault(p => p.GetCustomAttributes(typeof(IdentifierAttribute), false).Count() ==1);
        object ret = prop !=null ?  prop.GetValue(obj, null) : null;

        return ret;
    }  

3
投票
public class TestClass<T>
{
    public void GetIDForPassedInObject(T obj)
    {
        PropertyInfo[] properties =
            obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);            

        PropertyInfo IdProperty = (from PropertyInfo property in properties
                           where property.GetCustomAttributes(typeof(Identifier), true).Length > 0
                           select property).First();

         if(null == IdProperty)
             throw new ArgumentException("obj does not have Identifier.");

         Object propValue = IdProperty.GetValue(entity, null)
    }
}

3
投票

有点晚了,但这是我为枚举(也可以是任何对象)所做的事情,并使用扩展获取描述属性值(这可以是任何属性的通用属性):

public enum TransactionTypeEnum
{
    [Description("Text here!")]
    DROP = 1,

    [Description("More text here!")]
    PICKUP = 2,

    ...
}

获取值:

var code = TransactionTypeEnum.DROP.ToCode();

支持我所有枚举的扩展:

public static string ToCode(this TransactionTypeEnum val)
{
    return GetCode(val);
}

public static string ToCode(this DockStatusEnum val)
{
    return GetCode(val);
}

public static string ToCode(this TrailerStatusEnum val)
{
    return GetCode(val);
}

public static string ToCode(this DockTrailerStatusEnum val)
{
    return GetCode(val);
}

public static string ToCode(this EncodingType val)
{
    return GetCode(val);
}

private static string GetCode(object val)
{
    var attributes = (DescriptionAttribute[])val.GetType().GetField(val.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);

    return attributes.Length > 0 ? attributes[0].Description : string.Empty;
}

0
投票

示例

我已经使用接受参数的属性扩展了您的示例,并且可以使用一个属性

[AttributeUsage(AttributeTargets.Property,AllowMultiple = true)]
public class PersonAttribute : Attribute
{
    public readonly string FieldName;
    public readonly string FieldType;
    public PersonAttribute(string name,string type)
    {
        FieldName = name;
        FieldType = type;
    }    
}

该属性应用于

Person
类,如下所示:

public class Person
{
    public string FirstName {get;set;} = "FirstName";
    public string LastName {get;set;} = "LastName";
    
    [Person("addressline1","db")]
    [Person("new_address1","system")]
    public string AddressLine1 {get;set;} = "Banglore";
    
    [Person("addressline2","system")]
    public string AddressLine2 {get;set;} = "Karnataka";
    
    [Person("addressline3","dto")]
    public string AddressLine3 {get;set;} = "INDIA";    
}


解决方案

我创建了以下通用扩展函数,它将能够从类中读取所有属性和属性值。

public static class AttributesExt
{
    public static IEnumerable<PropertyInfo> AllAttributes<T>(this object obj,string name)
    {
        var allProperties = obj.GetType().GetProperties()
        .Where(_ => _.GetCustomAttributes(typeof(T), true).Length >= 1  && _.Name == name);
        return allProperties;
    }
    
    public static IEnumerable<PropertyInfo> AllAttributes<T>(this object obj)
    {
        var allProperties = obj.GetType().GetProperties()
        .Where(_ => _.GetCustomAttributes(typeof(T), true).Length >= 1);
        return allProperties;
    }

    public static T ReadAttribute<T>(this PropertyInfo propertyInfo)
    {
        var returnType = propertyInfo.GetCustomAttributes(typeof(T), true)
        .Cast<T>().FirstOrDefault();
        return returnType;
    }
}

现在在 main 方法中如果我们写

void Main()
{
    Person p = new Person();
    
    var all = p.AllAttributes<PersonAttribute>().Dump(); //Get All custom attributes
    p.AllAttributes<PersonAttribute>("AddressLine3").Dump();
    all.First(_=> _.Name == "AddressLine2").ReadAttribute<PersonAttribute>().Dump();
    all.First(_=> _.Name == "AddressLine2").ReadAttribute<PersonAttribute>().FieldName.Dump();
}

我们可以按照下面的屏幕截图读取这些值。


-1
投票

这是一个更真实的例子。我们使用扩展方法并检查属性是否包含 FieldMetaDataAttribute (我的源代码库中的自定义属性) 具有有效的主要版本和次要版本。人们普遍感兴趣的是我们使用父类类型和 GetProperties 并检索 ProperyInfo,然后在这种特殊情况下使用 GetCustomAttribute 检索属性 FieldMetaDataAttribute 的部分。使用此代码来获取如何以更通用的方式检索自定义属性的灵感。当然可以对其进行改进,以创建一个通用方法来检索类实例的任何属性的给定属性。

        /// <summary>
    /// Executes the action if not the field is deprecated 
    /// </summary>
    /// <typeparam name="TProperty"></typeparam>
    /// <typeparam name="TForm"></typeparam>
    /// <param name="form"></param>
    /// <param name="memberExpression"></param>
    /// <param name="actionToPerform"></param>
    /// <returns>True if the action was performed</returns>
    public static bool ExecuteActionIfNotDeprecated<TForm, TProperty>(this TForm form, Expression<Func<TForm, TProperty>> memberExpression, Action actionToPerform)
    {
        var memberExpressionConverted = memberExpression.Body as MemberExpression;
        if (memberExpressionConverted == null)
            return false; 

        string memberName = memberExpressionConverted.Member.Name;


        PropertyInfo matchingProperty = typeof(TForm).GetProperties(BindingFlags.Public | BindingFlags.Instance)
            .FirstOrDefault(p => p.Name == memberName);
        if (matchingProperty == null)
            return false; //should not occur

        var fieldMeta = matchingProperty.GetCustomAttribute(typeof(FieldMetadataAttribute), true) as FieldMetadataAttribute;
        if (fieldMeta == null)
        {
            actionToPerform();
            return true;
        }

        var formConverted = form as FormDataContract;
        if (formConverted == null)
            return false;

        if (fieldMeta.DeprecatedFromMajorVersion > 0 && formConverted.MajorVersion > fieldMeta.DeprecatedFromMajorVersion)
        {
            //major version of formConverted is deprecated for this field - do not execute action
            return false;
        }

        if (fieldMeta.DeprecatedFromMinorVersion > 0 && fieldMeta.DeprecatedFromMajorVersion > 0
                                                     && formConverted.MinorVersion >= fieldMeta.DeprecatedFromMinorVersion
                                                     && formConverted.MajorVersion >= fieldMeta.DeprecatedFromMajorVersion)
            return false; //the field is expired - do not invoke action 
        actionToPerform();
        return true;
    }
© www.soinside.com 2019 - 2024. All rights reserved.