1

I have a static class like this:

public static class MyApp
{
    public static volatile MultiThreadLogger Logger = new MultiThreadLogger();
}

And a string like this:

"MyApp.Logger"

How can I get the object reference just with knowing the string? The string can be different like "MyOtherNamespace.Subnamespace.StaticObjA.MemberIwantToAccess" and I don't want to create a new instance, I want to access the static instance - just by the string.

Possible?

Marc
  • 6,749
  • 9
  • 47
  • 78
  • Did you see this one? http://stackoverflow.com/questions/1433714/c-sharp-reflection-is-it-possible-to-find-an-instance-of-an-object-at-runtime?rq=1 – slawekwin Aug 31 '12 at 06:43

3 Answers3

3
Type t = Type.GetType("MyApp");
PropertyInfo p = t.GetProperty("Logger", Reflection.BindingFlags.Public | Reflection.BindingFlags.Static | Reflection.BindingFlags.FlattenHierarchy);
return p.GetValue(null, null);

http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx

How to get a Static property with Reflection

Community
  • 1
  • 1
Jude Fisher
  • 11,138
  • 7
  • 48
  • 91
  • 1
    You may want to remove `BindingFlags.Instance`, especially since you unconditionally call `GetValue` with the first argument as null. –  Aug 31 '12 at 06:49
  • @hvd Yes, you're right. Doing this from (fuzzy) memory. Thanks. – Jude Fisher Aug 31 '12 at 06:51
2
public class ClassFactory
{
    static Dictionary<string, object> _Instances;

    public static object Get(string type)
    {
        lock (_Instances)
        {
            object inst;
            if (_Instances.TryGetValue(type, out inst)) return inst;

            inst = Activator.CreateInstance(Type.GetType(type));
            _Instances.Add(type, inst);

            return inst;
        }
    }
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • @Marc but what's the goal to get an instance of a static class ? Even if L.B method is perfectly fine, you still need reflection to access "instance"'s methods / properties... So a basic `Type.GetType()` would be enough, no ? Do you think I miss something ? Logging the "instanciated" static classes ? – Raphaël Althaus Aug 31 '12 at 06:41
  • The qoal is that I create a User Control which you can put into a page and configure the object you want to diplay like this `` and on server page I have to access the object by the string. – Marc Aug 31 '12 at 06:45
0

Are you looking for a particular class? In that case, you can add an attribute then iterate the defined classes in an assemlby using reflection, MEF or something similar until you find the right class.

If you are looking for an object (an instance of the class), you will need to "register" it in a factory of some kind, possibly a static Dictionary, then load it from there.

A better solution is to consider IoC containers (http://en.wikipedia.org/wiki/Inversion_of_control). IoC containers will handle the lifetime management and return different versions for testing or different parts of the app. A good explanation of them can be found at http://weblogs.asp.net/sfeldman/archive/2008/02/14/understanding-ioc-container.aspx and a list of then at http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx.

akton
  • 14,148
  • 3
  • 43
  • 47