泛型反射获取特性值

泛型反射获取特性值,本文主要是讲述如何使用泛型以及反射来获取属性的特性值的。具体案例如下:

 

1、新建控制台项目 GenericReflectionGetsPropertyValues

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericReflectionGetsPropertyValues
{
class Program
{
static void Main(string[] args)
{


}
}

 

2、添加泛型反射获取类型的属性的特性值帮助类 CustomAttributeHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericReflectionGetsPropertyValues
{
    /// <summary>
    /// 泛型反射获取类型的属性的特性值帮助类
    /// </summary>
    public static class CustomAttributeHelper
    {
        /// <summary>
        /// 缓存字典
        /// </summary>
        public static readonly Dictionary<string, string> cacheKeyValuePairs = new Dictionary<string, string>();

        /// <summary>
        /// 泛型反射获取属性的特性值
        /// </summary>
        /// <typeparam name="T">泛型(特性)</typeparam>
        /// <param name="type">获取属性的特性的类</param>
        /// <param name="func">委托方法</param>
        /// <param name="name">属性名称</param>
        /// <returns>泛型反射获取属性的特性值</returns>
        public static string GetCustomAttributeValue<T>(this Type type, Func<T, string> func, string name) where T : Attribute
        {
            if (func == null || string.IsNullOrEmpty(name))
            {
                return null;
            }
            return GetCustomAttribute(type, func, name);
        }

        /// <summary>
        /// 获取属性的特性值
        /// </summary>
        /// <typeparam name="T">泛型(特性)</typeparam>
        /// <param name="type">获取属性的特性的类</param>
        /// <param name="func">委托方法</param>
        /// <param name="name">属性名称</param>
        /// <returns>反射获取属性的特性值</returns>
        public static string GetCustomAttribute<T>(Type type, Func<T, string> func, string name) where T : Attribute
        {
            string key = GetKey(type, name);
            string returnValue = null;
            if (!cacheKeyValuePairs.ContainsKey(key))
            {
                CacheCustomAttribute(type, func, name);
                if (cacheKeyValuePairs.ContainsKey(key))
                {
                    returnValue = cacheKeyValuePairs[key];
                }
            }
            else
            {
                returnValue = cacheKeyValuePairs[key];
            }

            return returnValue;

        }

        /// <summary>
        /// 缓存属性的特性值
        /// </summary>
        /// <typeparam name="T">泛型(特性)</typeparam>
        /// <param name="type">获取属性的特性的类</param>
        /// <param name="func">委托方法</param>
        /// <param name="name">属性名称</param>
        public static void CacheCustomAttribute<T>(Type type, Func<T, string> func, string name) where T : Attribute
        {
            string key = GetKey(type, name);
            string value = GetValue(type, func, name);
            if (!string.IsNullOrEmpty(value))
            {
                if (!cacheKeyValuePairs.ContainsKey(key))
                {
                    cacheKeyValuePairs[key] = value;
                }
            }
        }

        /// <summary>
        /// 泛型反射获取特性值
        /// </summary>
        /// <typeparam name="T">特性类型</typeparam>
        /// <param name="type">类型</param>
        /// <param name="func">获取特性值的委托</param>
        /// <param name="name">属性名</param>
        public static string GetValue<T>(Type type, Func<T, string> func, string name) where T : Attribute
        {
            object customAttr = null;
            if (string.IsNullOrEmpty(name))
            {
                customAttr = type.GetCustomAttributes(typeof(T), false).FirstOrDefault();
            }
            else
            {
                var item = type.GetProperty(name);
                if (item != null)
                {
                    customAttr = item.GetCustomAttributes(typeof(T), true).FirstOrDefault();
                }
                else
                {
                    var field = type.GetField(name);
                    if (field != null)
                    {
                        customAttr = field.GetCustomAttributes(typeof(T), true).FirstOrDefault();
                    }
                }
            }

            return customAttr == null ? null : func((T)customAttr);
        }

        /// <summary>
        /// 获取类型中的属性名,作为字典的key
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="name">名称</param>
        /// <returns>类型中的属性名</returns>
        public static string GetKey(Type type, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return type.FullName;
            }
            return type.FullName + "." + name;
        }
    }
}
 

 

4、添加测试类 TestMyStudent

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericReflectionGetsPropertyValues
{
/// <summary>
/// 测试类
/// </summary>
[MyTestAtts("TestMyStudentN", "TestMyStudentP")]
public class TestMyStudent
{
[System.ComponentModel.Description("123")]
[MyTestAtts("IdN", "IdP")]
public int Id { get; set; }

[System.ComponentModel.Description("123")]
[MyTestAtts("NameN", "NameP")]
public string Name { get; set; }

[MyTestAtts("AgeN", "AgeP")]
public int Age { get; set; }
}
}

5、修改Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericReflectionGetsPropertyValues
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("开始");
var item = (typeof(TestMyStudent)).GetCustomAttributeValue<MyTestAttsAttribute>(x => x.MyName, nameof(TestMyStudent.Name));//调用测试泛型反射获取属性的特性值
Console.WriteLine($"反射的特性 Attribute的值: {item}");
var item2 = (typeof(TestMyStudent)).GetCustomAttributeValue<MyTestAttsAttribute>(x => x.MyName, nameof(TestMyStudent.Name));//测试缓存
Console.WriteLine($"反射的特性 Attribute的值: {item2}");
//GetValue<MyAttsAttribute>(typeof(TestMyStudent), x => x.MyName, nameof(TestMyStudent.Name));
Console.Read();
}

/// <summary>
/// 泛型反射获取特性值
/// </summary>
/// <typeparam name="T">特性类型</typeparam>
/// <param name="type">类型</param>
/// <param name="func">获取特性值的委托</param>
/// <param name="name">属性名</param>
public static void GetValue<T>(Type type, Func<T, string> func, string name) where T : Attribute
{
var item = type.GetProperty(name);
var cus = item.GetCustomAttributes(typeof(T), true).FirstOrDefault();
Console.WriteLine($"反射 {item.Name}的特性 Attribute的值: {func((T)cus)}");
}
}
}

6、测试运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值