1

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.

P10trek
  • 167
  • 4
  • 15
  • Can you elaborate on why you would want to do that? Depending on your needs, there may be better solutions than to try find all instances of a given class via reflection. You can, for example, use the service locator pattern for creating / managing instances of a specific type... – MBender Sep 21 '16 at 09:59
  • Possible duplicate of [How do I get all instances of all loaded types that implement a given interface?](http://stackoverflow.com/questions/302542/how-do-i-get-all-instances-of-all-loaded-types-that-implement-a-given-interface) – Good Night Nerd Pride Sep 21 '16 at 10:12
  • Looking into DI & IoC (something like Autofac would do). – webnoob Sep 21 '16 at 10:13

2 Answers2

3

I solved my problem with:

SomeClass instance = ServiceLocator.Current.GetInstance<SomeClass>();
P10trek
  • 167
  • 4
  • 15
2

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;
    }
}
Steven Yates
  • 2,400
  • 3
  • 30
  • 58
  • 1
    i dont want to use static fields there. I read about: System.Activator.CreateInstance(type) but thats returns new instance. I need "Active" instance – P10trek Sep 21 '16 at 10:08
  • 1
    System.Activator.CreateInstance(type) will do exactly what it says, create a new instance of the given type, as you stated. To find the "Active" instance as you put it, you'll need to store it somewhere and know where it is stored. Using reflection at this point isn't going to help you searching for an already existing instance of an object. If you only need one instance of it at a time, using the singleton class above and extend it with some methods for setting the current instance. I've added something you could use, however this is no longer a singleton. – Steven Yates Sep 21 '16 at 10:11
  • So it is impossible if i only know type? – P10trek Sep 21 '16 at 10:15
  • Unless I don't understand your question correctly, what you are looking to do is search the application memory for the instance of an object given a specific type, to my knowledge this is not possible and even if it was, i doubt it would be very efficient. – Steven Yates Sep 21 '16 at 10:18
  • Not to my knowledge, you could use dependency injection which still isn't magic. DI keeps track of the instances of the objects it creates, basically doing what I've suggested above. – Steven Yates Sep 21 '16 at 10:25
  • 1
    SomeClass instance = ServiceLocator.Current.GetInstance(); You think im doing it right? – P10trek Sep 21 '16 at 10:49
  • Although ServiceLocator isn't quite DI, it's similar. – Steven Yates Sep 21 '16 at 10:51