反射抽象工厂

class User
    {
        private int _id;
        public int ID
        {
            get{return _id;}
            set{_id=value;}
        }
        private string _name;
        public string Name
        {
            get{return _name;}
            set{_name=value;}
        }
    }
    interface IUser
    {
        void Insert(User user);
        User GetUser(int id);
    }
    class SqlserverUser:IUser
    {
       
        public void Insert(User user)
        {
            Console.WriteLine("在SQL Server中根据ID得到User表增加1条记录");
        }
        public User GetUser(int id)
        {
            Console.WriteLine("在SQL Server中根据ID得到User表的1条记录");
            return null;
        }
    }
    class AccessUser : IUser
    {
        public void Insert(User user)
        {
            Console.WriteLine("在Access中给User表增加一条记录");
        }
        public User GetUser(int id)
        {
            Console.WriteLine("在Access中根据ID得到User表一条记录");
            return null;
        }
    }
    class DataAccess
    {
        private static readonly string AssemblyName = "File";
        private static readonly string db = "Sqlserver";
        public static IUser CreateUser()
        {
            string className = AssemblyName + "." + db + "User";
            return (IUser)Assembly.Load(AssemblyName).CreateInstance(className);
        }
    }
    class Test
    {
        static void Main()
        {
            User user = new User();
            IUser iu = DataAccess.CreateUser();
            iu.Insert(user);
            iu.GetUser(1);
            Console.Read();
        }
    }
为什么反射不出来,没有实例化,怎么解决

慕斯王
浏览 375回答 3
3回答

慕无忌1623718

namespace WOODY{  public   class User    {        private int _id;        public int ID        {            get { return _id; }            set { _id = value; }        }        private string _name;        public string Name        {            get { return _name; }            set { _name = value; }        }    }    public interface IUser    {        void Insert(User user);        User GetUser(int id);    }    public class SqlserverUser : IUser    {         public void Insert(User user)        {             HttpContext.Current.Response.Write("在SQL Server中根据ID得到User表增加1条记录");        }        public User GetUser(int id)        {            HttpContext.Current.Response.Write("在SQL Server中根据ID得到User表的1条记录");            return null;        }    }    public class AccessUser : IUser    {        public void Insert(User user)        {            HttpContext.Current.Response.Write("在Access中给User表增加一条记录");        }        public User GetUser(int id)        {            HttpContext.Current.Response.Write("在Access中根据ID得到User表一条记录");            return null;        }    }    public class DataAccess    {        private static readonly string AssemblyName = "WOODY";        private static readonly string db = "Sqlserver";         public   IUser CreateUser()        {            string className = AssemblyName + "." + db + "User";            string NAME = this.GetType().Assembly.FullName;            return (IUser)Assembly.Load(this.GetType().Assembly.FullName).CreateInstance(className);        }    } }     -----   User user = new User();        DataAccess DA = new DataAccess();        IUser iu = DA.CreateUser();        iu.Insert(user);        iu.GetUser(1);

慕森王

你的AssemblyName有没有错啊?

jeck猫

恩,你点到了
打开App,查看更多内容
随时随地看视频慕课网APP