三种用反射的方法获得instance实例

// 三种用反射的方法获得instance实例。

public static AbstractFactory GetFactoryInstance()
        {
            string factoryName = Constant.STR_FACTORYNAME.ToString();

            AbstractFactory instance;

            if (factoryName != "")
            //instance = (AbstractFactory)Assembly.Load("AbstractFactory").CreateInstance(factoryName);
            {
                //Type type = Type.GetType(factoryName);
                //instance = (AbstractFactory)Activator.CreateInstance(type);

               Type type = Type.GetType(factoryName);
               //ConstructorInfo constructor = type.GetConstructor(new Type[0]);
               ConstructorInfo constructor = type.GetConstructor(Type.EmptyTypes);
               instance = (AbstractFactory)constructor.Invoke(null);
            } 

           else
                instance = null;

            return instance;
        }

posted @ 2008-11-30 15:10  交叉小径的花园  阅读(384)  评论(0)    收藏  举报