繁体   English   中英

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

[英]C# Reflection Getting static property of concrete class from Interface

我有一个界面:

interface IInterface 
{
    string Name { get; }
}

由通用抽象 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; } }
}

这又在具体的 class 中实现:

public class CInterface : BInterface<int> 
{
}

我知道如何通过 'type.IsAssignableFrom'、'.type.IsInterface' 和 ',type.IsAbstract' 获取对具体类的引用,但这是我所能做到的。

我需要通过反射获取任何具体类的 static Name 属性的值。 但是,对于我可怜的大脑的生活,我无法计算出完成此操作的代码。 任何提示都会很棒。

编辑(澄清):

我知道 static 属性需要从基础 class 中读取。 然而....

static 字段将包含具体 class 的基本名称 --> 通过 static 构造函数中的反射派生而来当我们在所有地方都这样做时,这很有效(我知道如何完成它)。

在这种情况下,我试图建立一个工厂 class 需要知道这个 static 字段,并且由于工厂实施的一些(其他)要求而需要通过反射来获得它。

编辑(再次)扩展代码:

这是我试图完成的一个几乎完整的示例,如果没用的话。

    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 的 static 构造函数中的部分。 希望这更清楚。

这是如何从实例中获取具体 class 的 static 属性:

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

static成员不会按照您的想法工作。 它们属于基础 class ,因此您尝试使用static继承的成员是不可能的。

暂无
暂无

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

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