相关疑难解决方法(0)

获取提供给泛型方法的泛型参数类型和值

如何获得提供给闭合/构造的泛型方法的参数值?

我已经有一段时间没有触及反射.所有这些都曾经在我的后面,嗯,无论如何.

class Program
{
    static void Main(string[] args)
    {
        new ConcreteFoo().GenericMethod<int>(5);
        Console.ReadKey();
    }
}

class ConcreteFoo
{
    public void GenericMethod<Q>(Q q) 
    {
        var method = MethodInfo.GetCurrentMethod();    
        var parameters = method.GetParameters();    
        if (parameters.Length > 0)
            foreach (var p in parameters)
                Console.WriteLine("Type: {0}", p.ParameterType);

        // That still prints Q as the type. 
        // I've tried GetGenericArguments as well. No luck.                
        // I want to know:
        // 1) The closed type, i.e. the actual generic argument supplied by the caller; and
        // …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection

9
推荐指数
1
解决办法
1640
查看次数

标签 统计

.net ×1

c# ×1

reflection ×1