1

With this class definition:

public class Wrapper<T>
{
  public T Item;

  public Wrapper(T item)
  {
    Item = item;
  }

  public static implicit operator T(Wrapper<T> wrapper)
  {
    return wrapper.Item;
  }

  public static implicit operator Wrapper<T>(T item)
  {
    return new Wrapper<T>(item);
  }
}

I am wondering if there is a way to invoke a method as the Item as the Wrapper object, without doing the obvious Wrapper.Item.Bar();

So rather than do this:

Wrapper<Foo> wrappedFoo = new Foo();
wrappedFoo.Item.Bar();

I want to do this:

Wrapper<Foo> wrappedFoo = new Foo();
wrappedFoo.Bar(); //equivalent to wrappedFoo.Item.Bar();

Thanks for your help.

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
adrotter
  • 301
  • 3
  • 10
  • 1
    Can you clarify the purpose? It looks more like you're looking for `interfaces` than generic wrappers. – Claus Jørgensen Dec 20 '18 at 00:48
  • What are you trying to achieve by creating such a wrapper? – ProgrammingLlama Dec 20 '18 at 00:49
  • Yeah I can, I am using a more complicated class than the above one, which is a LRU Cache item. The class has a last accessed time, and everytime we access Item we update the last accessed time. I want to be able to make any type of object a LRU cache item, and have the LRU cache item be treated just like the item itself. – adrotter Dec 20 '18 at 00:51
  • Is [this helpful](https://stackoverflow.com/questions/15733900/dynamically-creating-a-proxy-class)? – ProgrammingLlama Dec 20 '18 at 00:58
  • Yes, that pointed me to the right direction, thank you! – adrotter Dec 20 '18 at 01:14

0 Answers0