If I have a MethodInfo on a closed generic Type is there an easy way to switch those types?

前端 未结 4 1463
醉酒成梦
醉酒成梦 2020-12-06 12:26

Let\'s say that I have the methodInfo for something like Nullable.HasValue. Is there anyway to convert it to Nullable.HasValue

4条回答
  •  粉色の甜心
    2020-12-06 12:54

    You can do it in one line with MethodBase.GetMethodFromHandle but in order to use this method you'll have to pass typeof(List) not just typeof(string).

    var methodinfo = typeof(List).GetMethod("Add");
    var methodinfo2 = MethodBase.GetMethodFromHandle(methodinfo.MethodHandle,
                                                     typeof (List).TypeHandle);
    
    Console.WriteLine(methodinfo);
    Console.WriteLine(methodinfo2);
    

    This link includes the above sample and an exlpanation of why ResolveMethod doesn't work.

    https://www.re-motion.org/blogs/mix/archive/2009/08/12/trying-to-resolve-a-method-in-a-closed-generic-type.aspx

提交回复
热议问题