[英]C# Can't cast to interface even though it inherits it
I have a small problem I can't seem to get working right. 我有一个小问题,我似乎无法正常工作。 I currently have a list of IModelConverters like this: 我目前有一个这样的IModelConverters列表:
public class ModelConverterList : List<IModelConverter<IDataCollection>>
{
}
And I am trying to add entries like this: 我正在尝试添加这样的条目:
public static void AddModelConverter<T>(IModelConverter<T> converter)
where T: IDataCollection
{
CheckForSetup();
modelConverters.Add(converter);
}
CheckForSetup is a method checking if the list isn't null (along with some other unrelevant checks). CheckForSetup是一种检查列表是否不为null的方法(以及其他一些不相关的检查)。 This is the interface I want to cast to: 这是我要转换为的接口:
public interface IModelConverter<in T>: IConverter where T: IDataCollection
{
ResponseData Activate(T documents, ServiceContext services, bool overrideIfNeeded = false);
bool ContainsFile(T document, ServiceContext services);
}
However, it doesn't want to cast to this interface. 但是,它不想转换为该接口。 I tried casting it to IModelConverter<T>
and IModelConverter<IDataCollection>
我尝试将其强制转换为IModelConverter<T>
和IModelConverter<IDataCollection>
The object I want to add has an abstract class that uses the interface, could this be why it isn't working? 我要添加的对象具有使用该接口的抽象类,这可能就是为什么它不起作用的原因吗?
I also thought about multiple references to it, but it doesn't seem like that's the case. 我也考虑过对此的多个引用,但事实并非如此。
EDIT: 编辑:
The error I get in the editor is this: "Argument 1: cannot convert from 'Extensions.Abstractions.Interfaces.IModelConverter' to 'Extensions.Abstractions.Interfaces.IModelConverter'" 我在编辑器中遇到的错误是:“参数1:无法从'Extensions.Abstractions.Interfaces.IModelConverter'转换为'Extensions.Abstractions.Interfaces.IModelConverter'”
And the class I want to add is this: 我要添加的类是这样的:
public class LanguageConverter : DocumentConverterCreatorBase<LanguageCollection>
{
protected override void ActivateDocument(LanguageCollection collection, ServiceContext services, bool overrideIfNeeded)
{
ILocalizationService fs = services.LocalizationService;
foreach (LanguageType language in collection.List)
{
if (fs.GetLanguageByIsoCode(language.CultureAlias) != null && overrideIfNeeded)
fs.Delete(fs.GetLanguageByIsoCode(language.CultureAlias));
if (fs.GetLanguageByIsoCode(language.CultureAlias) == null)
fs.Save(language.Construct(services));
}
}
public override bool ContainsFile(LanguageCollection document, ServiceContext services)
{
ILocalizationService ls = services.LocalizationService;
foreach (LanguageType item in document.List)
{
if (ls.GetLanguageByIsoCode(item.CultureAlias) == null)
return false;
}
return true;
}
} }
And the abstract class is this: 抽象类是这样的:
public abstract class DocumentConverterCreatorBase<T>: IAssetConverter, IModelConverter<T>, IModelCreator where T : IDataCollection
The abstract class has two abstract methods for the methods in the interface. 接口中的方法的抽象类有两个抽象方法。
The IDataCollection is nothing but a list of data. IDataCollection只是数据列表。 The interface is as follows: 界面如下:
public interface IDataCollection
{
int GetCount();
}
The problem you're seeing is one of co-variance. 您看到的问题是协方差之一。
If you have these type definitions: 如果您具有以下类型定义:
public class ModelConverterList : List<IModelConverter<IDataCollection>> { }
public interface IModelConverter<in T> : IConverter where T : IDataCollection { }
public interface IDataCollection { }
public interface IConverter { }
...then with this code: ...然后输入以下代码:
private static ModelConverterList modelConverters = new ModelConverterList();
public static void AddModelConverter<T>(IModelConverter<T> converter) where T : IDataCollection
{
modelConverters.Add(converter);
}
...you get the following error: ...您收到以下错误:
CS1503 Argument 1: cannot convert from 'IModelConverter<T>' to 'IModelConverter<UserQuery.IDataCollection>'
Even though we know that T
inherits from IDataCollection
, it isn't the same as saying that IModelConverter<T>
inherits from IModelConverter<IDataCollection>
- it doesn't. 即使我们知道T
继承自IDataCollection
,也与说IModelConverter<T>
继承自IModelConverter<IDataCollection>
-事实并非如此。 So there is no cast from IModelConverter<T>
to IModelConverter<IDataCollection>
. 因此,没有从IModelConverter<T>
到IModelConverter<IDataCollection>
。
This compiles: 这样编译:
public class ModelConverterList : List<IModelConverter<IDataCollection>> { }
public interface IModelConverter<out T> : IConverter where T : class, IDataCollection { }
public interface IDataCollection { }
public interface IConverter { }
private static ModelConverterList modelConverters = new ModelConverterList();
public static void AddModelConverter<T>(IModelConverter<T> converter) where T : class, IDataCollection
{
modelConverters.Add(converter);
}
But I've changed the definition of IModelConverter
from in T
to out T
and added a class
constraint. 但是我将IModelConverter
的定义从in T
更改为out T
并添加了一个class
约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.