问题:当我用Customer testAssembly = new Customer();反射Customer类信息时没有问题;
      当我把Customer类生成一个类库Test.dll,用Assembly testAssembly = Assembly.LoadFile("Test.dll");时,
      得到的method为空,这是为什么?      static void Main(string[] args)
        {
            //Assembly testAssembly = Assembly.LoadFile("Test.dll");            Customer testAssembly = new Customer();            Type t = testAssembly.GetType();            MethodInfo method = t.GetMethod("DoSomething");
            if (method != null)
            {
                Console.WriteLine("Abstract: {0}, Visible to Assembly: {1}, Constructor: {2}, Sealed: {3}",
                    method.IsAbstract,
                    method.IsAssembly,
                    method.IsConstructor,
                    method.IsFinal);
            }
        }
    }    class Customer
    {
        private int privData = 12;        public int PubData
        {
            get { return privData; }
            set { privData = value; }
        }        public void DoSomething()
        {
            int z = 21;
            string localString = "local";
        }
    }

解决方案 »

  1.   

    object testAssembly = Assembly.Load("Test").CreateInstance("Test.Customer");//Customer testAssembly = new Customer();Type t = testAssembly.GetType();MethodInfo method = t.GetMethod("DoSomething");
    if (method != null)
    {
    Console.WriteLine("Abstract: {0}, Visible to Assembly: {1}, Constructor: {2}, Sealed: {3}",
    method.IsAbstract,
    method.IsAssembly,
    method.IsConstructor,
    method.IsFinal);
      

  2.   

    Assembly.LoadFile("Test.dll");
    这里需要绝对路径
      

  3.   

    Customer testAssembly =(Customer)Assembly.Load("Test").CreateInstance("Test.Customer");
      

  4.   

    2楼的答案可以解决我的问题,请问2楼高手,我是否可以这样理解这个问题,即使反射一个dll文件,也要构建instance,有实例才可以?3楼,我把路径隐掉了,这不是我的问题,还是谢谢你。
      

  5.   

    用你的代碼沒有問題啊,大概問題出在DLL,或者找不到DLL