Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取包含在其自身中的方法的所有静态实例_C#_.net_Reflection - Fatal编程技术网

C# 获取包含在其自身中的方法的所有静态实例

C# 获取包含在其自身中的方法的所有静态实例,c#,.net,reflection,C#,.net,Reflection,我有一个这样设置的类: public class Strategy { public readonly string Name; public readonly int Age; public Strategy(string Name, int Age) { this.Name = Name; this.Age = Age; } public static readonly Strategy CoolStrate

我有一个这样设置的类:

public class Strategy
{
    public readonly string Name;
    public readonly int Age;

    public Strategy(string Name, int Age)
    {
        this.Name = Name;
        this.Age = Age;
    }

    public static readonly Strategy CoolStrategy = new Strategy("Super Awesome", 24);
    public static readonly Strategy LameStrategy = new Strategy("Work Harder!", 14);
}
Strategy[] CurrentStrategies = typeof(Strategy).GetStaticInstances<Strategy>();
我希望能够使用一个使用反射的扩展,我可以这样说:

public class Strategy
{
    public readonly string Name;
    public readonly int Age;

    public Strategy(string Name, int Age)
    {
        this.Name = Name;
        this.Age = Age;
    }

    public static readonly Strategy CoolStrategy = new Strategy("Super Awesome", 24);
    public static readonly Strategy LameStrategy = new Strategy("Work Harder!", 14);
}
Strategy[] CurrentStrategies = typeof(Strategy).GetStaticInstances<Strategy>();
Strategy[]CurrentStrategies=typeof(Strategy).GetStaticInstances();
然后,当我在这个类中添加更多的静态策略时,这个扩展将返回它们的数组。因此在本例中,它将返回一个包含CoolStrategy和LameStrategy的数组

这将允许我将实例添加到类中,并且在其他地方可以获得数组


有什么想法吗?

不太清楚你在问什么;但是听起来你想检查FieldInfo.FieldType而不是GetValue…

我理解你的问题,你想得到给定类型的所有公共静态字段的值。以下方法正是这样做的:

public static IEnumerable<T> GetStaticInstances<T>()
{
    Type typeOfInstance = typeof(T);

    IEnumerable<T> instances = typeOfInstance
        .GetFields(BindingFlags.Public | BindingFlags.Static)
        .Where(field => field.FieldType == typeOfInstance)
        .Select(field => (T)field.GetValue(null));

    return instances;
}
公共静态IEnumerable GetStaticInstances()
{
立场类型=立场类型(T);
IEnumerable实例=立场类型
.GetFields(BindingFlags.Public | BindingFlags.Static)
.Where(field=>field.FieldType==typeOfInstance)
.Select(field=>(T)field.GetValue(null));
返回实例;
}

然而,仅仅在
策略
类中包含一个将返回所有可用策略的方法难道还不够吗?如果将来要添加新策略,只需将其添加到该方法中即可。

如果使用调试器逐步完成循环(或至少在异常之前尽可能多地执行),则,FieldInfo对象的属性告诉了你什么?请更清楚地回答你的问题。我已经知道字段的类型-它应该与类型参数相同…@william我显然不知道你在问什么。在您的示例代码中,您获得了给定类型的静态字段的值,但是您需要一个名为GetStaticFields的方法,该方法没有异常——这是您到目前为止描述的唯一方法。但是,仅仅在Strategy类中包含一个将返回所有可用策略的方法难道还不够吗?为什么要添加一个只复制元数据表一部分的方法?此外,人们可能会忘记添加新策略、意外复制或删除行等。
public static class StrategyExtensions
{
    public static Strategy[] GetAllStrategies()
    {
        var statics = typeof (Strategy).GetFields(BindingFlags.Static | BindingFlags.Public);
        var strategies = statics.Where(f => f.FieldType == typeof (Strategy));
        var values = strategies.Select(s => s.GetValue(null));
        return values.Cast<Strategy>().ToArray();
    }
}