gpt4 book ai didi

.net - 在 Unity 中使用泛型... InvalidCastException

转载 作者:行者123 更新时间:2023-12-03 17:59:59 25 4
gpt4 key购买 nike

我的接口(interface)定义是:
公共(public)接口(interface) IInterface 其中 T:UserControl

我的类(class)定义是:
公共(public)部分类 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");

错误信息是:

Unable to cast object of type 'MyApplication.App1Control' to type 'MyApplication.IInterface`1[System.Windows.Forms.UserControl]'



我相信我的代码中有一个小错误......但我无法弄清楚是什么。有什么想法吗?

最佳答案

来自 OP Sunny D 的评论:

我的 App1Control 的定义有误。当我将定义更改为:

public partial class App1Control : UserControl, myInterface<App1Control>

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

关于.net - 在 Unity 中使用泛型... InvalidCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2624604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com