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

  • SEARCH
  • Home
  • 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 4012992
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:18:58+00:00 2026-05-20T09:18:58+00:00

My base class use reflection on derived classes to provide some functionality to them.

  • 0

My base class use reflection on derived classes to provide some functionality to them. Currently I do it this way:

abstract class BaseClass<T>
{
    string GetClassString()
    {
        // Iterate through derived class, and play with it's properties
        // So I need to know the type of derived class (here T).
        return result;
    }

    static bool TryParse(string classString, out T result)
    {
        // I should declare a variable as T and return it
    }
}

Can I do this without generics ?

  • 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-20T09:18:58+00:00Added an answer on May 20, 2026 at 9:18 am

    Edit:

    Sorry, you want the type parameter (ie typeof(T)).
    In that case you still use this.GetType() but you add .GetGenericArguments()[0] after.

    Try parse:
    You need to create a new instance of a type you don’t know

    There are two ways:
    First, without changing the rest, with the Activator class and the following code:

    result = (T) Activator.CreateInstance(typeof(T))
    

    (MSDN).

    Then, you could add a “new” constraint to your type:

    MyClass<T> where T : new() {...}
    result = new T();
    

    Both samples require parameter less constructors. If you want to pass parameters, then you need to go deeper inside System.Reflection, get the list of constructors and call the one you want. A factory pattern may also do the job.

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

Sidebar

Related Questions

How to use reflection call a base method that is overridden by derived class?
What do people here use C++ Abstract Base Class constructors for in the field?
I have a base class like this: package MyClass; use vars qw/$ME list of
I am trying to use reflection to determine which methods a derived class overrides
Say I have an abstract base class BaseClass . I'd like to give it
What is the use of creating base class object using child class reference in
When should I use an interface and when should I use a base class?
In the inherited class I use the base constructor, but I can't use the
In base class constructors I always see a parameterless constructor, like so: public abstract
As part of the base class for some extensive unit testing, I am writing

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.