using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;namespace AAAA
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "AAAA";
            string ClassName = path + "." + "MethodA";
            ITest t = (ITest)Assembly.Load(path).CreateInstance(ClassName);
            t.Run();
            Console.ReadKey();
        }
    }    public class MethodA : ITest
    {
        public void Run()
        {
            Console.WriteLine(this.GetType().ToString());
        }
    }    public class MethodB : ITest
    {
        public void Run()
        {
            Console.WriteLine(this.GetType().ToString());
        }
    }    public interface ITest
    {
        void Run();
    }
}Assemble name: AAAA   Namespace : AAAA以上可以正常执行,但是如果 Assemble name 和 Namespace 不一样 ,反射就会出错,不知道为什么?请高手们给答案,

解决方案 »

  1.   

    这里你的先理解 什么是程序集名 ... Assemble 使用的是程序集名 不是命名空间.
      

  2.   

    报错为 Object reference not set to an instance of an object,其实这个问题可以解决。就是将 Assemble name 和 Namespace 设置一样的,所有的类命名空间设置一样的,但是实际中不太可行,每个类都有不同的命名空间。这样划分增加class liarbry的可读性。。
      

  3.   

    高手你好。你说的意思是 。让我先了解什么是程序集吗? 我知道Assembly.Load 是程序集上面的例子如果是你 你怎么做啊?
      

  4.   

    个人喜欢使用 System.Activator.CreateInstance 这个方法.   Type _OjbectType =Type.GetType(ClassFullName);
                ITest _Object = (ITest)System.Activator.CreateInstance(_OjbectType);
      

  5.   

    一个程序集下包含多个命名空间.  Assemble name: AAAA  Namespace : AAAA   Class  MethodA 
                         Namespace : BBBB   Class  MethodA你获取到BBBB的Type 问题就解决了.按你的方法..
      ITest t = (ITest)Assembly.Load("AAAA").CreateInstance("BBBB.MethodA"); 
    注意子类不是.是 +
      

  6.   

    谢谢你。Type _OjbectType =Type.GetType(ClassFullName); 
                ITest _Object = (ITest)System.Activator.CreateInstance(_OjbectType); 这样做有什么优点吗?