C # Reflection Getting the static property of a particular class from an interface

I have an interface:

interface IInterface 
{
    string Name { get; }
}

which is implemented by the general abstract class:

public class BInterface<T> : IInterface
{
    static BInterface() 
    { 
        // Or anything that would be implementation class specific
        Name = typeof(BInterface<>).GetType().Name;  
    }
    public static string Name { get; private set; }
    string IInterface.Name { get { return Name; } }
}

Which, in turn, is implemented in a specific class:

public class CInterface : BInterface<int> 
{
}

I know how to get references to specific classes through "type.IsAssignableFrom", "! Type.IsInterface" and "! Type.IsAbstract", but this is how much I managed.

I need to get the VALUE value of the static Name property for any of the specific classes through Reflection. However, for the life of my poor brain, I cannot understand the code to execute this. Any clues would be great.

EDIT (clarification):

I know that a static property needs to be read from the base class. However....

→ , . ( , ), .

factory, , Reflection - () factory.

EDIT () :

, , .

    public interface IInterface
    {
        string Name { get; }
        object Value { get; set; }
    }

    public class BInterface<T> : IInterface
    {
        static BInterface()
        {
            // Or anything that would be implementation class specific
            Name = typeof(BInterface<>).GetType().Name; // Should be CInterface, DInterface depending on which class it is called from.
        }

    string IInterface.Name { get { return Name; } }
    object IInterface.Value { get { return Value; } set { Value = (T)value; } }

    public static string Name { get; private set; }
    public T Value { get; set; }
}

public class CInterface : BInterface<int>
{
}

public class DInterface : BInterface<double>
{
}

public static class InterfaceFactory
{
    private readonly static IDictionary<string, Type> InterfaceClasses;

    static InterfaceFactory()
    {
        InterfaceClasses = new Dictionary<string, Type>();

        var assembly = Assembly.GetExecutingAssembly();
        var interfaceTypes = assembly.GetTypes()
                                     .Where( type => type.IsAssignableFrom(typeof (IInterface)) 
                                         && !type.IsInterface 
                                         && !type.IsAbstract);
        foreach (var type in interfaceTypes)
        {
            // Get name somehow
            var name = "...";
            InterfaceClasses.Add(name, type);
        }
    }

    public static IInterface Create(string key, object value, params object[] parameters)
    {
        if (InterfaceClasses.ContainsKey(key))
        {
            var instance = (IInterface) Activator.CreateInstance(InterfaceClasses[key], parameters);
            instance.Value = value;

            return instance;
        }
        return null;
    }
}

IntefaceFactory foreach - , . , .

+3
2

:

var staticProperty = instance.GetType()
    .GetProperty("<PropertyName>", BindingFlags.Public | BindingFlags.Static);
var value = staticProperty.GetValue(instance, null);
+3

static , . , , , static.

+1

All Articles