【发布时间】:2013-08-01 14:14:25
【问题描述】:
//这里有一些代码
object target = Activator.CreateInstance(typeof(T));
PropertyInfo[] sourceProperties = sourceType.GetProperties();
foreach (PropertyInfo pi in sourceProperties)
{
PropertyInfo targetPi = typeof(T).GetProperty(pi.Name); //returns null why?
object piValue = pi.GetValue(source, null);
try
{
if (targetPi != null) // it doesnt work
{
targetPi.SetValue(target,piValue, null); // target has typeof(T)
}
}
catch { }
}
return(T)target;
}
【问题讨论】:
-
你没有告诉我们你想要达到的目标。您刚刚发布了格式错误的代码,仅此而已。请看tinyurl.com/so-hints
标签: c# reflection settings propertyinfo