Project DescriptionStop writing wrapper classes, and do something more productive with your time.
This utility will automatically generate the wrapper class and return to you the wrapped object that implements your interface.
Tested in .Net 3.5 SP1
Edit: DynamicWrapper does not work in SIlverlight as I first though. My unit tests passed, but in runtime I get an exception when I try to use the class. I will be looking at some alternatives.Simply copy DynamicWrapper.cs from the Downloads tab into your solution and use it. There are two extension methods available to you:
- Interface wrapper = myObject.As<Interface>();
- myObject = wrapper.AsReal<MyObjectType>();
ExamplesLets assume you have a class without an interface. This is often the case with .Net framework classes. Your code relies on this framework object, but you want to abstract it in order to substitute it under test. Typically, you would write a wrapper class that acts as a proxy for the real object, but implements an interface. This utility creates that wrapper for you on the fly.
public class ClassWithoutInterface
{
public void DoSomething()
{
// Do something
}
}
public interface IDoSomething
{
void DoSomething();
}
In C#, you cannot write the following because
ClassWithoutInterface does not implement
IDoSomething:
IDoSomething actor = objectWithoutInterface as IDoSomething; // will resolve as null With this utility, you can do this:
IDoSomething actor = objectWithoutInterface.As<IDoSomething>(); // succeeds The important thing to know is that this is a wrapped object. Unlike casting, the
actor object is not the same as the real object -- it is a proxy to the real object. You can get the real object back out:
ClassWithoutInterface realObject = actor.AsReal<ClassWithoutInterface>(); That is all there is too this utility. Stop writing wrapper classes, and do something more productive with your time.
Blog post talking about this project:
http://houseofbilz.com/Default.aspx