Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9157953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:08:23+00:00 2026-06-17T13:08:23+00:00

My intention is to have a class T, that has several static readonly instances

  • 0

My intention is to have a class T, that has several static readonly instances of its own type. What I am now trying to do is to create a generic method that will recognize all those instances and add them to a list. So far I could locate all the elements, and the FieldInfo.FieldHandle.Value seems to contain the object, but I am yet unskilled in getting it. Maybe it is wrong to use FieldInfo. Can anyone give me a hand?

(Thanks!) Here is the code example (with solution applied):

using System;
using System.Collections.Generic;
using System.Reflection;

namespace PickStatic {
  class Fruit {
    public static readonly Fruit Orange = new Fruit("Orange");
    public static readonly Fruit Kiwi = new Fruit("Kiwi");
    public static readonly Fruit Pear = new Fruit("Pear");

    public string name { set; get; }

    public Fruit(string name) {
      this.name = name;
    }

    public static List<T> getAll<T>() where T : class {
      List<T> result = new List<T>();
      MemberInfo[] members = typeof(T).GetMembers();
      foreach(MemberInfo member in members) {
        if(member is FieldInfo) {
          FieldInfo field = (FieldInfo) member;
          if(field.FieldType == typeof(T)) {
            T t = (T) field.GetValue(null);
            result.Add(t);
          }
        }
      }
      return result;
    }

    public static void Main(string[] args) {
      List<Fruit> fruits = getAll<Fruit>();
      foreach(Fruit fruit in fruits) {
        Console.WriteLine("Loaded: {0}", fruit.name);
      }
      Console.ReadLine();
    }
  }
}

The class Fruit contains three static objects of type Fruit. It is being attempted to get the list of all such objects using a generic method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:08:24+00:00Added an answer on June 17, 2026 at 1:08 pm

    You need to use the FieldInfo.GetValue method.

    Invoke it with null (Since it’s a static field)

    Edit : It is not recommended to use reflection in “Critical” sections of the code (In terms of performance), You may use a Dictionary to cache the results returned from GetAll method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a base class that has many children. This base class defines
I have a class that has dependencies that I've wired up with Ninject. public
I have a class called PointF and it has a constructor that takes a
I have instances of a class MyClass , a and b . MyClass has
I have a class that has a property that is defined as interface. Users
Let's say I have a class that requires configuration, dependency injection etc. public class
I have a question regarding dependency injection. say i want to create a class
I want to create a read-only user in PostgreSQL. The intention is to have
I'm trying to generate code from an XSD that has two complexTypes extending a
For a project I have a series of pseudo-repositories that create XML (they aren't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.