简体   繁体   中英

C# Reflection Getting static property of concrete class from Interface

I have an interface:

interface IInterface 
{
    string Name { get; }
}

that is implemented by an generic 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 is in turn implemented in a concrete class:

public class CInterface : BInterface<int> 
{
}

I know how to get the references to the concrete classes via 'type.IsAssignableFrom', '.type.IsInterface' and ',type.IsAbstract', but that is as far as I have managed.

I need to get, via Reflection, the VALUE of the static Name property for any of the concrete classes. However, for the life of my poor brain, I cannot figure the code to accomplish this. Any hints would be great.

EDIT (In clarification):

I am aware that the static property needs to read from the base class. However....

The static field will contain the base name of the concrete class --> derived via reflection in the static constructor of the base class. This works (and I know how to accomplish it) as we do it all over the place.

I this case, I am attempting to build a factory class that needs to know this static field, and needs to get to it via Reflection due to the some (other) requirements of the factory implementation.

EDIT (again) Expanded code:

Here is a nearly complete, if useless, example of what I am attempting to accomplish.

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

The part in the static constructor of the IntefaceFactory inside the foreach loop is what I am attempting to solve. Hopefully, this is clearer.

This is how to get static property of concrete class from the instance:

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

static members don't work the way you are thinking. They belong to the base class and thus what you are attempting is not possible with a static inherited member.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM