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 7616133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:48:57+00:00 2026-05-31T02:48:57+00:00

I have a generic class with a non generic static method that performs some

  • 0

I have a generic class with a non generic static method that performs some operations on the type in question.

class MyClass<T> {  
  static List<T> _listOfStuff;

  public static void DoX() { _listOfStuff.Clear(); }
}

Rather than writing the code below, I’d like to use reflection and loop over the generic types and calling DoX().

Keep in mind that the list of types could grow in the future and that MyClass could be used by developers external to the assembly where this code is maintained. I need an automated way to get a Type[] array of int, string, TypeX.

MyClass<int>.DoX();
MyClass<string>.DoX();
MyClass<TypeX>.DoX();

Any help would be appreciated.

  • 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-05-31T02:48:58+00:00Added an answer on May 31, 2026 at 2:48 am

    It seems to me that this is a very weird thing to do. If you want to perform some cleanup, you might consider using non-static classes, or something else. Again, this really does smell of bad design to me, but I can’t advise you any better way, because I still have no idea why are you trying to do this.

    If you’re sure you want to do this, there doesn’t seem to be a way to get a list of all used types directly. But you can keep a list of all used constructed types in some other non-generic type and add to it from the static constructor.

    But if all you want to do is to call DoX() for all types, you don’t actually need a list of types, you can use a list of delegates to DoX() for all used types. That could look something like this:

    class MyClass<T>
    {
        static MyClass()
        {
            MyClass.DoXDelegates.Add(DoX);
        }
    
        public static void DoX() { /* whatever */ }
    }
    
    static class MyClass
    {
        private static readonly List<Action> s_DoXDelegates = new List<Action>();
        internal static List<Action> DoXDelegates
        {
            get { return s_DoXDelegates; }
        }
    
        internal static void DoXForAll()
        {
            foreach (var doXDelegate in DoXDelegates)
                doXDelegate();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic class that should allow any type, primitive or otherwise. The
I have a generic class that I'm trying to implement implicit type casting for.
I have a generic class public MyClass<TContext, T> where TContext : DataContext that effectively
I have a generic class that takes a type T . Within this class
I have a generic method (in a non generic class) returning elements. public IEnumerable<T>
I have a base class which is non-generic with a derived generic class. The
I have a generic class, but I want my type to be forced to
I have a generic class that needs to limit an enum depending on the
I have a generics class, that uses TBase as the type parameter. Using MEF,
I have a public static property in a class. The class has some custom

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.