简体   繁体   English

Castle DynamicProxy CreateClassProxyWithTarget不将基础对象用于非拦截属性

[英]Castle DynamicProxy CreateClassProxyWithTarget not using underlying object for non-intercepted properties

I have some simple DTO classes like: 我有一些简单的DTO类,例如:

[XmlType]
class Person : AbstractResource
{
  [XmlElement("name")]
  public virtual string FirstName { get; set; }

  public virtual string Nickname { get { return "Mister cool"; } }
}

class SpecialPerson : Person
{
  public override string NickName { get { return FirstName; } }
}

In my code I deserialise a list of Person/SpecialPerson objects from an XML file and then try to wrap them all in proxy instances with CreateClassProxyWithTarget. 在我的代码中,我反序列化了XML文件中的Person / SpecialPerson对象列表,然后尝试使用CreateClassProxyWithTarget将它们全部包装在代理实例中。 For some reason FirstName is always null and NickName is null for any SpecialPerson proxy but returns "Mister cool" for any Person proxy. 由于某些原因,对于任何SpecialPerson代理,FirstName始终为null,NickName为null,但对于任何Person代理,返回“ Mister cool”。

I went into the debugger and looked at the underlying wrapped object and it has all the correct values. 我进入调试器,查看了基础包装对象,它具有所有正确的值。 I have also taken care not to intercept on FirstName or Nickname. 我也注意不要截取名字或昵称。 I would expect the proxy to simply call the wrapped object and in some cases it does [1] but for most it does not. 我希望代理可以简单地调用包装的对象,在某些情况下,它可以[1],但对于大多数情况却不是。 What am I doing wrong? 我究竟做错了什么?

[1] In my interceptor code I set, via reflection, some of the properties of the wrapped object and those show up correctly. [1]在拦截器代码中,我通过反射设置了包装对象的某些属性,并且这些属性正确显示。 But I don't see why those properties would read from the underlying object but others will not. 但是我不明白为什么这些属性会从基础对象中读取,而其他属性则不会。 It's almost as if any intercepted properties will always call the wrapped object when Invoke is called but for any methods you have returned false for the ShouldInterceptMethod it does not. 几乎好像在调用Invoke时,任何被拦截的属性都将始终调用包装的对象,但是对于您应为ShouldInterceptMethod返回false的任何方法,它都不会。 But that would make no sense to me, if I say not to intercept a method on a wrapped object what other possible action should be taken but simply using the properties from the wrapped object? 但这对我来说毫无意义,如果我说不截取包装对象上的方法,还应该采取其他可能的措施,而只是使用包装对象中的属性吗?

Oh dear, I feel bad about answering my own question but I worked out what was going wrong and I hope this will help anyone else who gets in this situation. 噢,亲爱的,我对回答自己的问题感到很难过,但我弄清楚出了什么问题,希望这对其他遇到这种情况的人有所帮助。 I was unable to find anything on google about this. 我无法在Google上找到与此相关的任何信息。

So the issue is, even when you create a proxy that wraps an existing object (ie with CreateClassProxyWithTarget) it still creates a whole new object . 所以问题是,即使您创建了一个包装现有对象的代理(即使用CreateClassProxyWithTarget),它仍然会创建一个全新的对象 It just happens that that object has a way of getting the wrapped object. 碰巧该对象具有获取包装对象的方法。 For any methods, properties, etc. that you do not intercept they will be called on the proxy object . 对于您不会拦截的任何方法,属性等,它们将在Proxy对象上调用 And in the case of properties, these will not use the value in the wrapped object, even if the properties are virtual because the proxy object has its own copy of all those properties. 对于属性,即使这些属性是虚拟的,这些属性也不会使用包装对象中的值,因为代理对象拥有所有这些属性的副本。

To solve this, you have to get rid of your selection hook and just intercept everything. 为了解决这个问题,您必须摆脱选择钩子,然后拦截所有内容。 If it is a property you don't actually want to intercept you can simply call invocation.Proceed . 如果它是您实际上并不想要拦截的属性,则只需调用invocation.Proceed This will cause it to forward the request to the wrapped object. 这将导致它转发请求到包装的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM