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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:05:37+00:00 2026-06-03T00:05:37+00:00

Is there a way to create a generic class from type parameter. I have

  • 0

Is there a way to create a generic class from type parameter.

I have something like this:

public class SomeClass
{
    public Type TypeOfAnotherClass { get; set; }
}

public class OneMoreClass
{
}

public class GenericClass<T>
    where T : class
{
    public void DoNothing() {}
}

public class GenericClass
{
    public static GenericClass<T> GetInstance<T>(T ignored)
        where T : class
    {
        return new GenericClass<T>();
    }
}

What I want is to create a GenericClass from a Type. Ex:

var SC = new SomeClass();
SC.TypeOfAnotherClass = typeof(OneMoreClass);
var generic = GenericClass.GetInstance(SC.TypeOfAnotherClass);

Assert.AreEqual(typeof(GenericClass<OneMoreClass>), generic.GetType());

Here I expect to get instance of GenericClass<OneMoreClass> but I get GenericClass<Type>

I also tried using instance of that type. Ex:

var generic = GenericClass.GetInstance(Activator.CreateInstance(SC.TypeOfAnotherClass));

This time I get GenericClass<object>

Is there a way to accomplish this task?

  • 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-03T00:05:38+00:00Added an answer on June 3, 2026 at 12:05 am

    If you know the type you actually want (OneMoreClass) at build time, then you should just use it:

    var generic = GenericClass.GetInstance<OneMoreClass>();
    

    But I am assuming you don’t know it at build time, and have to get the type at runtime.
    You can do it with reflection, but it isn’t pretty, and is slow:

    public class GenericClass
    {
        public static object GetInstance(Type type)
        {
            var genericType = typeof(GenericClass<>).MakeGenericType(type);
            return Activator.CreateInstance(genericType);
        }
    }
    

    Since you don’t know the resulting type at build time, you can’t return anything but object (or dynamic) from the method.


    Here is how much slower (for 100,000 creates)

    public class GenericClass
    {
        public static object GetInstance(Type type)
        {
            var genericType = typeof(GenericClass<>).MakeGenericType(type);
            return Activator.CreateInstance(genericType);
        }
    
        public static GenericClass<T> GetInstance<T>()
            where T : class
        {
            return new GenericClass<T>();
        }
    }
    
        [Test]
        public void CanMakeGenericViaReflection_ButItsSlow()
        {
            var timer = new Stopwatch();
            var SC = new SomeClass();
            SC.TypeOfAnotherClass = typeof(OneMoreClass);
    
            timer.Start();
            for (int x = 0; x < 100000; x++)
            {
                GenericClass.GetInstance(SC.TypeOfAnotherClass);
            }
            timer.Stop();
            Console.WriteLine("With Reflection: " + timer.ElapsedMilliseconds + "ms.");
    
            timer.Restart();
            for (int x = 0; x < 100000; x++)
            {
                GenericClass.GetInstance<OneMoreClass>();
            }
            timer.Stop();
            Console.WriteLine("Without Reflection: " + timer.ElapsedMilliseconds + "ms.");
        }
    

    Results:

    With Reflection: 243ms.
    Without Reflection: 2ms.
    

    So a little over 100x slower.

    The real thing to note about generics is that the <T>s in generics are resolved at build-time by the C# compiler, and the real class names are inserted. When you have to defer that until run-time, you end up paying the performance price.

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

Sidebar

Related Questions

Is there any way to create a new NSString from a format string like
Is there any way to create a property like this C# property in Objective-C?
Is there a way to create a condition like this? @products = Product.find(:all, :limit
I have a generic class like this: class MyGenericClass<T> { } On the other
I have an XML document that looks like this <Elements> <Element> <DisplayName /> <Type
I was thinking to create a generic base class and have each of the
I have a generic Class I'm using to hold information loaded from a database.
I have an ECMContext class that inherits from DbContext. Within ECMContext there is a
Is there any possible way that a generic type can be used to contain
Is there any way to create a naming convention for my primary key constraints

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.