简体   繁体   English

C#即使继承,也无法转换为接口

[英]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.

相关问题 C#泛型和接口继承接口 - C# Generics and Interface Inherits Interface 即使在 C# 接口内部有方法访问错误 class - Access error even though there is a method inside the C# Interface class 即使目录存在,我的C#代码也无法在C驱动器中看到我的目录。 - My C# code can't see my directory in my C drive…even though the directory exists 即使是C#winforms应用程序,由于缺少Android SDK也无法构建解决方案吗? - Can't build solution due to Android SDK missing even though it's a C# winforms application? 即使存在C#硒,也无法在页面上找到此元素 - Can't find this element on page even though it is exist C# selenium 在C#中,如果我的对象支持该接口,我如何检查T是否为IInterface类型并强制转换为? - In C# how can i check if T is of type IInterface and cast to that if my object supports that interface? 我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5) - How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5) C#报告mysql命令失败,即使它没有 - c# reports mysql command failed even though it didn't 即使实现了所需的接口,泛型对象也需要强制转换 - Generic object needs a cast even though it implements the required interface 如何在c#中将接口转换为类型? - How can I cast an Interface as its type in c#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM