问题
Some of my domain classes contain a complex type for a street address. I am capturing a log of my changes, and want to be able to reconstruct the address object from the ObjectStateEntry.CurrentValues
My code is detailed here
And I want to extract the address from CurrentValues as the answer suggests.
I can see the address in the _userObject property in the debugger, but i don't know how to extract it.
I have tried
var obj = entry.CurrentValues[ordinal];
var rec = (DbDataRecord)obj;
what should be next?
回答1:
public static T ConvertTo<T>(this DbDataRecord record)
{
T item = Activator.CreateInstance<T>();
for (int f = 0; f < record.FieldCount; f++)
{
var p = item.GetType().GetProperty(record.GetName(f));
if (p != null && p.PropertyType == record.GetFieldType(f))
{
p.SetValue(item, record.GetValue(f), null);
}
}
return item;
}
来源:https://stackoverflow.com/questions/19758252/extracting-a-complex-type-from-entry-currentvalues