以下工作,测试通过。但我对必须在调用中显式传递程序集名称感到不满。有没有更好的办法?
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; }
}
更新:
((typeof(T)).Assembly).FullName
在扩展方法中执行的值是:mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
如果我将对象创建语句更改为T tmp = Activator.CreateInstance<T>();
我收到以下错误:
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
更新2
获奖代码(包括相关的 List 扩展)如下所示。顺便说一句,这些扩展适用于匿名类型,例如那些由 linqselect new
表达式(即select new {prop1 = x, prop2 = y}
)返回的类型——这是我的主要动机。
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;
}