反射代码示例

namespace CLib
{
public interface ITest
    {
        void SysTest();
    }

 public class Test : ITest
    {
        public void SysTest()
        {
            System.Console.WriteLine("Hello Reflection");
        }
    }
}

class Program
    {
        static void Main(string[] args)
        {
            Assembly ass = System.Reflection.Assembly.LoadFrom("CLib.dll");
            Type type = ass.GetType("CLib.Test");
            Type type1 = System.Activator.CreateInstanceFrom("CLib.dll", "CLib.Test").GetType();
            Type type2 = System.Activator.CreateInstanceFrom("CLib.dll", "CLib.Test").Unwrap().GetType();

            Console.WriteLine(type.ToString());
            Console.WriteLine(type1.ToString());
            Console.WriteLine(type2.ToString());
            Console.WriteLine("=============================");
            
            CLib.ITest test = (CLib.ITest)System.Activator.CreateInstance("CLib", "CLib.Test").Unwrap();
            test.SysTest();

            Console.WriteLine("=============================");
            Console.ReadLine();
        }
    }

posted @ 2007-11-30 16:02  RobotTech  阅读(350)  评论(0编辑  收藏  举报