相关疑难解决方法(0)

调用泛型类的方法

这是上下文:

我尝试编写一个映射器,用于动态地将我的DomainModel对象转换为ViewModel Ojects.我得到的问题是,当我尝试通过反射调用泛型类的方法时,我得到了这个错误:

System.InvalidOperationException:无法对ContainsGenericParameters为true的类型或方法执行后期绑定操作.

有人可以帮助我找出错误在哪里吗?这将不胜感激

这是代码(我试图简化它):

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 = …
Run Code Online (Sandbox Code Playgroud)

.net c# generics reflection asp.net-mvc

11
推荐指数
1
解决办法
7025
查看次数

标签 统计

.net ×1

asp.net-mvc ×1

c# ×1

generics ×1

reflection ×1