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

推荐订阅源

Jina AI
Jina AI
T
Threat Research - Cisco Blogs
量子位
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
F
Fortinet All Blogs
爱范儿
爱范儿
D
Docker
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
A
About on SuperTechFans
P
Proofpoint News Feed
博客园 - 司徒正美
Recent Announcements
Recent Announcements
I
InfoQ
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
有赞技术团队
有赞技术团队
Webroot Blog
Webroot Blog
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
SecWiki News
SecWiki News
Attack and Defense Labs
Attack and Defense Labs
Hacker News: Ask HN
Hacker News: Ask HN
AI
AI
博客园_首页
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
T
Tenable Blog
宝玉的分享
宝玉的分享
Google Online Security Blog
Google Online Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
C
Check Point Blog
S
Security Affairs
L
LINUX DO - 最新话题
大猫的无限游戏
大猫的无限游戏
Scott Helme
Scott Helme
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
月光博客
月光博客
Y
Y Combinator Blog

博客园 - 风清云淡

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