Using generics in Unity ... InvalidCastException
- by Sunny D
Hi,
My interface definition is:
public interface IInterface where T:UserControl
My class definition is:
public partial class App1Control : UserControl, IInterface
The unity section of my app.config looks as below:
<unity>
<typeAliases>
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
<typeAlias alias="myInterface" type="MyApplication.IInterface`1, MyApplication" />
<typeAlias alias="App1" type="MyApplication.App1Control, MyApplication" />
</typeAliases>
<containers>
<container>
<types>
<type type="myInterface" mapTo="App1" name="Application 1">
<lifetime type="singleton"/>
</type>
</types>
</container>
</containers>
</unity>
The app runs fine but, the following code gives a InvalidCastException
container.Resolve<IInterface<UserControl>>("Application 1");
The error message is :
Unable to cast object of type 'MyApplication.App1Control' to type 'MyApplication.IInterface`1[System.Windows.Forms.UserControl]'
I believe there is a minor mistake in my code ... but am not able to figure out what. Any thoughts?