将类传递给方法需要接口的方法 [英] Issue passing class to method where method expects interface

查看:83
本文介绍了将类传递给方法需要接口的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



自从我在这里发布以来已经有一段时间了,但是我有一点母马,不能一辈子我记得我做错了什么。我很确定这很简单所以希望你能提供帮助。



我有以下(近似示例):



Hi All,

It's been a while since I've posted here but I'm having a bit of a mare and can't for the life of me remember what I'm doing wrong. I'm pretty sure it's fairly simple so hopefully you can help.

I have the following (approximated example):

public interface IFoo<T>
{   //...some other properties....
   List<T> Bars {get;set;}
}

public interface IBar
{   //...some properties...
}

public class Foo : IFoo<Bar>
{    //...some properties...
    List<Bar> Bars {get;set;}
}

public class Bar : IBar
{ //...some properties...
}





然后我有一个帮助类看起来有点像这样:





Then I have a helper class which looks a bit like this:

public interface IHelper 
{
   bool DoSomething(IFoo<IBar> model, string path);
}

public class Helper : IHelper
{ 
   public bool DoSomething(IFoo<IBar> model, string path)
   {
       //..Save model to path
       return true;
   }
}





所以,我遇到的问题是当我尝试将Foo的实例传递给我的助手方法我收到错误:

Argumet类型'Foo'不能分配给参数类型'IFoo< IBar>'



我不知道为什么我会收到此错误,因为Foo继承自IFoo,而Bar继承自IBar。



- ------------

附加信息

-------------

这就是Foo类最初的填充方式。这是来自网页的表单提交。如果我尝试声明Foo为 Foo:IFoo< IBar> 我收到错误:

无法创建实例表格提交时的界面。

。这是MVC生成的错误。因此,Foo的定义如上,使用 IFoo< Bar>





So, the issue I have is when I try to pass an instance of Foo to my helper method I get the error:
Argumet type 'Foo' is no assignable to parameter type 'IFoo<IBar>'

I'm not sure why I get this error because Foo inherits from IFoo and Bar inherits from IBar.

-------------
Additional Info
-------------
This is how the Foo class is initially getting populated. It's a form submission from a webpage. If I try to declare Foo to be Foo: IFoo<IBar> I get the error:
Cannot create an instance of an interface.
upon form submission. This is an MVC generated error. As a result Foo is defined as above using IFoo<Bar>

[HttpPost]
public ActionResult RequestSubmission(Foo model)
{
   if (ModelState.IsValid)
   {
       helper.DoSomething(model, Server.MapPath("/Assets/Template.docx"));
                return Json(new {IsSuccess=true});
   }
   return PartialView("Form", model);
        }





-------------

位范围

-------------

这个项目是一个没有认证的基本MVC5项目。

我有一个控制器,使用一个可怜的人控制器DI控制器,将辅助类注入其中。

我的MVC模型继承自接口以允许它们通过。



因此有效的Foo和Bar是数据模型。



在我尝试重新编写它以使用接口之前,这一切都正常工作,因为紧密耦合我没有任何问题。



所以,我的总结问题:



为什么我会收到此错误,我该如何解决?



如果您需要更多信息或澄清任何事情只需大喊:-)



希望你可以帮助。



-------------
Bit of scope
-------------
This project is a basic MVC5 project with no authentication.
I have a controller that, using a poor mans DI controller, is having the helper class injected into it.
My MVC models inherit from the interfaces to allow them to be passed through.

So effectively Foo and Bar are data models.

This was all working before I tried to re-write it to use Interfaces, as tightly coupled I had no problems at all.

So, my question in summary:

Why do I get this error, and how can I resolve it?

If you need more information or clarification on anything just yell :-)

Hope you can help.

推荐答案

尝试使用这些声明:

Try with these declarations:
public interface IFoo<T>
{   //...some other properties....
   List<T> Bars {get;set;}
}
 
public interface IBar
{   //...some properties...
}
 
public class Foo : IFoo<IBar>
{    //...some properties...
    List<IBar> Bars {get;set;}
}
 
public class Bar : IBar
{ //...some properties...
}



代替。


instead.


下面是对我在stackoverflow上发布相同问题的回复,并为我解决了问题:





您也可以使用通用类型条件,例如,如果您将IHelper更改为:



The below came from a response to me posting the same question on stackoverflow and solved the problem for me:


You can also do this with generic type conditions, for example, if you change your IHelper to this:

public interface IHelper
{
    bool DoSomething<t>(IFoo<t> model, string path) where T : IBar;
}</t></t>



然后你可以实现它:




You can then implement it:

public class Helper : IHelper
{
    public bool DoSomething<t>(IFoo<t> model, string path) where T : IBar
    {
        //..Save model to path
        return true;
    }
}</t></t>



并致电




And calling

someHelperInstance.DoSomething(someInstanceOfFoo, somePath);



不再抛出该错误。这是因为您正在限制通用IFoo< t>是任何IBar派生类型。您可能希望了解MSDN上的协方差与逆变。


No longer throws that error. This is because you are constraining the generic IFoo<t> to be any IBar derived type. You may want to read up on covariance vs contravariance on MSDN.


这篇关于将类传递给方法需要接口的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆