Using generics in Unity ... InvalidCastException

My interface definition: IInterface public interface, where T: UserControl

My class definition: open partial class App1Control: UserControl, IInterface

The unity section of my app.config is as follows:

  <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 application is working fine, but the following code gives an InvalidCastException

container.Resolve<IInterface<UserControl>>("Application 1");

Error message:

Unable to pass an object of type "MyApplication.App1Control" to enter "MyApplication.IInterface`1 [System.Windows.Forms.UserControl]"

I believe that there is a small error in my code ... but I cannot understand that. Any thoughts?

+5
1

OP Sunny D:

App1Control . , :

public partial class App1Control : UserControl, myInterface<App1Control>

public partial class App1Control : UserControl, myInterface<UserControl>
0

All Articles