1.使用反射的好处?2.Assembly类怎么获取第三方dll中类的对象?

解决方案 »

  1.   

    1.没有好处,只有用处
    2.Assembly..::.GetTypes 方法可以获得类型
      

  2.   

    但是在GetTypes()方法中要提供 程序集名.类名 这个参数的,我又不知道类名怎么办
      

  3.   

    谢谢   知道怎么用了     GetType() 要提供参数
      

  4.   


    Assembly SampleAssembly;
    SampleAssembly = Assembly.LoadFrom("c:\\Sample.Assembly.dll");
    // Obtain a reference to a method known to exist in assembly.
    MethodInfo Method = SampleAssembly.GetTypes()[0].GetMethod("Method1");
    // Obtain a reference to the parameters collection of the MethodInfo instance.
    ParameterInfo[] Params = Method.GetParameters();
    // Display information about method parameters.
    // Param = sParam1
    //   Type = System.String
    //   Position = 0
    //   Optional=False
    foreach (ParameterInfo Param in Params)
    {
        Console.WriteLine("Param=" + Param.Name.ToString());
        Console.WriteLine("  Type=" + Param.ParameterType.ToString());
        Console.WriteLine("  Position=" + Param.Position.ToString());
        Console.WriteLine("  Optional=" + Param.IsOptional.ToString());
    }
      

  5.   

    Type t = absembly.GetTypes()[0];
    object ob = Activator.CreateInstance(t);如果下面我要用这个dll中的方法,我必须要将 ob 强制类型转换一下才可以用
    但我不知道这个类名是什么,怎么转换?(这个dll文件不是用C#写的)
      

  6.   

    不是用c#写的?是.Net的吗?
    assembly只能装在.Net的dll
    Activator.CreateInstance(t).Unwrap()
      

  7.   


    没有Unwrap()  方法
      

  8.   

    Unwrap是另一个Activator.CreateInstance重载的方法,我记错了。
    像你那个object ob = Activator.CreateInstance(t); 如果没有基类或者接口,就不能再转换了,只能通过反射Invoke
      

  9.   

    就是说这个呢,不能再转换了。
    得这样:
    MethodInfo mi=t.GetMothod(....)
    mi.GetParameters(...)
    mi.Invoke(....
    这样来调用