给出以下代码:
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吗?
答案 0 :(得分: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());
您可以阅读以下内容
用于获取类型的System.Type对象。类型表达 采用以下形式:
获取当前类型的字段。
公共指定将公共成员包括在 搜索。
静态指定静态成员要包含在 搜索。
返回哪个
发现字段的属性并提供对字段的访问 元数据。
按(位置)过滤
获取一个值,该值指示该值是否在编译时写入 并且无法更改。
获取一个值,该值指示该字段是否只能在主体中设置 的构造函数。
然后选择
FieldInfo.GetRawConstantValue Method
由编译器返回与该字段关联的文字值。
答案 1 :(得分: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<>
...
using System.Collections.ObjectModel;
public static class SubClass
{
public static readonly ReadOnlyCollection<long> properties =
new ReadOnlyCollection<long>(
new long[] { 365635, 156346, 280847 }
);
}
如果值是唯一的并且排序不重要,则HashSet<>
可能是合适的...
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<>
...
using System.Collections.Immutable;
public static class SubClass
{
public static readonly ImmutableHashSet<long> properties = ImmutableHashSet.Create<long>(
new long[] { 365635, 156346, 280847 }
);
}
上述所有类型都可以按原样枚举,而无需使用包装方法。