About .NET, ASP.NET, MVC, C#, WPF, WCF and everything related to .NET and more.

Mugen Injection [Registering bindings via reflection]

The MugenInjector supports register binding via reflection.
This code demonstrate how you can register two interfaces(IAlpha, IBeta) to type(Beta):

public interface IAlpha { }
public interface IBeta { }

public class Alpha : IAlpha { }
public class Beta : Alpha, IBeta { }
public class Bravo : Beta { }

// Create your MugenInjector.
_injector = new MugenInjector();

//These code are equivalent
_injector.Bind(typeof(IAlpha), typeof(IBeta)).To(typeof(Beta));
_injector.Bind<IAlpha, IBeta>().To<Beta>();
All registered services are checked and if you are write this code:
_injector.Bind(typeof (IBeta)).To(typeof (Alpha));
You get an ArgumentException, with error 'The type of "IBeta" is not assignable from "Alpha".'
The MugenInjector knows how to distinguish a self-bindable type, and if you write this code, all be work fine:
// Create your MugenInjector.
_injector = new MugenInjector();

//Create new instance of Alpha as self-bindable type.
var alpha = _injector.Get<Alpha>();
The MugenInjector also supported open generic types:
public interface IRepository<T> { }
public class Repository<T> : IRepository<T> { }

// Create your MugenInjector.
_injector = new MugenInjector();

_injector.Bind(typeof (IRepository<>)).To(typeof (Repository<>));
var repository = _injector.Get<IRepository<Alpha>>();

Comments
Leave a Reply
*bold*
_italics_
+underline+
* Bullet List
** Bullet List 2
# Number List
## Number List 2
{"Do not apply formatting"}
{code:language} code here {code:language}.
Supports: aspx c#, c#, c++, html, sql, xml
[url:http://www.example.com]