在 Unity 中使用泛型 ... InvalidCastException
我的接口定义是: 公共接口 IInterface 其中 T:UserControl
我的类定义是: 公共部分类 App1Control :UserControl,IInterface
我的 app.config 的统一部分如下所示:
<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>
该应用程序运行良好,但是以下代码给出了 InvalidCastException
container.Resolve<IInterface<UserControl>>("Application 1");
错误消息是:
无法将“MyApplication.App1Control”类型的对象转换为“MyApplication.IInterface`1[System.Windows.Forms.UserControl]”类型
我相信我的代码中有一个小错误......但我无法弄清楚是什么。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自OP的评论Sunny D:
我的App1Control的定义有一个错误。当我将定义从: 更改
为:时,问题得到解决
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:
to: