问题描述
我有一个关于Singleton模式和MEF的问题。我在MEF实施新的插件,我还没有找到答案。
I have a question about the Singleton Pattern and MEF. I'm new in implementing MEF Plugins and I haven't found an answer.
是否有可能通过MEF实现插件只提供一个类的实例?
Is it possible to provide only one instance of a class through a MEF implemented Plugin?
我的老班是这样的:
#region Singleton
///
/// This class provide a generic and thread-safe interface for Singleton classes.
///
/// The specialized singleton which is derived
/// from SingletonBase<T>
public abstract class Base where T : Base
{
/* the lock object */
private static object _lock = new object();
/* the static instance */
private static T _instance = null;
///
/// Get the unique instance of .
/// This property is thread-safe!
///
public static T Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
/* Create a object without to use new (where you need a public ctor) */
object obj = FormatterServices.GetUninitializedObject(typeof(T));
if (obj != null) // just 4 safety, but i think obj == null shouldn't be possible
{
/* an extra test of the correct type is redundant,
* because we have an uninitialised object of type == typeof(T) */
_instance = obj as T;
_instance.Init(); // now the singleton will be initialized
}
}
}
}
else
{
_instance.Refresh(); // has only effect if overridden in sub class
}
return _instance;
}
}
///
/// Called while instantiation of singleton sub-class.
/// This could be used to set some default stuff in the singleton.
///
protected virtual void Init()
{ }
///
/// If overridden this will called on every request of the Instance but
/// the instance was already created. Refresh will not called during
/// the first instantiation, for this will call Init.
///
protected virtual void Refresh()
{ }
}
#endregion
#region class
public class xy : Base
{
private bool run;
public xy()
{
this.run = false;
}
public bool isRunning()
{
return this.run;
}
public void start()
{
// Do some stuff
this.run = true;
}
}
#endregion
有人可以提供我一个例如?
Can someone provide me an example?
推荐答案
是的,它是可以这样做。
Yes it is possible to do so.
在默认情况下,MEF将始终返回一个类的同一个实例时,它填补你的进口。所以,在技术上,如果你希望它是一个单身,你不必做任何事情。这是MEF调用共享创建策略。
By default, MEF will always return the same instance of a class when it fill your imports. So technically you do not have to do anything if you want it to be a singleton. This is what MEF calls a Shared Creation Policy.
如果你不想让你的进口来自于同一个实例,则需要指定它,无论是在你的属性也同样:
If you do not want your imports to come from the same instance, you need to specify it, either in your attributes likewise:
[Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
public MyClass : IMyInterface
或者,您可以覆盖自己CompositionContainer中,这样会默认创建非共享的实例。
Or you can override your own CompositionContainer so that will create NonShared instances by default.
请注意,您还可以显式地指定要共享创建策略(单身):
Note that you can also explicitly specify that you want a Shared Creation Policy (singletons):
[Import(RequiredCreationPolicy = CreationPolicy.Shared)]
public MyClass : IMyInterface
{
public MyClass() { } // you can have a public ctor, no need to implement the singleton pattern
}
但它不是必需的,因为共享(单) ,已经是默认值
But it is not necessary as Shared (singleton) is already the default value.
下面是MEF的文档的链接:这也解释了我刚才讲的。您也可以通过搜索找到关于这一主题的博客:MEF创建策略
Here is a link to MEF's documentation : http://mef.codeplex.com/wikipage?title=Parts%20Lifetime which explains what I just talked about. You can also find blogs on the subject by searching for : "MEF Creation Policy".
这篇关于C#Singleton模式和MEF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!