我使用工厂模式创建了若干方法例如public ClassFactory
{
public ReturnClass Create(ClassA a) {....}
public ReturnClass Create(ClassB b) {....}
}我只有在运行时才能得到ClassA或者是ClassB的类名的字符串形式,现在我有如下代码ClassFactory cf = new ClassFactory();
string paramName = GetParamName(); //paramName 就是“ClassA”或者“ClassB”//.....这段不会写啊 
//得到ClassA或ClassB的实例aReturnClass rc = cf.Create(a);

解决方案 »

  1.   

    http://hi.baidu.com/%B3%BE%CA%C0%D2%BB%E5%D0%D2%A3/blog/item/fda82339878bfa3d97ddd8c0.html
      

  2.   

    object result = null;
    Type type = Type.GetType(className);
    result = Activator.CreateInstance(type);
      

  3.   


    /// <summary>
        /// This class is implemented following the Abstract Factory pattern to create the BLL implementation
        /// specified from the configuration file
        /// </summary>
        public sealed class DataAccess
        {        // Look up the BLL implementation we should be using
            private static readonly string path = "*****.BLL";
            private static readonly Assembly bllAssembly = null;        static DataAccess()
            {
                bllAssembly = Assembly.Load(path);
            }        private DataAccess() { }        /// <summary>
            /// 创建业务逻辑对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            public static T GetBll<T>()
            {
                Type type = typeof(T);
                return (T)bllAssembly.CreateInstance(type.Namespace.Replace("IBLL", "BLL") + "." + type.Name.Substring(1));
            }
        }