问个问题,我想反射出一个DLL 文件中的接口,但是我的DLL文件没有被程序引用进来,指定路径获得
 Assembly getDll = Assembly.LoadFrom(@"D:\项目\Ming\bin\Debug\Ming.dll");
Type[] typ = getDll.GetTypes();
Type type = Type.GetType(typ[0].ToString());
  Type[] typi= type.GetInterfaces();
现在最大的问题是获取不了类的类型Type type = Type.GetType(typ[0].ToString());这句老是报错,未初始化
能帮我解决下吗,谢谢了

解决方案 »

  1.   

    lz可以这么写,但是这有什么意义?typ[0]不就是type吗。对于GetType,最好使用AssemblyQualifiedName,这样才不会冲突。Assembly   getDll   =   Assembly.LoadFrom(@"D:\项目\Ming\bin\Debug\Ming.dll"); 
    Type[]   typ   =   getDll.GetTypes(); 
    if(typ.Length > 0)
    {
      Type type = Type.GetType(typ[0].AssemblyQualifiedName); 
      Type[]   typi=   type.GetInterfaces(); 
    }
      

  2.   

    也许是我没说清楚。我现在是没把那个DLL引用进来,只是通过路径得到的。我怎么才能写一段相当与引用的的代码。我把DLL引用进来,谢谢了。
    如果分不够我还会在加的。会另开贴加分的。谢谢了
    高手出现啊。谢谢了。
      

  3.   

    写了个简单demo,lz参照一下吧,比较简单的,呵呵,效果就是不reference winform dll显示一个窗体:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Reflection;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // This is a Console program, when build, no required reference winform dll manual, but can show form as well.
                Assembly asm = null;
                try
                {
                    asm = Assembly.LoadWithPartialName("System.Windows.Forms");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error load assembly: \r\n" + ex.ToString());
                    return;
                }            if (asm == null)
                {
                    Console.WriteLine("No assembly loaded!");
                    return;
                }            Type formType = asm.GetType("System.Windows.Forms.Form");
                if (formType == null)
                {
                    Console.WriteLine("Cannot get form type.");
                    return;
                }            object form1 = Activator.CreateInstance(formType);
                if (form1 == null)
                {
                    Console.WriteLine("Cannot create form instance.");
                    return;
                }            MethodInfo showFormMethodEntry = formType.GetMethod("ShowDialog",  new Type[0]);
                if (showFormMethodEntry == null)
                {
                    Console.WriteLine("Cannot get Form.ShowDialog method entry.");
                    return;
                }            showFormMethodEntry.Invoke(form1, new object[0]);            Console.WriteLine("Demo Successed!");
            }
        }
    }
      

  4.   

    首先将dll加入到当前AppDomain里面(AppendPrivatePath) 然后使用你的代码来取得类型。Type[]   typ   =   getDll.GetTypes();
    Type   type   =   Type.GetType(typ[0].ToString());
        Type[]   typi=   type.GetInterfaces(); 
    ==================
    如果typ这个Array的Length是0或者typ为null,那么使用typ[0].ToString()就会抛出异常了。
      

  5.   

    首先将dll加入到当前AppDomain里面(AppendPrivatePath)   
    ---------------------------
    想问下这样做和Assembly   getDll   =   Assembly.LoadFrom(@"D:\项目\Ming\bin\Debug\Ming.dll"); 
    的区别是什么,现在我对这两个都比较模糊,都是添加引用吗,还是。。