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

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
The Exploit Database - CXSecurity.com
WordPress大学
WordPress大学
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 聂微东
J
Java Code Geeks
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Engineering at Meta
Engineering at Meta
M
MIT News - Artificial intelligence
A
About on SuperTechFans
Simon Willison's Weblog
Simon Willison's Weblog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Threatpost
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
S
Securelist
I
InfoQ
N
News and Events Feed by Topic
I
Intezer
A
Arctic Wolf
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
S
Secure Thoughts
P
Privacy & Cybersecurity Law Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
SecWiki News
SecWiki News
P
Palo Alto Networks Blog
MongoDB | Blog
MongoDB | Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
H
Help Net Security
B
Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V2EX - 技术
V2EX - 技术
S
SegmentFault 最新的问题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
人人都是产品经理
人人都是产品经理
PCI Perspectives
PCI Perspectives
F
Fortinet All Blogs

博客园 - 风清云淡

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