簡體   English   中英

C#即使繼承,也無法轉換為接口

[英]C# Can't cast to interface even though it inherits it

我有一個小問題,我似乎無法正常工作。 我目前有一個這樣的IModelConverters列表:

public class ModelConverterList : List<IModelConverter<IDataCollection>>
{
}

我正在嘗試添加這樣的條目:

public static void AddModelConverter<T>(IModelConverter<T> converter) 
  where T: IDataCollection
{
    CheckForSetup();
    modelConverters.Add(converter);
}

CheckForSetup是一種檢查列表是否不為null的方法(以及其他一些不相關的檢查)。 這是我要轉換為的接口:

public interface IModelConverter<in T>: IConverter where T: IDataCollection
{
    ResponseData Activate(T documents, ServiceContext services, bool overrideIfNeeded = false);

    bool ContainsFile(T document, ServiceContext services);
}

但是,它不想轉換為該接口。 我嘗試將其強制轉換為IModelConverter<T>IModelConverter<IDataCollection>

我要添加的對象具有使用該接口的抽象類,這可能就是為什么它不起作用的原因嗎?

我也考慮過對此的多個引用,但事實並非如此。

編輯:

我在編輯器中遇到的錯誤是:“參數1:無法從'Extensions.Abstractions.Interfaces.IModelConverter'轉換為'Extensions.Abstractions.Interfaces.IModelConverter'”

我要添加的類是這樣的:

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;
}

}

抽象類是這樣的:

public abstract class DocumentConverterCreatorBase<T>: IAssetConverter, IModelConverter<T>, IModelCreator where T : IDataCollection

接口中的方法的抽象類有兩個抽象方法。

IDataCollection只是數據列表。 界面如下:

public interface IDataCollection
  {
    int GetCount();
  }

您看到的問題是協方差之一。

如果您具有以下類型定義:

public class ModelConverterList : List<IModelConverter<IDataCollection>> { }
public interface IModelConverter<in T> : IConverter where T : IDataCollection { }
public interface IDataCollection { }
public interface IConverter { }

...然后輸入以下代碼:

private static ModelConverterList modelConverters = new ModelConverterList();

public static void AddModelConverter<T>(IModelConverter<T> converter) where T : IDataCollection
{
    modelConverters.Add(converter);
}

...您收到以下錯誤:

CS1503 Argument 1: cannot convert from 'IModelConverter<T>' to 'IModelConverter<UserQuery.IDataCollection>'

即使我們知道T繼承自IDataCollection ,也與說IModelConverter<T>繼承自IModelConverter<IDataCollection> -事實並非如此。 因此,沒有從IModelConverter<T>IModelConverter<IDataCollection>

這樣編譯:

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);
}

但是我將IModelConverter的定義從in T更改為out T並添加了一個class約束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM