Invoking a method of a Generic Class

后端 未结 1 1746
广开言路
广开言路 2021-01-04 11:07

Here is the Context :

I try to code a mapper for converting my DomainModel Objects to ViewModel Ojects dynamically. The problem I get, it\'s when I try to invoke a m

相关标签:
1条回答
  • 2021-01-04 12:00

    You need to call GetMethod on the constructed type constructed, not on the type definition d1.

    // ...
    
    Type d1 = typeof(MapClass<,>);
    Type[] typeArgs = { destinationProperty.GetType(), sourceType.GetType() };
    Type constructed = d1.MakeGenericType(typeArgs);
    
    object o = Activator.CreateInstance(constructed, null);
    
    MethodInfo theMethod = constructed.GetMethod("Test");
    
    string toto = (string)theMethod.Invoke(o, null);
    
    // ...
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题