简体   繁体   English

C# 反射从接口获取混凝土 class 的 static 属性

[英]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:由通用抽象 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:这又在具体的 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.我知道如何通过 'type.IsAssignableFrom'、'.type.IsInterface' 和 ',type.IsAbstract' 获取对具体类的引用,但这是我所能做到的。

I need to get, via Reflection, the VALUE of the static Name property for any of the concrete classes.我需要通过反射获取任何具体类的 static Name 属性的值。 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.我知道 static 属性需要从基础 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. static 字段将包含具体 class 的基本名称 --> 通过 static 构造函数中的反射派生而来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.在这种情况下,我试图建立一个工厂 class 需要知道这个 static 字段,并且由于工厂实施的一些(其他)要求而需要通过反射来获得它。

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.我正在尝试解决 foreach 循环内的 IntefaceFactory 的 static 构造函数中的部分。 Hopefully, this is clearer.希望这更清楚。

This is how to get static property of concrete class from the instance:这是如何从实例中获取具体 class 的 static 属性:

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. static成员不会按照您的想法工作。 They belong to the base class and thus what you are attempting is not possible with a static inherited member.它们属于基础 class ,因此您尝试使用static继承的成员是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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