What you're trying to accomplish, is known as duck typing. There are some dedicated libraries that will let you do that, although I haven't used any of them With little effort (and some reflection) you can use Castle Dynamic Proxy to do that, using approach outlined here If for some reason the interceptor based approach would not be acceptable for you Dynamic Proxy does not support that out of the box (yet), but if you use version 2.2 beta, it would be fairly easy to provide that in a strongly typed manner (without using interceptors), by providing your own proxy type builder (take a look at how mixins are implemented).
What you're trying to accomplish, is known as duck typing. There are some dedicated libraries that will let you do that, although I haven't used any of them. With little effort (and some reflection) you can use Castle Dynamic Proxy to do that, using approach outlined here.
If for some reason the interceptor based approach would not be acceptable for you Dynamic Proxy does not support that out of the box (yet), but if you use version 2.2 beta, it would be fairly easy to provide that in a strongly typed manner (without using interceptors), by providing your own proxy type builder (take a look at how mixins are implemented).
1 Thanks for joining :) – Mark Seemann Dec 17 '09 at 19:31.
You might want to look at Castle Project's DynamicProxy. That's what Rhino. Mocks uses for its type proxying.
I haven't used it myself, so I don't know how much effort it will require on your part, but I suspect it's a good starting point.
AFAIK, the current version of Dynamic Proxy only works on interfaces and concrete types, and can only intercept virtual methods. It doesn't do mapping. However, it may address this in the future: using.castleproject.Org/display/CASTLE/… (scroll down to "Type Wrapping") – Mark Seemann Dec 17 '09 at 8:46 @Mark: The question only mentions working with interfaces, so that doesn't sound like a relevant significant restriction to me.
– Jon Skeet Dec 17 '09 at 9:01 @Jon Skeet: Yes, but he wants to wrap/proxy a concrete class that neither implements an interface nor has virtual methods, so I think it is very relevant. – Mark Seemann Dec 17 '09 at 9:59 @Mark: But he only needs to intercept the interface calls - he can make those call the non-virtual methods on the specific instance fairly easily, I'd imagine.It won't be a one-liner, but it's a starting point. Maybe I'm missing the point, but if it works for mocks I can't see how it couldn't work for this... – Jon Skeet Dec 17 '09 at 10:16 @Jon: I managed to draw Krzysztof Koźmic to answer this question as well.
I'll let him have the final word here - he knows a lot more about Dynamic Proxy than I do :) – Mark Seemann Dec 17 '09 at 19:30.
Here's a slightly different approach using generics. This would need a little more work thought. You need to implement a wrapper for each Interface and call all the methods with reflection.
Class FooWrapper : IFoo { private T obj; public FooWrapper(T obj) { this. Obj = obj; } public void method() { // call method with reflection // obj.method(); } } IFoo foo = new FooWrapper(new Bar()).
1: would do the same. Plus adding some reflection cache to minimize the performance penalty added by finding the methods by reflection – Marc Wittke Dec 17 '09 at 11:10 Nice idea, but if you are using Reflection, then you may as well go for some existing duck typing solutions. At the end, for performance reasons, you would end up Emiting IL code and reinventing the wheel.
– Groo Dec 17 '09 at 11:53.
You could create a new class class SubBar : IFoo, Bar { } and instantiate that (Assuming Bar truly has the ducktype, i.e. , the exact IFoo methods).
If you want light and simple Duck typing support, you can also check out: Duck Typing project. It works with . Net 2.0 and newer.
Usage example (taken from David Meyer's site): public interface ICanAdd { int Add(int x, int y); } // Note that MyAdder does NOT implement ICanAdd, // but it does define an Add method like the one in ICanAdd: public class MyAdder { public int Add(int x, int y) { return x + y; } } public class Program { void Main() { MyAdder myAdder = new MyAdder(); // Even though ICanAdd is not implemented by MyAdder, // we can duck cast it because it implements all the members: ICanAdd adder = DuckTyping. Cast(myAdder); // Now we can call adder as you would any ICanAdd object. // Transparently, this call is being forwarded to myAdder.Int sum = adder.
Add(2, 2); } } Using extension methods, you could simplify it into something like this (simlar to Bart De Smet's syntax) MyAdder myAdder = new MyAdder(); // this doesn't implement the interface ICanAdd adder = myAdder.AsIf(); // but it quacks like a duck int sum = adder. Add(2, 2).
If you are willing to use . NET 4, a solution could be to define the wrapper class as a DynamicObject, and convert the calls to the dynamic methods to calls to the wrapped class by using reflection (I'm not sure if that would actually be less work, anyway; also take in account possible performance concerns associated to the usage of reflection).
Take a look at Introducing “The C# Ducktaper†– Bridging the dynamic world with the static world as this blog post describes exactly what you need.
Although I haven't used them myself I think T4 templates in Visual Studio can be used for code generation of dynamic types link text.
If your class Bar already has all the functions from the interface you could just write class Bar : IFoo { and just recompile it. Now your class can also be casted as that interface. But this only works, if you have access to the code of class Bar.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.