为了账号安全,请及时绑定邮箱和手机立即绑定

反射抽象工厂

反射抽象工厂

慕斯王 2018-12-06 20:44:59
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();        }    }为什么反射不出来,没有实例化,怎么解决
查看完整描述

3 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

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);

查看完整回答
反对 回复 2019-01-21
?
慕森王

TA贡献1777条经验 获得超3个赞

你的AssemblyName有没有错啊?

查看完整回答
反对 回复 2019-01-21
?
jeck猫

TA贡献1909条经验 获得超7个赞

恩,你点到了
查看完整回答
反对 回复 2019-01-21
  • 3 回答
  • 0 关注
  • 374 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信