Extracting a complex type from entry.CurrentValues

◇◆丶佛笑我妖孽 提交于 2019-12-08 07:55:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!