如何使用约束实现泛型方法(How to implement generic method with constraints)

编程入门 行业动态 更新时间:2024-10-21 07:47:56
如何使用约束实现泛型方法(How to implement generic method with constraints)

从我的头衔中可能有点难以理解我想要实现的目标,所以我会进一步了解细节。

我有以下界面:

public interface IModelBuilder<T> where T : IStandardTemplateTemplate { M Build<M>(T pTemplate, params object[] pParams) where M : BaseModel; }

现在我想在我的实际构建器中实现该接口。 我用来映射不同对象类型的构建器。 所以这看起来如下:

public class BusinessModelBuilder : IModelBuilder<IBusinessTemplate> { public virtual M Build<M>(IBusinessTemplate pTemplate, params object[] pParams) where M : BussinessModel { var businessModel = Activator.CreateInstance<M>(); // map data return businessModel; } }

现在问题如下。 我不能让约束工作。 由于我在接口上定义了约束,因此即使我的BusinessModel继承自BaseModel,也不会让我对实际方法使用不同的约束。 它一直告诉我我的约束M必须与界面的约束相匹配。 我尝试了几种不同的方法,但似乎都没有。

有谁知道这是否或如何实现? 我只是想在接口中告诉我允许继承模型的约束。

from my title it might be a little hard to understand what I'm trying to achieve, so I'll go a little further into detail.

I have the following interface:

public interface IModelBuilder<T> where T : IStandardTemplateTemplate { M Build<M>(T pTemplate, params object[] pParams) where M : BaseModel; }

Now I want to implement the interface in my actual builder. The builder I use to map different object types. So this looks as follows:

public class BusinessModelBuilder : IModelBuilder<IBusinessTemplate> { public virtual M Build<M>(IBusinessTemplate pTemplate, params object[] pParams) where M : BussinessModel { var businessModel = Activator.CreateInstance<M>(); // map data return businessModel; } }

Now the problem is the following. I can't get the constraint to work. Since I defined the constraint on the interface it won't let me use a different constraint on my actual method, even though my BusinessModel inherits from BaseModel. It keeps telling me my constraint M must match the constraint from the interface. I tried several different approaches, but none seem to work.

Does anyone know if or how this can be achieved? I just want to tell my constraint in the interface that inherited models are allowed.

最满意答案

以下是您的问题的简短但完整的示例:

public class Parent { } public class Child { } public interface Interface { void Foo<T>() where T : Parent; } public class Implementation : Interface { public void Foo<T>() where T : Child { } }

这不会编译,因为你的不会编译。 接口方法的实现必须对泛型参数具有完全相同的约束,它不能具有更严格的约束。

您的Build方法只能将类型约束为BaseModel ,而不是BussinessModel 。

如果更改IModelBuilder接口以添加其他类级别泛型参数,然后将用作约束,则可以获得所需的功能:

public interface IModelBuilder<T, Model> where T : IStandardTemplateTemplate where Model : BaseModel { M Build<M>(T pTemplate, params object[] pParams) where M : Model; } public class BusinessModelBuilder : IModelBuilder<IBusinessTemplate, BussinessModel> { public virtual M Build<M>(IBusinessTemplate pTemplate, params object[] pParams) where M : BussinessModel { var businessModel = Activator.CreateInstance<M>(); // map data return businessModel; } }

这个解决方案部分基于里德的答案,但更进了一步。

Here's a short but complete example of your problem:

public class Parent { } public class Child { } public interface Interface { void Foo<T>() where T : Parent; } public class Implementation : Interface { public void Foo<T>() where T : Child { } }

This won't compile, for the same reason that yours won't. The implementation of the interface method must have the exact same constraint on the generic argument, it can't have a more restrictive constraint.

Your Build method can only constrain the type to BaseModel, not BussinessModel.

If you alter your IModelBuilder interface to add an additional class level generic argument, and then use that as the constraint, then you can get the desired functionality:

public interface IModelBuilder<T, Model> where T : IStandardTemplateTemplate where Model : BaseModel { M Build<M>(T pTemplate, params object[] pParams) where M : Model; } public class BusinessModelBuilder : IModelBuilder<IBusinessTemplate, BussinessModel> { public virtual M Build<M>(IBusinessTemplate pTemplate, params object[] pParams) where M : BussinessModel { var businessModel = Activator.CreateInstance<M>(); // map data return businessModel; } }

This solution is based in part on Reed's answer but which takes it a step further.

更多推荐

本文发布于:2023-08-05 21:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437827.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   方法   implement   constraints   method

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!