Singleton with Activator.CreateInstance - c#

I have a class which implements the Singleton design pattern. However, whenever i try to get an instance of that class, using Activator.CreateInstance(MySingletonType) only the private constructor is called. Is there any way to invoke other method than the private constructor?
My class is defined as follow:
public class MySingletonClass{
private static volatile MySingletonClassinstance;
private static object syncRoot = new object();
private MySingletonClass()
{
//activator.createInstance() comes here each intantiation.
}
public static MySingletonClassInstance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
}
And the instantiation as follow:
Type assemblyType = Type.GetType(realType + ", " + assemblyName);
IService service = Activator.CreateInstance(assemblyType, true) as IService;

Activator.CreateInstance, except for one extreme edge-case, always creates a new instance. I suggest that you probably dont want to use Activator here.
However, if you have no choice, the hacky hack hack hack is to make a class that inherits from ContextBoundObject, and decorate it with a custom subclass of ProxyAttribute. In the custom ProxyAttribute subclass, override CreateInstance to do whatever you want. This is all kinds of evil. But it even works with new Foo().

Hei i do not know why are you creating an object of singleton class using reflection.
the basic purpose of singleton class is that it has only one object and has global access.
however you can access any of your method in singleton class like :
public class MySingletonClass {
private static volatile MySingletonClass instance;
private static object syncRoot = new object();
private MySingletonClass() { }
public static MySingletonClass MySingletonClassInstance {
get {
if (instance == null) {
lock (syncRoot) {
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
public void CallMySingleTonClassMethod() { }
}
public class program {
static void Main() {
//calling a
methodMySingletonClass.MySingletonClassInstance
.CallMySingleTonClassMethod();
}
}

Related

What can I do for a static class to be reloaded/reinstantiated?

An application I work with has a static class that loads some configuration from XML files in its constructor.
But when we make changes to one of these XML's, this class is not reloaded (as it should be, since it is static).
What can I do for this static class to be instantiated again, reloading the configuration?
Would I need to restart the IIS server? Are there some other ways?
Probably better to use a Singleton pattern with locks and with data invalidation
(typed in here so sorry in any syntax is wrong)
public class MySingleton
{
private static MySingleton _instance;
private static object _lock = new object();
private static MySingleton
{
// initialize here
}
public static MySingleton Instance
{
get
{
var singl = _instance;
if (singl != null)
return singl;
lock(_lock)
{
if (singl != null)
return singl;
_instance = new MySingleton();
return _instance;
}
}
}
public static void Invalidate()
{
lock(_lock)
{
_instance = null;
}
}
// -- your non-static methods
public bool CheckSomething(){ return true; }
}
use
// thread one
if (MySingleton.Instance.CheckSomething())
// my code
// thread two
MySingleton.Invalidate();

C# - Finding instance of Class

How can i find instance of class from another application layer. I have to refresh one propertie from DAL(data acces layer) using my MV(model view). What is simplest way to finish my task. Is this possible??
I mean something like:
SomeClass someClass = FindInstance<SomeClass>([params]);
thanks for help.
What I beleive you are attempting to do is create a singleton object. This is it in it's most simple form.
public class SomeClass
{
//single instance used everywhere.
private static SomeClass _instance;
//private constructor so only the GetInstance() method can create an instance of this object.
private SomeClass()
{
}
//get single instance
public static SomeClass GetInstance()
{
if (_instance != null) return _instance;
return _instance = new SomeClass();
}
}
Now to access the same instance of your object, you can just call
SomeClass singleton = SomeClass.GetInstance();
If you want to use more advanced techniques then you could consider using something like dependency injection, this however is a different discussion.
EDIT:
public class SomeClass
{
private static SomeClass _instance;
private SomeClass()
{
}
public static SomeClass GetInstance()
{
if (_instance == null)
throw new Exception("Call SetInstance() with a valid object");
return _instance;
}
public static void SetInstance(SomeClass obj)
{
if (obj == null)
throw new ArgumentNullException(nameof(obj));
_instance = obj;
}
}
I solved my problem with:
SomeClass instance = ServiceLocator.Current.GetInstance<SomeClass>();

How to easily simulate the not-thread-safeness of this Singleton pattern?

According to Jon Skeet's article, the following pattern is bad as it is not thread safe.
// Bad code! Do not use!
public sealed class Singleton
{
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
I have not learnt threading yet so it is a bit abstract to me. Could you give me a simple code to simulate the threading problem (we get notified when the problem occurs)?
Well thats pretty simple, just let something access a property within your singleton in parallel, for example like this console app.
class Program
{
static void Main(string[] args)
{
var threads = Enumerable.Repeat(new Action(() => Console.WriteLine(Singleton.Instance.guid)), 10);
Parallel.ForEach(threads, t => t());
Console.Read();
}
}
(I've added a guid property to your class to test that)
public sealed class Singleton
{
public Guid guid = Guid.NewGuid();
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
The issue with this singleton implementation is that 2 threads can access the getter simultaneously and each one will create a new instance. So the first thread might end up with a different instance than the second one... which can lead to unexpected behavior.
This is just in reply to OP comment:
static void Main(string[] args)
{
int test = 5;
Task<Singleton>[] arr =
{
Task<Singleton>.Factory.StartNew(() => Singleton.Instance),
Task<Singleton>.Factory.StartNew(() => Singleton.Instance),
};
Task.WaitAll(arr);
foreach (var item in arr)
{
Singleton s = item.Result;
s.MyProperty = test++;
Console.WriteLine(s.MyProperty);
}
}
MyProperty is just an int property i added.

Deallocate and re-instantiate new a singleton

I want to de-allocate the memory from the original singleton object and create a new one with another method.
public sealed class ObjectZ {
static readonly ObjectZ _instance = new ObjectZ();
private ObjectZ() {}
public static ObjectZ Instance{
get { return _instance; }
}
}
What would this method look like?
Singletons are usually created once and exist for the lifetime of the domain, recreating a singleton is dodgy business and by definition the code I've provided isn't truly a singleton.
The behaviour you seem to be after is a statically accessible single object cache that can be invalidated.
public static class SingletonAccessor
{
private static SomeClass _instance;
private static object _lock = new Object();
public static SomeClass Singleton
{
get
{
lock (_lock)
{
if (_instance == null)
{
_instance = new SomeClass();
}
return _instance;
}
}
}
public static void Recycle()
{
lock (_lock)
{
if (_instance != null)
{
// Do any cleanup, perhaps call .Dispose if it's needed
_instance = null;
}
}
}
}

Thread safety of C# singleton instance in ApplicationState

I have a bit of code that I've been trying to examine for thread safety. I'm using the basic lazy singleton model found here. I was wondering if it is still thread safe if I'm putting the instance in the HttpApplicationState object. I need to access this instance across all instances of the web application, so if this is not thread safe how can I make it thread safe?
public sealed class EmailWorker {
private HttpApplicationState _app;
private const EMAIL_WORKER = "EmailWorker";
EmailWorker() { }
class NestedWorker {
static NestedWorker() { }
internal static readonly EmailWorker Instance = new EmailWorker();
}
public static void Initialize(HttpApplicationState appState) {
_appState = appState;
_appState.Lock();
if (_appState[EMAIL_WORKER] == null) {
_appState.Add(EMAIL_WORKER, NestedWorker.Instance);
}
_appState.UnLock();
}
public static EmailWorker Instance {
get {
// TODO: If we haven't called Initialize() first then throw exception
return (EmailWorker)_appState[EMAIL_WORKER];
}
}
}
You don't need to use Application state at all.
It should be thread-safe, but why bother?
A "standard" singleton will also be accessible across the entire application, and it won't require injecting and keeping a reference to the HttpApplicationState:
public sealed class EmailWorker
{
private EmailWorker() { }
private static class NestedWorker
{
static NestedWorker() { }
internal static readonly EmailWorker Instance = new EmailWorker();
}
public static EmailWorker Instance
{
get { return NestedWorker.Instance; }
}
}

Categories

Resources