How to get a custom attribute from object instance in C#

前端 未结 6 1201
眼角桃花
眼角桃花 2020-12-05 07:40

Let\'s say I have a class called Test with one property called Title with a custom attribute:

public class Test
{
    [DatabaseField(\"title\")]
    public s         


        
6条回答
  •  时光说笑
    2020-12-05 07:59

    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Test t = new Test();
    
                Console.WriteLine(t.FieldName("Title").FieldName());
                Console.WriteLine(t.FieldName("Title").FieldIsPrimaryKey());
            }
    
    
        }
    
        public class Test
        {
            [DatabaseField("titlezzz", true)]
            public string Title
            {
                get;
                set;
            }
        }
    
    
        public class BaseDatabaseFieldAttribute : Attribute
        {
            private readonly string _name;
    
            public string Name { get { return _name; } }
    
            public BaseDatabaseFieldAttribute(string name)
            {
                _name = name;
            }
        }
        public class DatabaseFieldAttribute : BaseDatabaseFieldAttribute
        {
            private readonly bool _isPrimaryKey;
    
            public bool IsPrimaryKey { get { return _isPrimaryKey; } }
    
            public DatabaseFieldAttribute(string name, bool isPrimaryKey): base(name)
            {
                _isPrimaryKey = isPrimaryKey;
            }
        }
    
        public static class Helper
        {
    
            public static PropertyInfo FieldName(this object obj, string propertyName)
            {
                return obj.GetType().GetProperty(propertyName);
            }
    
            public static string FieldName(this PropertyInfo property) where T: BaseDatabaseFieldAttribute
            {
                object[] os = property.GetCustomAttributes(typeof(T), false);
    
                if (os != null && os.Length >= 1)
                    return (os[0] as T).Name;
                else
                    return "N/A";
            }
    
            public static bool? FieldIsPrimaryKey(this PropertyInfo property) where T : DatabaseFieldAttribute
            {
                object[] os = property.GetCustomAttributes(typeof(T), false);
    
                if (os != null && os.Length >= 1)
                    return (os[0] as T).IsPrimaryKey;
                else
                    return null;
            }
        }
    
    
    }
    

提交回复
热议问题