我正在尝试动态创建一个从泛型interface继承的类型的实例。
例如,我有以下base-interface,其中有几个其他接口派生自:
public interface IDummy { }我有两个派生接口:
public interface IDummyDerived<T> : IDummy
{
void Foo(T value);
}
public interface ITempDerived<T> : IDummy
{
void HelloWorld(T value);
}现在我需要一个ServiceProvider-Class,我可以在其中创建、查找实现给定接口的类。每个接口(IDummyDerived和ITempDerived)只实现一次。
我的方法是:
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>>()
发布于 2018-05-29 19:54:06
一个更简单的解决方案是重写where,如下所示
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);也许你可以写你需要的东西,我不明白用例,对我来说,这似乎是一件奇怪的事情。
发布于 2018-05-30 17:40:30
您正在使用derived type检查base generic interface type之间的等价性,这将始终为false:
请参阅以下代码
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.DummyDerived
1[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验证是否可以将指定类型的实例分配给当前类型的实例。
https://stackoverflow.com/questions/50583390
复制相似问题