我试图弄清楚如何将 System.Data.Entity.DynamicProxies 克隆或转换为它的实际类。例如:
System.Data.Entity.DynamicProxies.Currency_F4008E27DE_etc is the proxy class
MyApp.Entities.Currency is the real class
MyApp.Entities 中的所有类都继承自 BaseEntity,所以我尝试在那里进行转换:
public abstract partial class BaseEntity
{
public T ShallowCopy<T>() where T : BaseEntity
{
return this.MemberwiseClone() as T;
}
// other BaseEntity properties not relevent here
}
然后将 DynamicProxies 转换为真正的类:
// this returns a DynamicProxies class
Currency currency = LookupDefaultCurrency();
// this one needs to return a Entities.Currency class
// (but currently returns a DynamicProxies class too
Currency pocoCurrency = (Currency)currency.ShallowCopy<Currency>();
HttpRuntime.Cache[key] = pocoCurrency;
这样做的原因是我想从此对象中删除所有实体框架跟踪等,并将其普通(POCO)属性存储在缓存中。而且我需要能够为所有 100 个左右的 Entity 类执行此操作,因此它必须是相当通用的 - 无需为每个属性手动说 object1.foo = object2.foo。