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.

2
  • 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... Commented Sep 21, 2016 at 9:59
  • Looking into DI & IoC (something like Autofac would do). Commented Sep 21, 2016 at 10:13

2 Answers 2

3

I solved my problem with:

SomeClass instance = ServiceLocator.Current.GetInstance<SomeClass>();
Sign up to request clarification or add additional context in comments.

Comments

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;
    }
}

7 Comments

i dont want to use static fields there. I read about: System.Activator.CreateInstance(type) but thats returns new instance. I need "Active" instance
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.
So it is impossible if i only know type?
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.
SomeClass instance = ServiceLocator.Current.GetInstance<SomeClass>(); You think im doing it right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.