简体   繁体   English

如何创建实现基本类型通用接口的类的实例

[英]How do I create instance of class implementing generic interface with base type

Lets say I have this: 可以说我有这个:

public class BaseEntity {}

public class Product : BaseEntity {}

public interface IPreLoad<T> where T : BaseEntity 
{
    void Preload();
}

public class ProductService : IPreload<Product> 
{
    public void Preload()
    {
        //Do something
    }
}

//Returns list of types implementing IPreload<>
var preloaders = typeFinder.FindClassesOfType(typeof(IPreLoad<>)).ToList(); 

foreach (var preloaderType in preloaders)
{
    var preloader = (IPreLoad<BaseEntity>)EngineContext.Current.ResolveUnregistered(preloaderType);
    cacheManager.Get(preloader.PreLoadCacheKey, () => preloader.PreLoad());
}

The issue I have is that I can't cast a ProductService : IPreLoad<Product> to a IPreLoad<BaseEntity> and I don't know how else I can cast this to achieve the same result. 我遇到的问题是我无法将ProductService : IPreLoad<Product>转换为IPreLoad<BaseEntity>而且我不知道如何将其转换为相同的结果。 Basically, I want to create an instance of this service and call preload. 基本上,我想创建此服务的实例并调用preload。

Not performing the cast works fine and it creates an instance but then as I haven't cast it as anything, I can't make any calls to it based on the interface. 不执行强制转换效果很好,它会创建一个实例,但是由于我还没有将其强制转换为任何实例,因此无法基于该接口对其进行任何调用。

Your interface should be covariant: 您的接口应该是协变的:

public interface IPreLoad<out T> where T : BaseEntity 
{
    void Preload();
}

Then, assigment will be ok: 然后,可以进行分配:

IPreLoad<BaseEntity> preLoader = new ProductService();

In your example, The Preload Method is not affected by the generic type T, you could use a non generic interface like 在您的示例中,Preload方法不受通用类型T的影响,您可以使用非通用接口,例如

public interface IPreLoad
{
    void Preload();
}

public interface IPreLoad<T>: IPreload where T : BaseEntity 
{
   // your other typed methods...
}

then you cast your objects as IPreload instead of IPreLoad<BaseEntity> 然后将对象转换为IPreload而不是IPreLoad<BaseEntity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建通用类型类的新实例 - How do I create new instance of Generic Type Class 如何返回具有泛型的类实例作为具有泛型的接口? - How can I return a class instance with a generic type as an interface with a generic type? 如何使用类型列表的参数创建泛型类型的实例 - How do I create an instance of a generic type with a parameter of type list 通用类型约束:如何创建 C# 文档中描述的 class 的实例 - Generic type constraint: How do I create an instance of the class described in this C# documentation 如何在基类中使用泛型? - How do i use generic type in my base class? 将实现接口的泛型类型类分配给作为接口的泛型类型 - Assign generic type class implementing interface to generic type being interface 创建一个List实例,其类型派生自基类和接口 - Create an instance of a List where the type derives from a base class and an interface 我可以将基本 class 类型作为通用参数传递给接口吗 - Can I pass a base class type as a generic parameter to an interface 如何创建一个泛型类,将泛型类作为泛型类? - How do I create a generic class that takes as its generic type a generic class? 具有通用类型的基类,该基类实现具有通用类型的接口 - Base class with a generic type which implements an interface with a generic type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM