Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't do this, and for good reasons. This is not specific to Castle Windsor. The issue is that you have no guarantee that the methods are marked as <code>virtual</code> and therefore you have an inconsistency where there is some state coming from the wrapped object and some state coming from the proxy object.</p> <p>Think of the following very simple example:</p> <pre><code>abstract class AbstractPerson { public int Age { get; protected set; } public abstract void Birthday(); } class Person : AbstractPerson { public Person(int age) { Age = age; } public override Birthday() { Age++; } } </code></pre> <p>Let's say that we want to create a proxy for <code>AbstractPerson</code> to intercept <code>Birthday</code>.</p> <pre><code>class PersonProxy : AbstractPerson { readonly AbstractPerson wrappedPerson; public PersonProxy(AbstractPerson person) { wrappedPerson = person; } public override void Birthday() { DoInterceptors(); wrappedPerson.Birthday(); } public void DoInterceptors() { // do interceptors } } </code></pre> <p>Notice that we can't override <code>Age</code> because it's not marked as <code>virtual</code>. This is where the yucky state inconsistencies will come from:</p> <pre><code>Person knuth = new Person(71); PersonProxy proxy = new PersonProxy(knuth); Console.WriteLine(knuth.Age); knuth.Birthday(); Console.WriteLine(knuth.Age); Console.WriteLine(proxy.Age); </code></pre> <p>This will print</p> <pre><code>71 72 0 </code></pre> <p>to the console. What happened? Because <code>Age</code> is not marked as virtual, our proxy object can't override the base behavior and call <code>wrappedPerson.Age</code>. This example even shows that adding <code>Age = wrappedPerson.Age</code> to the constructor for <code>PersonProxy</code> will not help. Our proxy isn't really a proxy. This is why you can not wrap existing objects.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload