//原始對象
Test test =
new
Test();
test.Name =
"反射"
;
var
props = test.GetType().GetProperties();
StructData structData =
new
StructData();
Object someBoxedStruct = structData;
StructData someUnBoxedStruct =
new
StructData();
foreach
(
var
item
in
props)
{
var
setobj = structData.GetType().GetProperty(item.Name);
//從舊對象中獲取值
var
value = item.GetValue(test,
null
);
Type myType =
typeof
(StructData);
FieldInfo myFieldInfo = myType.GetField(item.Name);
myFieldInfo.SetValue(someBoxedStruct, value.ToString());
someUnBoxedStruct = (StructData)someBoxedStruct;
}