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

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

博客园 - 风清云淡

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配置加载项