gpt4 book ai didi

c# - 支持 IoC 的基类方法的接口(interface)声明

转载 作者:太空宇宙 更新时间:2023-11-03 11:47:03 25 4
gpt4 key购买 nike

代码如下:

interface A<T>
{
T Save();
}

interface B : A<B>
{
}

class BusinessBase<T>
{
T Save();
}

class D<T, U> : BusinessBase<T>
where T : BusinessBase<T>
where U : B<U>
{
new U Save()
{
base.Save(); // this is where the error is because T can't be converted to U
}
}

class Concrete : D<Concrete , B>, B
{
}

因此,我希望拥有的是一个基类,它定义了 A 的所有方法,这些方法实际上只是重定向到 C 中的方法。但是为了支持 IoC 和 CSLA,它必须是这样的。所以我正在寻找的是让 D 中的保存返回 U 而不是 T 以匹配接口(interface)签名..

我已经盯着这个看了一段时间,似乎无法弄清楚我错过了什么。

最佳答案

我想你需要:

class BusinessBase<T> : B
{
T Save();
}

和:

class D<T, U> : BusinessBase<T>
where T : BusinessBase<T>
where U : B<U>
{
new U Save()
{
return base.Save();
}
}

我认为这个错误是因为你在 BusinessBase<T>.Save() 之间没有任何隐式或显式的联系。和 B<U>你正试图返回。这是可能的还是会破坏其他接口(interface)?

您是否看过涵盖 CSLA 框架的 Expert C# 2008 Business Objects?

关于c# - 支持 IoC 的基类方法的接口(interface)声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3145726/

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