我现在有三个文件
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

解决方案 »

  1.   

    假设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)
                    {
                    }
                }
      

  2.   

    StartWCF 在哪定义的?TestModule.dll的存放位置和ModuleA的类名可以通过配置文件告诉FrmMain
    ----------------------
    如果FrmMain中可以访问到相关位置的文件的话,那么是可以的。
      

  3.   

    http://topic.csdn.net/u/20111207/21/01ff43e0-f827-41e0-9612-ce65332c426f.html
      

  4.   

    ITestModule<IContract,Implement> 这里 IContract 定义没有意义。因为 ITestService 没有继承任何接口,这里第一个泛型无法进行任何约束。本身从 Implement 就可以提取 Interface 信息(当然需要考虑实现多个接口的情况,但如果是WCF完全可以提取是[ServiceContract]的接口信息)。所以有没有第一个泛型无所谓。
      

  5.   

    类似的问题我以前就纠结过,这是面向接口设计的典型问题解决方案是 控制反转(IOC) 也叫 依赖注入(DI)我想你是想把接口都声明在core中,而把实现放在TestModule中,而Test.exe则只是个载体从你的类库的物理角度来说,依赖关系必须是这样(引用关系)
    Test.exe -> core.dll <-----
           |                    \
            \--------------> TestModule
    而Test.exe对testModule的依赖是你不希望出现的。
                      
    这就是控制反转要解决的问题。
    实现方法是,通过Text.exe的配置文件来指定,Test.exe在运行时创建core.dll中的具体实现,当然用的是反射。而至于泛型参数,那必须是编译器确定的问题,不添加DLL的引用就无法确定具体类型
    可以考虑StartWCF(Type t1, Type t2)
      

  6.   

    而至于泛型参数,那必须是编译器确定的问题,不添加DLL的引用就无法确定具体类型
    可以考虑StartWCF(Type t1, Type t2)--------说的对, ITestModule<IContract,Implement> 如果是通过反射创建,无法类型化。
    泛型的作用也看不出来。