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

时间:2017-11-06 10:46:10

标签: c# generics interface

我有一个小问题,我似乎无法正常工作。我目前有一个像这样的IModelConverters列表:

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

我正在尝试添加这样的条目:

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

CheckForSetup是一个检查列表是否为空的方法(以及其他一些不相关的检查)。 这是我想要转换的接口:

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

1 个答案:

答案 0 :(得分:2)

您所看到的问题是共同变化之一。

如果您有这些类型定义:

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并添加了internal class NoDiscriminatorConvention : ConventionBase, IClassMapConvention { public void Apply(BsonClassMap classMap) { classMap.SetDiscriminatorIsRequired(false); } } 约束。

相关问题