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?

1

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

You may want to remove BindingFlags.Instance, especially since you unconditionally call GetValue with the first argument as null.
@hvd Yes, you're right. Doing this from (fuzzy) memory. Thanks.
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;
        }
    }
}

2 Comments

@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(<your class name>) would be enough, no ? Do you think I miss something ? Logging the "instanciated" static classes ?
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 <ThreadActionLog:ActionReport ID="ControlId" runat="server" LogObject="MyApp.Logger" /> and on server page I have to access the object by the string.
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.

Comments

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.