关于C#泛型及反射调用的问题?
我现在有三个文件
Test.exe
{
FrmMain //类
}
Core.dll
{
ITestModule<IContract,Implement> //接口
}
TestModule.dll.
{
ITestService //类
TestService:ITestService //类
ModuleA:ITestModule<ITestService,TestService> //继承接口ITestModule<IContract,Implement>
}
我想在Frmain类中调用一个StartWCF<IContract,Implement>()的方法,
请问我在不知道TestModule.dll的情况下,是否可以用反射将ITestService,和TestService传给StartWCF方法,谢谢了。这里TestModule.dll的存放位置和ModuleA的类名可以通过配置文件告诉FrmMain.
下边是我画的UML类图。
http://hi.csdn.net/space-2425583-do-album-picid-1006718.html
[解决办法]
假设TestModel在执行目录
foreach (string file in Directory.GetFiles(Environment.CurrentDirectory,"*.dll")) { try { foreach (Type type in Assembly.LoadFile(file).GetTypes()) { Type[] interfaces = type.GetInterfaces(); bool find = false; foreach (var parent in interfaces) { if(!parent.IsGenericType)continue; var definition = parent.GetGenericTypeDefinition(); if (definition == typeof (ITestModule<,>)) { MessageBox.Show(type.GetMethod("ToString").Invoke(Activator.CreateInstance(type), Type.EmptyTypes).ToString()); find = true; break; } } if(find) break; } } catch (Exception) { } }
[解决办法]