C# 反射 从接口获取具体类的静态属性

本文关键字:静态 属性 反射 接口 获取 | 更新日期: 2023-09-27 17:57:22

我有一个接口:

interface IInterface 
{
    string Name { get; }
}

它由一个泛型抽象类实现:

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

这反过来又在具体类中实现:

public class CInterface : BInterface<int> 
{
}

我知道如何通过"类型"获取对具体类的引用。IsAssignableFrom', '!type.IsInterface"和"!type"。是抽象的",但这是我所管理的。

我需要通过反射获取任何具体类的静态 Name 属性的 VALUE 。 然而,对于我可怜的大脑来说,我无法想出实现这一目标的代码。 任何提示都会很棒。

编辑(澄清):

我知道静态属性需要从基类中读取。 但是....

静态字段将包含具体类的基名称 -->通过基类的静态构造函数中的反射派生的。 这很有效(我知道如何完成它),因为我们到处都在做。

在这种情况下,我正在尝试构建一个需要知道此静态字段的工厂类,并且由于工厂实现的某些(其他)要求,需要通过反射来访问它。

编辑(再次)扩展代码:

这是一个几乎完整的,如果无用的话,我试图完成的事情的例子。

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

Foreach 循环中 IntefaceFactory 的静态构造函数中的部分是我试图解决的问题。 希望这一点更清楚。

C# 反射 从接口获取具体类的静态属性

这是从实例中获取具体类的静态属性的方法:

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

static成员不会按照你的想法工作。它们属于基类,因此您正在尝试的内容无法使用static继承的成员。