Return an Array of Constant Field values a certain Type from a Static Class

Given this code:

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

}

A class with N long properties. Is possible to add a method to return an IEnumerable with all the properties values?

Upvotes: 1

Views: 565

Answers (2)

Lance U. Matthews
Lance U. Matthews

Reputation: 16596

Just to demonstrate how you could do this very simply with an iterator method...

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

...or an array initializer...

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

Of course, depending on how big N is either method could grow very long. Unlike @TheGeneral's answer, as constants are added or deleted you would also have to manually update the methods to reflect the change.

Also, to @maccettura's point, if these numbered constants are related and you want to access them in a collection-like manner, it would be better to store them as a collection in the first place. You could use an array...

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

...or, to ensure the elements are never modified, a ReadOnlyCollection<>...

using System.Collections.ObjectModel;

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

If the values are unique and ordering is not important a HashSet<> might be appropriate...

using System.Collections.Generic;

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

...and if you're using .NET Core and, again, want to ensure the set is never modified you can use an ImmutableHashSet<>...

using System.Collections.Immutable;

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

All of the above types can be enumerated as-is with no need for a wrapper method.

Upvotes: 1

TheGeneral
TheGeneral

Reputation: 81493

Is possible to add a method to return an IEnumerable with all the properties values?

You can use this, It will select all public constant long fields from the static class

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

You can read it as follows

typeof (C# Reference)

Used to obtain the System.Type object for a type. A typeof expression takes the following form:

Type.GetFields Method

Gets the fields of the current Type.

BindingFlags Enum

Public Specifies that public members are to be included in the search.

Static Specifies that static members are to be included in the search.

Which returns

FieldInfo Class

Discovers the attributes of a field and provides access to field metadata.

Filter by (Where)

FieldInfo.IsLiteral Property

Gets a value indicating whether the value is written at compile time and cannot be changed.

FieldInfo.IsInitOnly Property

Gets a value indicating whether the field can only be set in the body of the constructor.

Then select with

FieldInfo.GetRawConstantValue Method

Returns a literal value associated with the field by a compiler.

Upvotes: 3

Related Questions