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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:06:00+00:00 2026-06-15T03:06:00+00:00

The example below shows the intent. The example is iterating through an array of

  • 0

The example below shows the intent. The example is iterating through an array of Objects, and creating an array with the same Types loaded with default values. The values themselves do not need to be copied from one list to the next. This is sort of a “Type Clone” of the list.

The code below produces the following error for the Activator:

“System.Int32 is not a GenericTypeDefinition. MakeGenericType may only be called on a type for which Type.IsGenericTypeDefinition is true.”

The details of Activator, CreateInstance, and MakeGenericType are still a little confusing to me.

How can I change the code to avoid the error ?

Any suggestions would be appreciated.

    private void Test()
    {
        object[] a = new object[] {100, "Text", new clsMyClass()};
        object[] b = new object[a.Length];

        for (int i = 0; i < a.Length; i++)
        {
            b[i] = Activator.CreateInstance(a[i].GetType().MakeGenericType());
        }

        for (int i = 0; i < b.Length; i++)
        {
            Console.WriteLine(b[i].GetType().ToString());
        }
    }

The expected output would be:

  • System.Int32
  • System.String
  • MyNamespace.clsMyClass

The resulting values would be:

  • b[0] = 0
  • b[1] = “”
  • b[2] = new clsMyClass
  • 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-15T03:06:02+00:00Added an answer on June 15, 2026 at 3:06 am

    remove MakeGenericType() and it will start to work, use this

    b[i] = Activator.CreateInstance(a[i].GetType());
    

    MakeGenericType is for Substituting generic parameters with defined ones.
    As example if your array would contain List<T>, call of a[i].GetType().MakeGenericType(typeof(int)) will return type List<int>.

    In your case I don’t see any generic type, so I wonder why you use it

    Also note that calling Activator.CreateInstance like so will work only if type have parameterless constructor, which is not true for example in case of string. You can handle string case like this

    for (int i = 0; i < a.Length; i++)
    {
       var oType = a[i].GetType();
       if (oType == typeof(string)) 
           b[i] = string.Empty; //or may be null 
       else
           b[i] = Activator.CreateInstance(oType);
    }
    

    Something simular should be done for all types without parameterless constructor.
    Activator.CreateInstance(type, parameters...) can do the job.
    You can use reflection to investigate type constructor paramaters if needed

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

Sidebar

Related Questions

The example below shows the code I am using to test whether a user
The example below shows the result for every Name that has a connection to
Slower than what? slower than creating the same textures from an image loaded from
The code example below shows how I search by the Name string in a
I wonder if anyone could assist? Table_A below shows an example product in my
Given the example below, can someone please show me how this could be called?
I have a SQLite table called posts . An example is shown below. I
Reproducible example below. I have a simulation loop, within which I occasionally have rows
The example below, is just an example, I know that I don't need an
this example below works when hover event is trigered and when its not, its

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.