Using generics in Unity ... InvalidCastException
Posted
by Sunny D
on Stack Overflow
See other posts from Stack Overflow
or by Sunny D
Published on 2010-04-12T19:01:47Z
Indexed on
2010/04/12
19:13 UTC
Read the original article
Hit count: 324
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?
© Stack Overflow or respective owner