首页 > 编程语言 >C# IOC注入示例

C# IOC注入示例

时间:2023-12-25 21:04:59浏览次数:25  
标签:Console Show C# void 示例 System WriteLine using IOC



文章目录

  • 主函数
  • `常规注入`
  • `属性注入`
  • `方法注入`


主函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace IOCTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            //container.RegisterType<Interface1,Class1>();
            container.RegisterType<Interface5, Class5>();
            //Class1 class1 = container.Resolve<Class1>();
            //Class2 class2 = container.Resolve<Class2>();
            //Class3 class3 = container.Resolve<Class3>();
            //Class4 class4 = container.Resolve<Class4>();

            Class5 class5 = container.Resolve<Class5>();

            Console.ReadKey();
        }
    }
}
常规注入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTest
{
    internal interface Interface1
    {
        void Show();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTest
{
    internal class Class1 : Interface1
    {
        public Class1()
        {
            Console.WriteLine($"{this.GetType().Name}被构造");
        }

        public void Show()
        {
            Console.WriteLine($"{this.GetType().Name}--Show");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTest
{
    internal interface Interface2
    {
        void Show();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTest
{
    internal class Class2 : Interface2
    {
        private Class1 _class1 = null;
        public Class2(Class1 class1)
        {
            _class1= class1;
            Console.WriteLine($"{this.GetType().Name}被构造");
        }

        public void Show()
        {
            Console.WriteLine($"{this.GetType().Name}--Show");
        }
    }
}
属性注入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTest
{
    internal interface Interface4
    {
        void Show();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace IOCTest
{
    internal class Class4
    {
        [Dependency]
        public Class3 _class3 { get; set; }

        public Class4()
        {
            //Console.WriteLine($"{_class3.GetType().Name}被构造");
            Console.WriteLine($"{this.GetType().Name}被构造");
        }

        public void Show()
        {
            Console.WriteLine($"{this.GetType().Name}--Show");
        }
    }
}
方法注入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IOCTest
{
    internal interface Interface5
    {
        void Show();

       // void Init(Class4 class4);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace IOCTest
{
    internal class Class5:Interface5
    {

        private Class4 _class4 { get; set; }

        public Class5()
        {
            //Console.WriteLine($"{_class3.GetType().Name}被构造");
            Console.WriteLine($"{this.GetType().Name}被构造");
           
        }

        [InjectionMethod]
        public void Init(Class4 class4)
        {
            _class4 = class4;
        }

        public void Show()
        {
            Console.WriteLine($"{this.GetType().Name}--Show");
        }
    }
}


标签:Console,Show,C#,void,示例,System,WriteLine,using,IOC
From: https://blog.51cto.com/u_14910409/8972143

相关文章

  • 关于NPOI word 异常解决NPOI.OpenXml4Net.Exceptions.OpenXML4NetRuntimeException:“
    NPOI.OpenXml4Net.Exceptions.OpenXML4NetRuntimeException:“Failtosave:anerroroccurswhilesavingthepackage:Mustsupportwriting源代码stringfileContent=File.ReadAllText(filePath,Encoding.Default);Dictionary<string,string>dic=......
  • 数据库 Mysql 多表查询,left join联合两个sql示例
    SELECTt1.RowID,t1.UserID,t1.CreateDate,t1.BatchState,t2.InputDataCount,t1.QtyFROM(SELECT@curRow:=@curRow+1ASRowID,`UserID`,DATE_FORMAT(CreateDate,'%Y-%m-%d')ASCreateDate,......
  • C# 解决从其他地方迁移项目,引用中大多数包是感叹号的问题
    当在VisualStudio中复制别人的C#项目时,遇到许多包冒感叹号的问题通常是因为缺少相关的NuGet包或引用不正确导致的。这会在解决方案资源管理器中的引用下显示感叹号。解决办法如下:在VisualStudio中打开项目。在解决方案资源管理器中,找到项目中的引用文件夹。右击要修改......
  • JavaScript读写FM1208 CPU卡源码
    本示例使用的发卡器:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv=&quo......
  • JavaScript读写15693 ICod2 卡源码
    本示例使用设备:<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="......
  • windows使用rclone挂载alist为本地磁盘,设置开机自启
    前言实现在windows下将alist挂载为本地磁盘,并设置开机自启,使得重启后依然生效。教程下载软件Rclone:RclonedownloadsWinFsp:https://winfsp.dev/rel/配置Rclone假设Rclone所在目录是:D:\rclone-v1.64.0-windows-amd64,进入文件目录,在地址栏输入cmd输入rcloneconfig......
  • COAP协议
    COAP协议RFC7252:TheConstrainedApplicationProtocol(CoAP)https://www.rfc-editor.org/rfc/rfc72522.ConstrainedApplicationProtocolTheinteractionmodelofCoAPissimilartotheclient/servermodelofHTTP.However,machine-to-machineinter......
  • C++ filesystem 库使用
    一、filesystem介绍filesystem源自boost.filesystem库,在C++17合并进C++标准库中,filesystem中的所有操作是线程不安全的。二、路径相关操作在filesystem库中提供path类来对路径进行操作,后续的相关操作,如打开文件、遍历目录、判断文件类型等,都是需要用path作为参数来指定操作具......
  • Thunderbolt 3 PCIe Expansion 扩展卡
    计算机目前大部分都能够提供Thunderbolt3接口了。Thunderbolt3的传输速度更快,所以我们需要把Thunderbolt3转换为SASHBA,但市场上没有这个转换设备。后来我们发现有Thunderbolt3PCIeExpansion,就是通过这个设备把Thunderbolt3转换为PCIe卡槽,然后再插上SASHBA卡,......
  • 『LeetCode』9. 回文数 Palindrome Number
    题目描述给你一个整数x,如果x是一个回文整数,返回true;否则,返回false。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。例如,121是回文,而123不是。示例1:输入:x=121输出:true示例2:输入:x=-121输出:false解释:从左向右读,为-121。从右向左读,为121-。因此......