Skip to content

A utility to automatically generate the wrapper class and return to you the wrapped object that implements your interface.

License

Notifications You must be signed in to change notification settings

skyguy94/dynamic-wrapper

Repository files navigation

Stop writing wrapper classes, and do something more productive with your time.

This is a transport of a project originally hosted on on CodePlex, written by Brian Genisio.

Build status Gitter NuGet version

This utility will automatically generate the wrapper class and return to you the wrapped object that implements your interface.

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>();

Examples

Lets 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 to this utility. Stop writing wrapper classes and do something more productive with your time.

About

A utility to automatically generate the wrapper class and return to you the wrapped object that implements your interface.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages