首页 新闻 搜索 专区 学院

反射抽象工厂

0
悬赏园豆:10 [已解决问题] 解决于 2009-12-06 13:47

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();
        }
    }
为什么反射不出来,没有实例化,怎么解决

dahaiwuliang的主页 dahaiwuliang | 初学一级 | 园豆:175
提问于:2009-12-04 23:54
< >
分享
最佳答案
1

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

收获园豆:5
woody.wu | 老鸟四级 |园豆:3621 | 2009-12-06 12:01
谢谢
dahaiwuliang | 园豆:175 (初学一级) | 2009-12-06 13:47
其他回答(1)
0

你的AssemblyName有没有错啊?

收获园豆:5
persialee | 园豆:3217 (老鸟四级) | 2009-12-05 00:07
恩,你点到了
支持(0) 反对(0) dahaiwuliang | 园豆:175 (初学一级) | 2009-12-06 13:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册