开发者

Using generics in Unity ... InvalidCastException

开发者 https://www.devze.com 2022-12-27 03:34 出处:网络
My interface definition is: public interface IInterface where T:UserControl My class definition is: public partial class App1Control : UserControl, IInterface

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&g开发者_StackOverflowt;

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?


from a comment from the OP Sunny D:

There was a mistake in the definition of my App1Control. The issue was fixed when I changed the definition from:

public partial class App1Control : UserControl, myInterface<App1Control>

to:

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

精彩评论

暂无评论...
验证码 换一张
取 消