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);
}
}
最新文章
- web.config配置
- [lintcode 14] First Position of Target
- NavBarControl
- 使用豆瓣的pypi源
- 【leetcode】Binary Search Tree Iterator
- 使用haproxy做负载均衡时保持客户端真实的IP
- xcode 4 安装cocos2d-x 2.1.4
- linux之utime函数解析
- Atitit.Gui控件and面板----数据库区-mssql 2008 权限 配置 报表查看成员
- python运维开发(二十二)---JSONP、瀑布流、组合搜索、多级评论、tornado框架简介
- WCF不支持 ASP.NET 兼容性 解决办法
- Kafka 源代码分析之LogManager
- C++STL之String
- SpriteBuilder中粒子发射器的reset on visibility toggle选项解释
- #Java学习之路——基础阶段二(第四篇)
- winform datagridview 导出excel
- 安装xgboost
- Puppet日常总结
- 正向选择(positive selection)、中性选择(neutral selection)、平衡选择(balancing selection)示意图
- Android开发工程师文集-1 小时学会各种Drawable