Saturday, 15 February 2014

c# - Get the reference of a specific class object in memory -



c# - Get the reference of a specific class object in memory -

is there way retrieve reference instance of specific class?

i know name of class , know there 1 instance of in memory.

i need in order phone call method of instance, design reasons can not define method 'static'.

for illustration like:

public static void main() ... object temp= getcurrentinstanceof("classtouse"); temp.methodineed(); ... }

no, there's no way of doing that.

you have static variable in class, maintain reference "most created instance" available - prevent garbage collection, of course. or create class singleton - similar solution, allowing single instance ever created.

but ideally, you'd alter design create right instance available in different ways. dependency injection way forwards here, without more details it's impossible how pan out in situation.

anything relying on "global state" becomes pain testability. strongly urge reconsider overall design , info flow rather using statics global state.

c#

No comments:

Post a Comment