C#把Type当做泛型T,来作为方法的泛型进行使用

问题点:

一般情况下,是无法把Type类型当做泛型T来使用的,你会收到IDE的提示如下
在这里插入图片描述
这里当然可以直接将Person传入泛型,但我使用一个Person类来获取Type只是为了测试,
因为有一些情况下我们只有运行中的Type对象,并不知道它具体叫什么
并要使用其作为方法的泛型

按IDE的提示,我们无法这样直接使用,
但是微软为我们提供了反射的方法实现这一功能

解决方法

使用
请添加图片描述

 public class Tests
    {
        public static string TestMethod<T>()
        {
            Console.WriteLine("具体的传入类型为"+typeof(T));
            return "这是个测试";
        }

        internal class Person
        {
            public string Name { get; set; }
            public int Id { get; set; }
        }
        [Test]
        public void Test1()
        {
            
            Type targetType = typeof(Person);
			//无法直接使用
            Tests.TestMethod<targetType>();
            
			//可以使用,MakeGenericMethod可以获取多个一个及多个泛型
            MethodInfo method = typeof(Tests).GetMethod("TestMethod").MakeGenericMethod(targetType);

            Console.WriteLine(method.Invoke(null, null));
        }


        [SetUp]
        public void Setup()
        {
        }

        
        }
   }     

请添加图片描述

解决的实际问题:

我有一个ASP.NET CORE的缓存工具,
在gitee上可以查看https://gitee.com/godenSpirit/cache-attribute
使用的是SqlSugar提供的Redis帮助类来实现下面的接口

public interface ICacheable
    {
        /// <summary>
        /// 泛型获取
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        T Get<T>(string key);

        /// <summary>
        /// object获取
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        object Get(string key);
		...........
    }

其中使用非泛型缓存方法时会得到别多包装一层的object缓存,无法在正确返回到Swagger
故改用泛型方法,
具体代码如下,

//[CacheAble策略]的方法执行前,第一个策略每一行都注释一下,如果回头看只需参考这个即可
CacheAble_Strategy_Before = (context, _cache) => {
    //获取调用接口的Controller
    ControllerActionDescriptor controller = (ControllerActionDescriptor)context.ActionDescriptor;
    //进一步获取调用的接口的反射对象
    MethodInfo method = controller.MethodInfo;
    //从反射上拿到策略对应的Attribute,
    CacheAbleAttribute attribute = (CacheAbleAttribute)method.GetCustomAttribute(typeof(CacheAbleAttribute));

    //获取Attribute中需要解析的缓存名属性TotalName,解析ToTalName,替换得出真正的缓存名
    string real_cacheName = CacheNameResolver.ResolveTotalName(attribute.Totoal_Name, context);

    //如果注解的参数为空则直接报异常
    if (string.IsNullOrEmpty(attribute.preffix) || string.IsNullOrEmpty(attribute.Totoal_Name))
        throw new CacheAbleArgsException("The Args Cannot Be NULL");

    //拿到解析的缓存名去缓存里找是否有该缓存
    if (_cache.Exist(attribute.preffix + real_cacheName)&&attribute.work==true)
    {
		
        //StandResult<object> res = _cache.Get<StandResult<object>>(attribute.preffix + real_cacheName); 测试用的标志返回类型,可以正确获取

        //获取返回值的类型
        Type returnType = method.ReturnType;
        //获取泛型Get方法的反射(通过返回值的Type指定方法泛型)
        MethodInfo GetGen = _cache.GetType().GetMethod("Get").MakeGenericMethod(returnType);
        //放射调用泛型Get方法,获取正确的缓存信息
        object res = GetGen.Invoke(_cache,new object[] {attribute.preffix+real_cacheName});

        return res;
    }
    else
    {
        return null; //没找到就返回Null
    }
    
};

详情可以查看我的这个CacheAttribute工具的源码,欢迎提出意见。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗马苏丹默罕默德

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值