简体   繁体   English

调用泛型类的方法

[英]Invoking a method of a Generic Class

Here is the Context :这是上下文:

I try to code a mapper for converting my DomainModel Objects to ViewModel Ojects dynamically.我尝试编写一个映射器,用于将我的 DomainModel 对象动态转换为 ViewModel 对象。 The problem I get, it's when I try to invoke a method of generic class by reflection I get this error :我遇到的问题是,当我尝试通过反射调用泛型类的方法时,出现此错误:

System.InvalidOperationException : Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true. System.InvalidOperationException :无法对 ContainsGenericParameters 为 true 的类型或方法执行后期绑定操作。

Can someone help-me to figure out where is the fault ?有人可以帮我找出问题所在吗? It would be greatly appreciated将不胜感激

Here is the Code (I tried to simplified it) :这是代码(我试图简化它):

public class MapClass<SourceType, DestinationType>
{

    public string Test()
    {
        return test
    }

    public void MapClassReflection(SourceType source, ref DestinationType destination)
    {
        Type sourceType = source.GetType();
        Type destinationType = destination.GetType();

        foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
        {
            string destinationPropertyName = LookupForPropertyInDestinationType(sourceProperty.Name, destinationType);

            if (destinationPropertyName != null)
            {
                PropertyInfo destinationProperty = destinationType.GetProperty(destinationPropertyName);
                if (destinationProperty.PropertyType == sourceProperty.PropertyType)
                {
                    destinationProperty.SetValue(destination, sourceProperty.GetValue(source, null), null);
                }
                else
                {

                       Type d1 = typeof(MapClass<,>);
                        Type[] typeArgs = { destinationProperty.GetType(), sourceType.GetType() };
                        Type constructed = d1.MakeGenericType(typeArgs);

                        object o = Activator.CreateInstance(constructed, null);

                        MethodInfo theMethod = d1.GetMethod("Test");

                        string toto = (string)theMethod.Invoke(o,null);

                }
                }
            }
        }


    private string LookupForPropertyInDestinationType(string sourcePropertyName, Type destinationType)
    {
        foreach (PropertyInfo property in destinationType.GetProperties())
        {
            if (property.Name == sourcePropertyName)
            {
                return sourcePropertyName;
            }
        }
        return null;
    }
}

You need to call GetMethod on the constructed type constructed , not on the type definition d1 .你需要调用GetMethod的构造类型constructed ,而不是类型定义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);

// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM