首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >比较generic-interface-type与给定类型

比较generic-interface-type与给定类型
EN

Stack Overflow用户
提问于 2018-05-29 19:35:45
回答 2查看 1K关注 0票数 1

我正在尝试动态创建一个从泛型interface继承的类型的实例。

例如,我有以下base-interface,其中有几个其他接口派生自:

代码语言:javascript
复制
public interface IDummy { }

我有两个派生接口:

代码语言:javascript
复制
public interface IDummyDerived<T> : IDummy
{
  void Foo(T value);
}

public interface ITempDerived<T> : IDummy
{
  void HelloWorld(T value);
}

现在我需要一个ServiceProvider-Class,我可以在其中创建、查找实现给定接口的类。每个接口(IDummyDerived和ITempDerived)只实现一次。

我的方法是:

代码语言:javascript
复制
internal class DummyServiceProvider
{
  public T GetDummy<T>() where T : IDummy
  {
    Type baseType = typeof(IDummy);
    Type[] types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(p => baseType.IsAssignableFrom(p) && p.IsClass).ToArray();
    //now I have all classes which implements one of my interfaces
    foreach(Type type in types)
    {
      // here I want to check if the current type is typeof(T)
      // (typeof(T) == type) ->  doesn't work
      // (type.GetGenericTypeDefinition() == type) doesnt work

    }
  }
  return default(T);
}

如何正确地将给定的typeof(T)与类型数组中的类型进行比较?

--更新:

DummyServiceProvider的用法如下所示:

IDummyDerived<string> dummyDerived = myDummyServiceProvider.GetDummy<IDummyDerived<string>>()

EN

回答 2

Stack Overflow用户

发布于 2018-05-29 19:54:06

一个更简单的解决方案是重写where,如下所示

代码语言:javascript
复制
string nameOfIDummy = typeof(IDummy).Name;
string nameOfT = typeof(T).Name;
IEnumerable<Type> classTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(p => p.IsClass && p.GetInterface(nameOfIDummy) != null && p.GetInterface(nameOfT ) != null);

也许你可以写你需要的东西,我不明白用例,对我来说,这似乎是一件奇怪的事情。

票数 0
EN

Stack Overflow用户

发布于 2018-05-30 17:40:30

您正在使用derived type检查base generic interface type之间的等价性,这将始终为false:

请参阅以下代码

代码语言:javascript
复制
using System;
using System.Reflection;
using System.Linq;

namespace AssemblyLoadRefelectionExp
{
    public class MyClass
    {
        public string Key { get; set; }
    }

    public interface IDummy { }

    public interface IDummyDerived<T> : IDummy
    {
        void Foo(T value);
    }

    public interface ITempDerived<T> : IDummy
    {
        void HelloWorld(T value);
    }

    public class DummyDerived<T> : IDummyDerived<T>
    {
        public void Foo(T value)
        {
            Console.WriteLine("DummyDerived`1 ->Foo");
        }
    }

    public class AnotherDummy : IDummyDerived<string>
    {
        public void Foo(string value)
        {
            Console.WriteLine("AnotherDummy -> Foo");
        }
    }

    public class Program
    {
        public static void Main()
        {
            Console.WriteLine($"Is {typeof(DummyDerived<MyClass>)} equal to {typeof(IDummyDerived<MyClass>)} : {CheckType<DummyDerived<MyClass>>(typeof(IDummyDerived<MyClass>))}");
            Console.WriteLine("---------------------------------");
            GetDummy<IDummyDerived<string>>();

            Console.ReadLine();
        }

        public static bool CheckType<T>(Type type)
        {
            return typeof(T) == type ? true : false;
        }

        public static T GetDummy<T>() where T : IDummy
        {
            Type baseType = typeof(IDummy);
            Type[] types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()).Where(p => baseType.IsAssignableFrom(p) && p.IsClass).ToArray();
            //now I have all classes which implements one of my interfaces
            foreach (Type type in types)
            {
                Console.WriteLine($"Is {type.Name} assignable from {typeof(T)} : {typeof(T).IsAssignableFrom(type)}");
            }

            return default(T);
        }
    }
}

它将输出结果生成为

Is AssemblyLoadRefelectionExp.DummyDerived1[AssemblyLoadRefelectionExp.MyClass] equal to AssemblyLoadRefelectionExp.IDummyDerived1AssemblyLoadRefelectionExp.MyClass : False`

Is DummyDerived1 assignable from AssemblyLoadRefelectionExp.IDummyDerived1System.String : False

是否可以从AssemblyLoadRefelectionExp.IDummyDerived`1System.String赋值AnotherDummy : True

如果您签入上面的代码,为了检查具有派生类型的基类型,我在这里使用了IsAssignableFrom方法,因为GetDummy方法是用base generic interface type调用的。IsAssignableFrom验证是否可以将指定类型的实例分配给当前类型的实例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50583390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档