Microsoft.Practices.Unity
//
// Summary:
// Register a type mapping with the container.
//
// Parameters:
// container:
// Container to configure.
//
// injectionMembers:
// Injection configuration objects.
//
// Type parameters:
// TFrom:
// System.Type that will be requested.
//
// TTo:
// System.Type that will actually be returned.
//
// Returns:
// The Microsoft.Practices.Unity.UnityContainer object that this method was called
// on (this in C#, Me in Visual Basic).
//
// Remarks:
// This method is used to tell the container that when asked for type TFrom, actually
// return an instance of type TTo. This is very useful for getting instances of
// interfaces.
// This overload registers a default mapping and transient lifetime.
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "As designed")]
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, params InjectionMember[] injectionMembers) where TTo : TFrom;
class Program
{
static UnityContainer container = new UnityContainer();
static void Init()
{
container.RegisterType<IPlayer, Player>();
container.RegisterType<IMediaFile, MediaFile>();
}
static void Main(string[] args)
{
Init();
OperationMain op1 = container.Resolve<OperationMain>();
op1.PlayMedia();
Console.ReadLine();
}
}
//媒体文件
public interface IMediaFile
{
string FilePath { get; set; }
}
//默认媒体文件
public class MediaFile : IMediaFile
{
private string filePath;
public string FilePath
{
get
{
return filePath;
}
set
{
filePath = value;
}
}
}
//播放器
public interface IPlayer
{
void Play(IMediaFile file);
}
//默认播放器
public class Player : IPlayer
{
public void Play(IMediaFile file)
{
Console.WriteLine(file.FilePath);
}
}
public class OperationMain
{
IMediaFile _mtype;
IPlayer _player;
public OperationMain(IPlayer player,IMediaFile mtype)
{
_player = player;
_mtype = mtype;
}
public void PlayMedia()
{
_player.Play(_mtype);
}
}
Microsoft.Practices.Unity的更多相关文章
- 利用Microsoft.Practices.Unity的拦截技术,实现.NET中的AOP
1.记住这个单词的意思:Interception(拦截) 2.首先说一下原理和背景 原理:所谓的AOP就是面向切面编程,这里不多说,百度搜索. 目的:个人认为是为了解耦,部分代码跟业务代码分离,业务代 ...
- 第九回 Microsoft.Practices.Unity.Interception实现基于数据集的缓存(针对六,七,八讲的具体概念和配置的解说)
返回目录 概念 Microsoft.Practices.Unity.Interception是一个拦截器,它隶属于Microsoft.Practices.Unity组成之中,主要完成AOP的功能,而实 ...
- Microsoft.Practices.Unity 给不同的对象注入不同的Logger
场景:我们做项目的时候常常会引用第三方日志框架来帮助我们记录日志,日志组件的用途主要是审计.跟踪.和调试.就说我最常用的日志组件log4net吧,这个在.NET同行当中应该算是用得非常多的一个日志组件 ...
- VS - Microsoft.Practices.Unity
PM> Install-Package Unity Web.config <configSections> <section name="unity" t ...
- WPF Microsoft.Practices.Unity 注入大法简单示例
最近新入职了公司,做WPF方向的项目,进来后看到这边大量运用了依赖注入来解耦,采用的是Microsoft.Practices.Unity. WPF的话,目前主要有两个技术来实现IOC,unity和ME ...
- 使用Microsoft.Practices.Unity 依赖注入
Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...
- 使用Microsoft.Practices.Unity 依赖注入 转载https://www.cnblogs.com/slardar1978/p/4205394.html
Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...
- 20190703_创建 unity 的配置节处理程序时出错: The type name or alias Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension
创建 unity 的配置节处理程序时出错: The type name or alias Microsoft.Practices.Unity.InterceptionExtension.Configu ...
- Microsoft.Practices.Unity入门
Unity是微软Patterns & Practices团队所开发的一个轻量级的,并且可扩展的依赖注入(Dependency Injection)容器,它支持常用的三种依赖注入方式:构造器注入 ...
随机推荐
- 终于我还是没忍住,用Python爬了一波女神
你学爬虫,最终不还是为了爬妹子 啥也不说,开始福利赠送~ 女神大会 不是知道有多少人知道“懂球帝”这个 APP(网站),又有多少人关注过它的一个栏目“女神大会”,在这里,没有足球,只有女神哦. 画风是 ...
- Threads(异步和多线程)
Task是.NET Framework4.5出现的,线程是基于线程池的,然后提供丰富的api,Thread方法很多很强大,但是太过强大,没有限制. DoSomethingLong方法如下: /// & ...
- python_正则表达式学习
re.match()函数: 函数语法: re.mathch ( pattern , string , flags = 0) 参数说明: pattem 匹配的正则表达式 string 要匹配的字符串 f ...
- spark的wordcount
在开发环境下实现第一个程序wordcount 1.下载和配置scala,注意不要下载2.13,在spark-core明确支持scala2.13前,使用2.12或者2.11比较好. https://ww ...
- Python类的定义、方法和属性使用
类用来描述具有相同的属性和方法的对象的集合.对于在类中定义的函数,称为方法.类变量不直接叫做类变量,称为属性. 1.类的定义 例子: class User(): pass 说明: (1)定义了一个类名 ...
- [N久以前发布的文章]php 获取yahoo股票csv数据并封闭成为接口输出json数据
思路 先从yahoo接口获取数据,再定义接口,转化成为json输出.只供卡通网(kt5.cn)使用 stock.php 接口处理代码 <?php header("Content-Typ ...
- SSDB数据库笔记
目录 环境 配置文件 启动服务器 客户端 SSDB:一个高性能的支持丰富数据结构的 NoSQL 数据库, 用于替代 Redis. 参考文献: SSDB官网 环境 win10 下 wsl 环境 ubun ...
- postman---postman简单介绍
有小伙伴们想要了解postman,今天它来了,不要问它到底有多强大,自古免费好用即是王道,它不仅仅是开发接口强大的调试工具,还是测试接口的居家必备,它不仅仅可以把需要调用的接口保存下来方便调用,还可以 ...
- 分布式系统ID的几种生成办法
前言 一般单机或者单数据库的项目可能规模比较小,适应的场景也比较有限,平台的访问量和业务量都较小,业务ID的生成方式比较原始但是够用,它并没有给这样的系统带来问题和瓶颈,所以这种情况下我们并没有对此给 ...
- Object的多种方法
Object.entries() Object.entries() 方法返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致(区别在于 for-i ...