【问题标题】:Return an Array of Constant Field values a certain Type from a Static Class从静态类返回特定类型的常量字段值数组
【发布时间】:2019-02-11 01:49:29
【问题描述】:

鉴于此代码:

public static class SubClass
{
    public const long poperty1 = 365635;
    public const long poperty2 = 156346;
    public const long poperty3 = 280847;
    .
    .
    public const long propertyN = 29145;

}

具有 N 个长属性的类。是否可以添加一个方法来返回具有所有属性值的 IEnumerable?

【问题讨论】:

  • 是的。有反射。
  • 这些是字段,不是属性
  • 您的示例代码表明您做错了什么。使用集合

标签: c# .net


【解决方案1】:

只是为了演示如何使用iterator method 非常简单地做到这一点...

public static IEnumerable<long> GetConstantsAsEnumerable()
{
    yield return poperty1;
    yield return poperty2;
    yield return poperty3;
}

...或array initializer...

public static long[] GetConstantsAsArray()
{
    return new long[] {
        poperty1,
        poperty2,
        poperty3
    };
}

当然,取决于N 的大小,任何一种方法都可能会增长很长时间。与 @TheGeneral's answer 不同,添加或删除常量时,您还必须手动更新方法以反映更改。

另外,对于@maccettura's point,如果这些编号的常量是相关的,并且您想以类似集合的方式访问它们,最好首先将它们存储为集合。你可以使用一个数组...

public static class SubClass
{
    public static readonly long[] properties = new long[] { 365635, 156346, 280847 };
}

...或者,为了确保元素永远不会被修改,ReadOnlyCollection&lt;&gt;...

using System.Collections.ObjectModel;

public static class SubClass
{
    public static readonly ReadOnlyCollection<long> properties = 
        new ReadOnlyCollection<long>(
            new long[] { 365635, 156346, 280847 }
        );
}

如果值是唯一的并且排序不重要,HashSet&lt;&gt; 可能是合适的...

using System.Collections.Generic;

public static class SubClass
{
    public static readonly HashSet<long> properties = new HashSet<long>(
        new long[] { 365635, 156346, 280847 }
    );
}

...如果您使用的是 .NET Core,并且再次希望确保集合永远不会被修改,您可以使用 ImmutableHashSet&lt;&gt;...

using System.Collections.Immutable;

public static class SubClass
{
    public static readonly ImmutableHashSet<long> properties = ImmutableHashSet.Create<long>(
        new long[] { 365635, 156346, 280847 }
    );
}

上述所有类型都可以按原样枚举,无需包装方法。

【讨论】:

    【解决方案2】:

    可以添加一个方法来返回一个 IEnumerable 与所有 属性值?

    你可以使用这个,它将从静态类中选择所有公共常量长字段

    var result = typeof(SubClass).GetFields(BindingFlags.Public | BindingFlags.Static)
                                 .Where(x=> x.IsLiteral && !x.IsInitOnly && x.FieldType == typeof(long))
                                 .Select(x => x.GetRawConstantValue());
    

    可以如下阅读

    typeof (C# Reference)

    用于获取某个类型的 System.Type 对象。一种类型的表达式 采用以下形式:

    Type.GetFields Method

    获取当前Type的字段。

    BindingFlags Enum

    Public 指定公共成员将包含在 搜索。

    Static 指定静态成员将包含在 搜索。

    返回

    FieldInfo Class

    发现字段的属性并提供对字段的访问 元数据。

    按(位置)过滤

    FieldInfo.IsLiteral Property

    获取一个值,表示该值是否在编译时写入 并且无法更改。

    FieldInfo.IsInitOnly Property

    获取一个值,该值指示该字段是否只能在正文中设置 构造函数。

    然后用选择

    FieldInfo.GetRawConstantValue Method

    通过编译器返回与字段关联的文字值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 2012-11-20
      • 1970-01-01
      • 2016-12-24
      相关资源
      最近更新 更多