当前位置:网站首页>Autofac初学(1)
Autofac初学(1)
2022-06-12 14:18:00 【有诗亦有远方】
一、创建控制台项目
引入nuget包
二、创建接口
using System;
using System.Collections.Generic;
using System.Text;
namespace _02_autofac
{
internal interface Person
{
void Eat();
void Say();
}
}
三、创建实现类
using System;
using System.Collections.Generic;
using System.Text;
namespace _02_autofac
{
public class Student:Person
{
public void Eat()
{
Console.WriteLine("吃早饭");
}
public void Say()
{
Console.WriteLine("说早安");
}
}
}
四、创建实例
using Autofac;
using System;
using System.ComponentModel;
namespace _02_autofac
{
internal class Program
{
private static Autofac.IContainer Container {
get; set; }
static void Main(string[] args)
{
//创建容器
var builder = new ContainerBuilder();
//创建builder,并在builder中注册类型。
builder.RegisterType<Student>().As<Person>();
//实例化容器(可以讲第一步合并进来)
Container = builder.Build();
//在需要使用接口的地方,通过container来解析得到一个借口的实例。
Person person = Container.Resolve<Person>();
person.Eat();
person.Say();
}
}
}
五、测试结果

边栏推荐
- Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge
- 通信流量分析
- Crawler problem set (I) record
- Machine learning learning notes
- Can you believe it? It took me only two days to develop a management system
- Codeforces Round #798 (Div. 2)(A~D)
- C secret arts script Chapter 2 (detailed explanation of pointer) (Section 1)
- 【OCR】AspriseOCR C# 英文、數字識別(中文不行)
- PMP agile knowledge points
- Player practice 15 xdemux and avcodecparameters
猜你喜欢

Interview (XI) futu written test questions

Appnium (I) basic use of appnium

Two methods of implementing asynchronous calling function with QT

华为设备配置H虚拟专用网

Introduction to QT reflection mechanism and signal slot mechanism

Player actual combat 25 unpacking module add close

Player actual combat 21 audio and video synchronization

【活动早知道】LiveVideoStack近期活动一览

Des File Encryptor based on MFC framework

Recursive summary of learning function
随机推荐
Can you believe it? It took me only two days to develop a management system
【MySQL】数据库基本操作
Mémoire de l'examen d'entrée à l'université
Raspberry pie get temperature and send pictures to email
How to realize the bidding strategy that pays more attention to transformation in the company's operation Google sem
华为设备配置OSPF伪连接
Crack WinRAR to ad pop-up window
JD scanning code to obtain cookies
Basic usage of scanner
Three common methods of C language array initialization ({0}, memset, for loop assignment) and their principles
Redis core configuration and advanced data types
Socket model of punctual atom stm32f429 core board
C secret arts script Chapter 2 (detailed explanation of pointer) (Section 2)
Location (I) error: command erred out with exit status
QT link error: undefined reference to VTable for "XXX“
Common DOS commands
工业机械臂(机器人)视觉定位引导系统
Player actual combat 22 to solve the problems of flower screen and Caton
Player actual combat 13 create qtopengl project to promote window control and reload qoopenglwedge
Visual positioning guidance system for industrial manipulator (robot)