Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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 - Fatal编程技术网

C# 从静态类返回特定类型的常量字段值数组

C# 从静态类返回特定类型的常量字段值数组,c#,.net,C#,.net,鉴于此代码: 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个长属性的类。是否可以添加一个方法来返回包含所有属性值的I

鉴于此代码:

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

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

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

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

您可以阅读如下内容

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

获取当前类型的字段

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

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

返回的值

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

过滤依据(其中)

获取一个值,该值指示是否在编译时写入该值 不能改变

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

然后用选择

返回编译器与字段关联的文本值


只是为了演示你如何用一个

当然,取决于
N
的大小,这两种方法都可能增长很长时间。与此不同,在添加或删除常量时,还必须手动更新方法以反映更改

另外,对于,如果这些编号的常量是相关的,并且您希望以类似集合的方式访问它们,那么最好首先将它们存储为集合。你可以使用数组

public static class SubClass
{
    public static readonly long[] properties = new long[] { 365635, 156346, 280847 };
}
…或者,为了确保元素永远不会被修改,一个

使用System.Collections.ObjectModel;
公共静态类子类
{
公共静态只读只读只读集合属性=
新只读集合(
新长[{36563515646280847}
);
}
如果值是唯一的且顺序不重要,则a可能是合适的

using System.Collections.Generic;

public static class SubClass
{
    public static readonly HashSet<long> properties = new HashSet<long>(
        new long[] { 365635, 156346, 280847 }
    );
}
使用System.Collections.Generic;
公共静态类子类
{
公共静态只读哈希集属性=新哈希集(
新长[{36563515646280847}
);
}
…如果您正在使用.NET Core,并且再次希望确保该集永远不会被修改,则可以使用

使用System.Collections.Immutable;
公共静态类子类
{
公共静态只读ImmutableHashSet属性=ImmutableHashSet.Create(
新长[{36563515646280847}
);
}

以上所有类型都可以按原样枚举,而不需要包装器方法。

Yes。这些是字段,而不是poperties。您的示例代码表明您做错了什么。使用集合
public static class SubClass
{
    public static readonly long[] properties = new long[] { 365635, 156346, 280847 };
}
using System.Collections.ObjectModel;

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

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

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