惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
The Exploit Database - CXSecurity.com
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
D
DataBreaches.Net
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
Latest news
Latest news
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Threat Research - Cisco Blogs
G
GRAHAM CLULEY
G
Google Developers Blog
W
WeLiveSecurity
Project Zero
Project Zero
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
F
Full Disclosure
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 【当耐特】
Google DeepMind News
Google DeepMind News
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
小众软件
小众软件
N
News | PayPal Newsroom
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
AI
AI
T
Tenable Blog
S
Schneier on Security
O
OpenAI News
The Register - Security
The Register - Security
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
T
Threatpost
Hacker News: Ask HN
Hacker News: Ask HN

博客园 - 风清云淡

Extjs使用 RestfulWebApi +Token验证小结 Oracle 10g的备份与还原 如何通过反射动态调用泛型方法 Oracle与Sql Server写SQL的区别 Oracle 表与字段的注释操作 将MS SQL SERVER 数据库导入到ORACLE的坑 SQL SERVER 触发器的误区 Asp.net Core部署于CentOS上报404错误的坑 SqlBulkCopy 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 bit SQL SERVER OVER开窗函数,Partition By,ROW_NUMBER(),DENSE_RANK(),RANK()排名函数 CefSharp"Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies" 在同一个项中引用同一类库的多个版本 Visual Studio中Js使用智能感知 Func<T>,Action<T>,Predicate<T>使用小结 Nuget 在VS中操作命令 枚举的使用总结 IIS WEB程序如何访问共享目录 AngularJS之页面跳转Route ASP.NET MVC4 BundleConfig的注意事项
Unity的动态加载简单使用
风清云淡 · 2016-11-05 · via 博客园 - 风清云淡

Unity的动态加载简单使用
Unity可以快速,轻量化的实现IOC,不用自已写类似反射代码来动态加载类或dll了
使用Unity先要用nuget获取相关引用文件
Unity可通过代码或config文件来配置要加动态加载的内容

使用示例

        static void Main(string[] args)
        {
            Console.WriteLine("'Y' use Config Register Type,Otherwise use Code Register Type");
            bool isUseConfig = Console.ReadLine().ToUpper().Equals("Y");

            IUnityContainer container = new UnityContainer();//定义ioc容器

            if (isUseConfig)
            {
                //通过Config来注册Type
                UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
                configuration.Configure(container, "defaultContainer");//defaultContainer为在config中定义的IOC容器名称
            }
            else
            {
                //通过代码来注册Type
                CodeReg(container);
            }

            A a = container.Resolve<IA>() as A;//演示动态构造对像
            B b = container.Resolve<IB>() as B;

            var list = new ParameterOverrides();
            list.Add("name", "TomMao");//构造函数的参数名,参数值
            IE e = container.Resolve<IE>("e",list);//演示构造函数传参 构造对像
            e.ShowName();
            e = container.Resolve<IE>("ee", list);//演示通过ioc不同名字,用不同的class构造同一接口对像,ee,e即是不同的ioc名字,代表使用不同的class,在注册时加上ioc名字
            e.ShowName();

            if (null != a)
            {
                Console.WriteLine("a.B == null ? {0}", a.B == null ? "Yes" : "No");
                Console.WriteLine("a.C == null ? {0}", a.C == null ? "Yes" : "No");
                Console.WriteLine("a.D == null ? {0}", a.D == null ? "Yes" : "No");
            }
            Console.ReadLine();
        }

使用代码配置加载项

        //通过代码来实现Ioc容器的Type注册
        private static void CodeReg(IUnityContainer container)
        {
            container.RegisterType(typeof(IA), Type.GetType("UnityDemo.A"));
            container.RegisterType(typeof(IB), Type.GetType("UnityDemo.B"));
            container.RegisterType(typeof(IC), Type.GetType("UnityDemo.C"));
            container.RegisterType(typeof(ID), Type.GetType("UnityDemo.D"));
            container.RegisterType(typeof(IE), Type.GetType("UnityDemo.E"), "e");//加入注册的别名,可通过此名称实现同一接口,不同实现
            container.RegisterType(typeof(IE), Type.GetType("UnityDemo.EE"), "ee");
        }

使用Config配置加载项