我现在有三个文件
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
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
{
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)
{
}
}
----------------------
如果FrmMain中可以访问到相关位置的文件的话,那么是可以的。
Test.exe -> core.dll <-----
| \
\--------------> TestModule
而Test.exe对testModule的依赖是你不希望出现的。
这就是控制反转要解决的问题。
实现方法是,通过Text.exe的配置文件来指定,Test.exe在运行时创建core.dll中的具体实现,当然用的是反射。而至于泛型参数,那必须是编译器确定的问题,不添加DLL的引用就无法确定具体类型
可以考虑StartWCF(Type t1, Type t2)
可以考虑StartWCF(Type t1, Type t2)--------说的对, ITestModule<IContract,Implement> 如果是通过反射创建,无法类型化。
泛型的作用也看不出来。