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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:04:42+00:00 2026-05-15T02:04:42+00:00

I’m trying to store types in a collection, so that i can later instantiate

  • 0

I’m trying to store types in a collection, so that i can later instantiate objects of the types in the collection. But I’m not sure how to do this the best way.

What i have so far:

List<Type> list = new List<Type>();
list.Add(typeof(MyClass));
var obj = (MyClass)Activator.CreateInstance(list[0]);

I would like to have some constrains on the Type, or better yet, just a generic type in the collection instead of an instantiated Type object. Is this possible?

  • 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-15T02:04:42+00:00Added an answer on May 15, 2026 at 2:04 am

    In this particular scenario, where it seems we have a “factory” pattern, we would constrain the method invoking the activator, such as

    private readonly List<Type> _supportedTypes = new List<Type> ();
    public void RegisterSupportedType<T> () where T : SomeConstraintType
    {
        _supportedTypes.Add (typeof (T));
    }
    
    // if we do not know the type, but somehow know an index to type
    public object Create (int supportedTypeIndex)
    {
        object untyped = Activator.
            CreateInstance (_supportedTypes[supportedTypeIndex]);
        return untyped;
    }
    
    // if we know instance type\subtype (eg interface) and know an index
    public T Create<T> (int supportedTypeIndex)
    {
        T typed = default (T);
        object untyped = Create (supportedTypeIndex);
        if (!(untyped is T))
        {
            // throw meaningful exception :)
        }
        typed = (T)(untyped);
        return typed;
    }
    

    An alternative, is to create a constrained Type

    public class ConstrainedType<T> 
    {
        public Type Type { get; private set; }
        public ConstrainedType (Type type)
        {
            // may have this backward, would have to fact check before
            // rolling out to prod ;)
            if (!typeof (T).IsAssignableFrom (type))
            {
                // throw meaningful exception!
            }
            Type = type;
        }
    }
    
    List<ConstrainedType<SomeTypeConstraint>> list = 
        new List<ConstrainedType<SomeTypeConstraint>> ();
    
    // will throw meaningful exception if MyClass is not 
    // SomeTypeConstraint or a sub class
    list.Add (new ConstrainedType (typeof (MyClass)));
    
    SomeTypeConstraint baseType = 
        (SomeTypeConstraint)(Activator.CreateInstance(list[0].Type));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.