使用反射在程序集类型中查找 Interface<T>

内特W

我不确定是否是尖括号导致了我的问题,但我无法在 Stack Overflow 的答案中找到答案。如果已经有一个,我欢迎你指出正确的方向。

总而言之,我有一个接口,我无法通过反射找到它。让我们设置舞台:

接口

interface ICrew
{
    string Name { get; set; }
}

interface IDataService<T>
{
    IEnumerable<T> GetItems();
}

班级

class Crew : ICrew
{
    public string Name { get; set; }
}

class CrewRepository : IDataService<ICrew>
{
    DbContext _context { get; }

    public CrewRepository(DbContext context)
    {
        _context = context;
    }


    public IEnumerable<ICrew> GetItems()
    {
        return _context.Crews;
    }
}

class DbContext
{
    public DbContext()
    {
        Crews = new List<Crew>();
    }
        
    public List<Crew> Crews;
}

代码

class Program
{
    private static DbContext _context;

    static void Main(string[] args)
    {
        _context = new DbContext();

        //I want to find CrewRepository (which is type of IDataService<ICrew>)
        var dataService = Get(typeof(ICrew));
    }

    private static object Get(Type T)
    {
        var types = typeof(CrewRepository).Assembly.GetTypes();

        var test = types.Where(t => t.IsAssignableFrom(T)); //Finds ICrew

        var test2 = types.Where(t => t.GetInterfaces().Contains(T)); //Finds Crew

        var test3 = types.Where(t => t.GetInterfaces().Contains(typeof(IDataService<>))); //Finds nothing

        var test4 = types.Where(t => t.IsAssignableFrom(typeof(IDataService<>))); //Finds interface IDataService

        var test5 = types.Where(t => typeof(IDataService<>).IsAssignableFrom(t)); //Finds interface IDataService

        var instance = types
            .Where(t => t.IsAssignableFrom(T))
            .FirstOrDefault();

        if (instance != null)
        {
            return Activator.CreateInstance(instance, _context);
        }

        return default;
    }

}

我不知道如何正确查询程序集类型以获取IDataService<T>. 我的Get方法直到运行时才知道它会被要求什么类型,并且只有在编译时知道类型时才能使用尖括号内的泛型。谁能帮我弄清楚我应该在这里做什么才能Get通过使用反射返回我希望的正确类存储库?

海尼

正如评论中提到的,必须使用该Type.MakeGenericType方法IDataService<"T">在运行时创建所需的类型您的Get方法可能如下所示:

private static object Get(Type t)
{
    var types = typeof(CrewRepository).Assembly.GetTypes();
    var runtimeType = typeof(IDataService<>).MakeGenericType(t);
    var type = types.SingleOrDefault(x => x.GetInterfaces().Contains(runtimeType));

    if (type != null)
    {
        return Activator.CreateInstance(type, _context);
    }

    return null;
}

演示:https : //dotnetfiddle.net/vRJzr8

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Unity解析Interface <T>

使用C#的Interface <T>的接口

Java中的“ Interface <T>”到底是什么意思?

如何使用反射在Go中查找空结构值?

使用反射在Golang中打印结构指针字段类型

如何遍历反射结构上的* T函数,而不是reflect.TypeOf(interface {})?

在Golang中传递interface {}或[] interface {}

使用反射在运行时实例化List <T>的派生类

使用返回泛型 interface<T> 的工厂的类解析 T 列表,而不仅仅是 T

[] interface {}的元素类型

如何使用反射来检查结构字段的类型是否为interface {}?

使用map [string] int作为map [interface {}] interface {}类型的参数

如何使用反射在VB.Net中查找嵌套类?

使用反射在`this`类中查找私有字段,并将其实例化

解析<T扩展Interface <T >>并结束潜在的无限循环

C# Casting T where T: struct to an interface without boxing

如何使用反射获取Func <T>的返回类型?

如何使用反射检测通用值任务类型 ValueTask<T>

Interface Builder中的WKWebView

使用map [string] interface {}:

使用反射在单个实用程序方法中调用类的不同方法的最佳方法

为什么编译器在具有双界泛型类型时不将 T(interface) 推断为实现 T 的类?

如何使用interface {}作为通配符类型?

无法从[[] interface {}`断言类型[[] string`

interface {}进行函数类型转换

通过反射将类型“ interface {}”转换为类型“ interface {}”的切片中的另一个接口

开始-通过反射附加到[] interface {}

与不同实现列表上的 Interface<T> 方法交互

在@interface中声明类实例