当前位置: 首页 > news >正文

合肥企业网站建设工wordpress 获取文章的标签

合肥企业网站建设工,wordpress 获取文章的标签,wordpress程序的主题,网站建设网站建设公司我们在第三篇中学习里一个简易的IoC框架。今天我们接着上次的程序#xff0c;实现带参数构造函数对象的实例和属性的注入 。  我们知道可以通过反射获取类的构造函数及参数(GetConstructors方法)#xff1b;可以获取属性和属性的类型(GetProperties方法)。通过Activator的C…  我们在第三篇中学习里一个简易的IoC框架。今天我们接着上次的程序实现带参数构造函数对象的实例和属性的注入 。  我们知道可以通过反射获取类的构造函数及参数(GetConstructors方法)可以获取属性和属性的类型(GetProperties方法)。通过Activator的CreateInstance(Type type, params object[] args)方法可以创建带参数构造函数的实例。通过SetValue方法可以给属性赋值这样一来我们就上次的代码稍加改造就可以实现属性的注入了。   下面是完成的代码 Domain    public class Person    {        public string Name { get; set; }        public int Age { get; set; }    }   public class PersonDao    {        private int intProp;        public PersonDao(int intProp)        {            this.intProp  intProp;        }        public Person Entity { get; set; }        public override string ToString()        {            return 构造函数参数intProp为  this.intProp;        }    }  ObjectFactorypublic class ObjectFactory    {        private IDictionarystring, object objectDefine  new Dictionarystring, object();        private ObjectFactory(string fileName)        {            InstanceObjects(fileName);  // 实例IoC容器            DiObjects(fileName);  // 属性注入        }        private static ObjectFactory instance;        private static object lockHelper  new object();        public static ObjectFactory Instance(string fileName)        {            if (instance  null)            {                lock (lockHelper)                {                    instance  instance ?? new ObjectFactory(fileName);                }            }            return instance;        }        /**//// summary        /// 实例IoC容器        /// /summary        /// param namefileName/param        private void InstanceObjects(string fileName)        {            XElement root  XElement.Load(fileName);            var objects  from obj in root.Elements(object)                          select obj;            //无参构造函数            objectDefine  objects.Where(obj                 obj.Element(constructor-arg)  null).ToDictionary(                    k  k.Attribute(id).Value,                     v                      {                        string typeName  v.Attribute(type).Value;                          Type type  Type.GetType(typeName);                          return Activator.CreateInstance(type);                    }                );            //有参构造函数            foreach (XElement item in objects.Where(obj                  obj.Element(constructor-arg) ! null))            {                                                                                                                                                                  string id  item.Attribute(id).Value;                string typeName  item.Attribute(type).Value;                Type type  Type.GetType(typeName);                var args  from property in type.GetConstructors()[0].GetParameters()                           join el in item.Elements(constructor-arg)                           on property.Name equals el.Attribute(name).Value                           select Convert.ChangeType(el.Attribute(value).Value,                           property.ParameterType);                object obj  Activator.CreateInstance(type, args.ToArray());                objectDefine.Add(id, obj);            }        }        /**//// summary        /// 属性注入        /// /summary        /// param namefileName/param        private void DiObjects(string fileName)        {            XElement root  XElement.Load(fileName);            var objects  from obj in root.Elements(object)                          select obj;            foreach (KeyValuePairstring,object item in objectDefine)            {                foreach (var el in objects.Where(e                      e.Attribute(id).Value  item.Key).Elements(property))                {                    Type type  item.Value.GetType();                    //获取属性                    foreach (PropertyInfo property in type.GetProperties())                    {                        if (property.Name  el.Attribute(name).Value)                        {                            if (el.Attribute(value) ! null)                            {                                //设置属性值                                property.SetValue(item.Value,                                     Convert.ChangeType(el.Attribute(value).Value,                                     property.PropertyType), null);                            }                            else if (el.Attribute(ref) ! null)                            {                                object refObject  null;                                if (objectDefine.ContainsKey(el.Attribute(ref).Value))                                {                                    refObject  objectDefine[el.Attribute(ref).Value];                                }                                //设置关联对象属性                                property.SetValue(item.Value, refObject, null);                            }                        }                    }                }            }        }        /**//// summary        /// 获取对象        /// /summary        /// param nameid/param        /// returns/returns        public object GetObject(string id)        {            object result  null;            if (objectDefine.ContainsKey(id))            {                result  objectDefine[id];            }            return result;        }    }   App.config?xml version1.0 encodingutf-8 ?objects  object idperson typeSpringNetMyDi.Person, SpringNetMyDi    !--属性值类型注入--    property nameName valueLiu Dong/    property nameAge value27/  /object  object idpersonDao typeSpringNetMyDi.PersonDao, SpringNetMyDi    !--构造器注入--    constructor-arg nameintProp value1/    property nameEntity refperson /      /object/objects   Program   class Program    {        static void Main(string[] args)        {            ObjectFactory factory  ObjectFactory.Instance(F:\Exercise\SpringNet\Step1\SpringNet_Lesson9\SpringNetMyDi\Objects.xml);            PersonDao dao  (PersonDao)factory.GetObject(personDao);            Console.WriteLine(姓名  dao.Entity.Name);            Console.WriteLine(年龄  dao.Entity.Age);            Console.WriteLine(dao);            Console.ReadLine();        }    }    输入结果          代码下载       返回目录转载于:https://www.cnblogs.com/GoodHelper/archive/2009/11/02/SpringNet_MyDi.html
http://www.pierceye.com/news/202040/

相关文章:

  • 关于对网站建设情况的通报东南亚跨境电商有哪些平台
  • 如需郑州网站建设长沙微网站制作
  • html格式网站网站搜索排名优化软件
  • 定制 网站天津做网站要多少钱
  • 免费做问卷的网站网站建设 好发信息网
  • 网站职能建设论文网页跳转链接怎么制作
  • 专题网站开发工具有哪些wordpress迁移500
  • 酒泉网站建设费用磁力搜索引擎不死鸟
  • 有网站是做水果原产地代发的吗临沂哪家做网站最好
  • 网站建设的背景wordpress htaccess
  • 如何在百度上注册自己的网站郓城做网站哪家好
  • 企业电子商务网站建设总结安徽建设工程信息网查询平台公司
  • 推荐常州微信网站建设做外汇看的网站
  • 网站页面优化方案网页设计培训费用多少
  • 落寞文学网单本多本小说wordpress主题哈尔滨网站优化指导
  • 网站域名使用费用网站在vps能访问 在本地访问不了
  • wordpress可以做成企业站吗一个域名可以建设几个网站
  • 企业网站备案需要什么资料工装装饰公司
  • 网站建设精美模板下载邢台165信息交友
  • 普陀区建设工程质检网站网站关键词优化排名
  • 云服务器多网站解析企业网站打不开了
  • 搭建flv视频网站如何注册域名步骤
  • 平面设计公司网站单页模板
  • 网站模板 代码免费建设部网站有项目经理资质查询系统
  • 网站建设注意哪些方面wordpress 做手机站
  • 淄博网站制作定制推广网站程序源代码
  • 二手房地产中介网站建设wordpress改背景图片
  • 可以自己做安卓app的网站Wordpress会员插件出错
  • 做网站优化有前景吗可信网站验证服务中心
  • 韩国设计欣赏网站天津工程建设网官方网站