从entry.CurrentValues中提取复杂类型 [英] Extracting a complex type from entry.CurrentValues

查看:64
本文介绍了从entry.CurrentValues中提取复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的某些域类包含街道地址的复杂类型。
我正在捕获所做更改的日志,并希望能够从ObjectStateEntry.CurrentValues

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

我的代码很详细此处

我想从CurrentValues中提取地址,如答案所示。

And I want to extract the address from CurrentValues as the answer suggests.

我可以在调试器的_userObject属性中看到该地址,但是我不知道

I can see the address in the _userObject property in the debugger, but i don't know how to extract it.

我尝试过

var obj = entry.CurrentValues[ordinal];
var rec = (DbDataRecord)obj;

下一步应该是什么?

推荐答案

    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;
    }

这篇关于从entry.CurrentValues中提取复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆