C# >T。但是你发布的代码不包括任何抽象类类;您是否尝试使用其他类,或者您发布的代码是否不完整?这是我获得工作Activator.CreateInstance的唯一方法——可能是因为类型声明和扩展位于不同的程序集中(在我的解决方案中)?我喜欢新约束带来的改进。

C# >T。但是你发布的代码不包括任何抽象类类;您是否尝试使用其他类,或者您发布的代码是否不完整?这是我获得工作Activator.CreateInstance的唯一方法——可能是因为类型声明和扩展位于不同的程序集中(在我的解决方案中)?我喜欢新约束带来的改进。,c#,generics,reflection,extension-methods,C#,Generics,Reflection,Extension Methods,>T。但是你发布的代码不包括任何抽象类类;您是否尝试使用其他类,或者您发布的代码是否不完整?这是我获得工作Activator.CreateInstance的唯一方法——可能是因为类型声明和扩展位于不同的程序集中(在我的解决方案中)?我喜欢新约束带来的改进。。。我知道必须有更好的方法……关于扩展方法在不同的程序集中,我认为这不重要。当您调用它时,您已经输入了已解析的类型。使用此重载是否仍然不适用于您?它会抛出什么异常?你在这里说的有道理,但它不起作用。我添加了异常详细信息作为问题的更新。选择了您的


>T。但是你发布的代码不包括任何
抽象类
类;您是否尝试使用其他类,或者您发布的代码是否不完整?这是我获得工作Activator.CreateInstance的唯一方法——可能是因为类型声明和扩展位于不同的程序集中(在我的解决方案中)?我喜欢新约束带来的改进。。。我知道必须有更好的方法……关于扩展方法在不同的程序集中,我认为这不重要。当您调用它时,您已经输入了已解析的类型。使用此重载是否仍然不适用于您?它会抛出什么异常?你在这里说的有道理,但它不起作用。我添加了异常详细信息作为问题的更新。选择了您的答案。我想了解在没有新约束的情况下发生了什么——不足以提出另一个问题——如果对您有意义的话,也许可以在这里回答?@marfarma:从错误消息中,看起来您正在为类型
T
传递一个
抽象
类。但是你发布的代码不包括任何
抽象类
类;您是否尝试使用其他类,或者您发布的代码是否不完整?除非它没有返回正确的程序集-有关详细信息,请参阅更新。除非它没有返回正确的程序集-有关详细信息,请参阅更新。
public static object ToType<T>(this object obj, T type, string assembly, IDictionary<string,string> maps) {
    //create instance of T type object:
    var tmp = Activator.CreateInstance(assembly, type.ToString()); 

    foreach( var map in maps) {
        try 
        {   
            PropertyInfo source = obj.GetType()
                .GetProperty(map.Value);

            tmp.Unwrap().GetType().GetProperty(map.Key)
                .SetValue(tmp.Unwrap(), source.GetValue(obj, null), null);
        }
        catch 
        {
            throw new ArgumentException("Error converting to Type: "+type);
        }
    }
    return tmp.Unwrap();
}

[Test]
public void TestToTypeExtension()
{
    Source item = new Source();
    item.OtherObj_One     = "1234567890";
    item.OtherObj_Code      = "IBM.N";
    item.OtherObj_CodeType  = "S";
    item.OtherObj_CodeGroup = "EQUITY";

    Target row = (Target)item.ToType(typeof(Target), ((typeof(Target)).Assembly).FullName, Target.map);

    Assert.AreEqual(item.OtherObj_One, row.One);
    Assert.AreEqual(item.OtherObj_Code, row.Code);
    Assert.AreEqual(item.OtherObj_CodeType, row.CodeType);
}

public class Target
{
    public static Dictionary<String, String> map = new Dictionary<string, string>{
                                                    {"One"          ,"OtherObj_One"},
                                                    {"Code"         ,"OtherObj_Code"},
                                                    {"CodeType"     ,"OtherObj_CodeType"},

    };

    public String One { get; set; }
    public String Code { get; set; }
    public String CodeType { get; set; }

}

public class Source
{
    public String OtherObj_One { get; set; }
    public String OtherObj_Code { get; set; }
    public String OtherObj_CodeType { get; set; }
    public String OtherObj_CodeGroup { get; set; }

}
Test Name:  TestToTypeExtension
Test FullName:  Solution.Test.UtilsTests.TestToTypeExtension
Test Source:    [ ... ]\UnitTestProject1\UtilsTests.cs : line 39
Test Outcome:   Failed
Test Duration:  0:00:00.071

Result Message: System.MissingMethodException : Cannot create an abstract class.
Result StackTrace:  
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
at System.Activator.CreateInstance[T]()
at Util.MappingExtensions.ToType[T](Object obj, T type, String assembly, IDictionary`2 maps) in [ ... ]\Utils\MappingExtensions.cs:line 22
at Solution.Test.UtilsTests.TestToTypeExtension() in [ ... ]\UnitTestProject1\UtilsTests.cs:line 46
    public static object ToType<T>(this object obj, IDictionary<string, string> maps) 
           where T : new()
    {
        T tmp = new T();
        Type objType = obj.GetType();
        Type tType = typeof(T);

        foreach( var map in maps) {
            try 
            {   
                 PropertyInfo source = objType.GetProperty(map.Value);

                 tType.GetProperty(map.Key)
                         .SetValue(tmp, source.GetValue(obj, null), null);
             }
             catch 
             {
                 throw new ArgumentException("Error converting to Type: "+ tType);
             }
        }
        return tmp;
    }

    public static List<T> ToTypeList<T,U>(this List<U> source
                  , IDictionary<string, string> maps) 
        where T : new ()
    { 
        List<T> result = new List<T>();

        foreach (var item in source)
        {
            result.Add((T)item.ToType<T>(maps));
        }
        return result;
    }
var assembly = ((typeof(T)).Assembly).FullName;
public static T ToType<T>(this object obj, IDictionary<string, string> maps)
{
    //create instance of T type object:
    var tmp = Activator.CreateInstance(typeof(T).Assembly.FullName, typeof(T).ToString());

    foreach (var map in maps)
    {
        try
        {
            PropertyInfo source = obj.GetType()
                .GetProperty(map.Value);

            tmp.Unwrap().GetType().GetProperty(map.Key)
                .SetValue(tmp.Unwrap(), source.GetValue(obj, null), null);
        }
        catch
        {
            throw new ArgumentException("Error converting to Type: " + typeof(T));
        }
    }
    return (T)tmp.Unwrap();
}
 Target row = item.ToType<Target>(Target.map);
public static object ToType<T>(this object obj, IDictionary<string,string> maps) {
    //create instance of T type object:
    T tmp = Activator.CreateInstance<T>(); 

    foreach( var map in maps) {
        try 
        {   
            PropertyInfo source = obj.GetType()
                .GetProperty(map.Value);

            tmp.Unwrap().GetType().GetProperty(map.Key)
                .SetValue(tmp.Unwrap(), source.GetValue(obj, null), null);
        }
        catch 
        {
            throw new ArgumentException("Error converting to Type: "+ typeof(T));
        }
    }

    return tmp.Unwrap();
}
public static object ToType<T>(this object obj, IDictionary<string,string> maps) where T : new()
{
    //create instance of T type object:
    T tmp = new T();

    foreach( var map in maps) {
        try 
        {   
            PropertyInfo source = obj.GetType()
                .GetProperty(map.Value);

            tmp.Unwrap().GetType().GetProperty(map.Key)
                .SetValue(tmp.Unwrap(), source.GetValue(obj, null), null);
        }
        catch 
        {
            throw new ArgumentException("Error converting to Type: "+ typeof(T));
        }
    }

    return tmp.Unwrap();
}
Target row = item.ToType<Target>(Target.map)
public static object ToType<T>(this object obj, IDictionary<string,string> maps) where T : new()
{
    //create instance of T type object:
    T tmp = new T();

    Type objType = obj.GetType();
    Type tType = typeof(T);

    foreach( var map in maps) {
        try 
        {   
            PropertyInfo source = objType.GetProperty(map.Value);

            tType.GetProperty(map.Key)
                .SetValue(tmp, source.GetValue(obj, null), null);
        }
        catch 
        {
            throw new ArgumentException("Error converting to Type: "+ tType);
        }
    }

    return tmp;
}