C# 无法将泛型类型大小写为父类型
我有以下课程C# 无法将泛型类型大小写为父类型,c#,C#,我有以下课程 public interface IEngine { void DoSomething(); } 我试图通过以下方法实现这一目标: IDictionary<string, string> engineLocales = GetEngineLocalesFromConfig(); IDictionary<string, IEngine> engines = GetEnginesFromConfig(); IDictionary<stri
public interface IEngine
{
void DoSomething();
}
我试图通过以下方法实现这一目标:
IDictionary<string, string> engineLocales = GetEngineLocalesFromConfig();
IDictionary<string, IEngine> engines = GetEnginesFromConfig();
IDictionary<string, IEngine> mappedEngines = MapEngines(engineLocales, engines);
// ...
private static IDictionary<string, IEngine> GetEnginesFromConfig(EnginesElement enginesElement)
{
var engines = new Dictionary<string, IEngine>();
foreach (PropertyInformation property in enginesElement.ElementInformation.Properties)
{
var factory = property.Value as EngineElement<IEngine>;
engines[property.Name] = factory.Create();
}
return engines;
}
但是,这不起作用,因为从EngineElement
到EngineElement
的案例失败。我不想让映射这些内容的代码知道实现的必要性
有没有一种方法可以在不知道具体实现的情况下实现这种类型的动态映射?如果没有,我还有什么选择呢?我可以通过调用堆栈中的
IEngineFactory
来获得所需的行为。因为我不关心它的实际实现,只关心它创建的能力
我将iEngineeFactory
更改为
public interface IEngineFactory
{
IEngine Create();
}
public abstract class EngineElement : ConfigurationElement, IEngineFactory
{
public abstract IEngine Create();
}
public class FooEngineElement : EngineElement
{
[ConfigurationProperty("id", IsRequired = true)]
public string Id
{
get { return (string) this["id"]; }
set { this["id"] = value; }
}
public override IEngine Create()
{
return new FooEngine(Id);
}
}
EngineElement
to
public interface IEngineFactory
{
IEngine Create();
}
public abstract class EngineElement : ConfigurationElement, IEngineFactory
{
public abstract IEngine Create();
}
public class FooEngineElement : EngineElement
{
[ConfigurationProperty("id", IsRequired = true)]
public string Id
{
get { return (string) this["id"]; }
set { this["id"] = value; }
}
public override IEngine Create()
{
return new FooEngine(Id);
}
}
fooEngineeElement
to
public interface IEngineFactory
{
IEngine Create();
}
public abstract class EngineElement : ConfigurationElement, IEngineFactory
{
public abstract IEngine Create();
}
public class FooEngineElement : EngineElement
{
[ConfigurationProperty("id", IsRequired = true)]
public string Id
{
get { return (string) this["id"]; }
set { this["id"] = value; }
}
public override IEngine Create()
{
return new FooEngine(Id);
}
}
以及工厂的创建
var factory = property.Value as EngineElement;
我不明白你的目标…底线:演员阵容失败是因为
AbstractFoo
不是AbstractFoo
。根据AbstractFoo
是什么,您可以通过将类型声明为接口而不是抽象基类(您仍然可以使用抽象基类实现接口)来实现所需的功能。有关更多详细信息,请参阅许多现有的解决泛型类型差异的堆栈溢出问题。@PeterDuniho我已更新了我的问题,以更具体地说明我试图实现的目标。不幸的是,知道为什么要从更派生的泛型类型参数强制转换为基类型并不能改变一切。事实是你不能那样做。它只是在泛型类或方法中不受支持。您需要声明一个变体接口来完成类似的任务。如果在研究了C#通用接口的差异并尝试使用该功能解决您的场景之后,您仍然存在问题,那么您可以发布一个新问题,其中包括一个很好的演示您尝试过的内容的问题,以及关于该代码的特定问题。
public class FooEngineElement : EngineElement
{
[ConfigurationProperty("id", IsRequired = true)]
public string Id
{
get { return (string) this["id"]; }
set { this["id"] = value; }
}
public override IEngine Create()
{
return new FooEngine(Id);
}
}
var factory = property.Value as EngineElement;