Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我无法在字典中转换此实例_C#_Oop_Interface - Fatal编程技术网

C# 我无法在字典中转换此实例

C# 我无法在字典中转换此实例,c#,oop,interface,C#,Oop,Interface,我在写这句话: var factory = new Dictionary<Types, Func<IProblemFactory<IProblem>>>(); factory.Add(Types.Arithmetic, ()=> new ArithmeticProblemFactory())); public interface IProblem { ... } public interface IProblemFactory<T

我在写这句话:

var factory = new Dictionary<Types, Func<IProblemFactory<IProblem>>>();
        factory.Add(Types.Arithmetic, ()=> new ArithmeticProblemFactory()));

public interface IProblem { ... }
public interface IProblemFactory<T> where T : IProblem
{
    // Some stuff
}

public class Arithmetic<TResult> : IProblem
{ }
public class ArithmeticProblemFactory : IProblemFactory<Arithmetic<decimal>>
{ }
var factory=newdictionary();
Add(Types.算术,()=>新的算术问题factory());
公共接口问题{…}
公共接口IProbleFactory,其中T:IProblem
{
//一些东西
}
公共类算术:IProblem
{ }
公共类算术问题工厂:IProblemFactory
{ }
它告诉我这个错误:

错误1无法将类型“Exam.arithmetricProblemFactory”隐式转换为“Exam.ipProblemFactory”。存在显式转换(是否缺少强制转换?)

错误2无法将lambda表达式转换为委托类型“System.Func>”,因为块中的某些返回类型不能隐式转换为委托返回类型


我做错了什么人?

您需要使您的IProblemFactory协变来支持此场景:

public interface IProblemFactory<out T> where T : IProblem
公共接口IProbleFactory,其中T:IProblem
基本上,这意味着T可以是一个IProblem或任何在类似您的情况下实现它的东西

这里有几篇关于C#中协方差和逆变换的文章:


这就是我所说的问题工厂:阿方索非常讨厌。你救了我的命我知道这个问题对这件事没有意义但是。。你会推荐什么OOP书给我?不客气!在我看来,一旦你了解了OOP的基本知识和一些模式,练习就是掌握它的最好方法。这是一本介绍OOP、OOAD、UML等基础知识的好书: