0
//這裏我如何設置targetPi?我認爲定義是不正確的,但我不知道如何解決這個問題?
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;
}
你還沒有告訴我們任何東西* *關於你想要達到的目標。你剛剛發佈格式不正確的代碼,就這些。請參閱http://tinyurl.com/so-hints –