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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:12:43+00:00 2026-06-16T00:12:43+00:00

So I asked something similar last week, but I think it was pretty confusing,

  • 0

So I asked something similar last week, but I think it was pretty confusing, so Ill try to simplify it.

Say for instance I have a class that contains only properties like this:

public class MyPropertyClass
{
    public int IntegerProperty { get; set; }
}

Now suppose I have created another class with an array of MyPropertyClass like this:

public class AnotherPropertyClass
{
    public MyPropertyClass[] ArrayProperty { get; set; }
}

Now here is the complicated part.
I need to dynamically create a MyPropertyClass[] somehow. I’ve been trying it with a List<object> thus far. Then, make a call to InvokeMember with this array. Something like this:

//The list that I am adding elements to elsewhere in the code
List<object> objList = new List<object>();

//Adding a couple elements
objList.Add(new MyPropertyClass());
objList.Add(new MyPropertyClass());

//Create the parameter object array, has to be length one and contain an
//object array casted to MyPropertyClass or it will throw an exception.
object[] ob = new object[1] { objList.ToArray() };

//Instantiate the actual object I want to assign the array to.
object obj = new AnotherPropertyClass();

//The call to InvokeMember
obj.GetType().InvokeMember(
    "ArrayProperty",
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty,
    Type.DefaultBinder,
    obj,
    ob);

This code will throw an exception. The problem is, objList.ToArray() creates an object[] and when InvokeMember tries to assign it to the MyPropertyClass[], it complains about the type mismatch, even though all of the elements are MyPropertyClass types. Basically what I need is a way to say, “hey, all of the elements in objList.ToArray() are going to be MyPropertyClass” i.e object{MyPropertyClass[]} while letting the actual type be arbitrary, it might not be MyPropertyClass, it could be some other type, I don’t know at compile time.

What I have here is only my attempt so far, if you know a different approach i’m all ears. If you want more information, see my old question here:

runtime casting of an object[] to a custom type array

I just think there is little too much extra stuff in there that’s not related to the actual problem i’m having.

  • 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-16T00:12:44+00:00Added an answer on June 16, 2026 at 12:12 am

    You can create an array of an unspecified type like this:

    Array array = Array.CreateInstance(someType, someSize);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have asked something similar in the past but think I might have worded
I have asked something similar before, but never go to the solution I need.
Something similar has been asked in maven user list but I have the situation,
I have asked something similar but not the same at here (and it's already
O.K. Maybe I have asked something similar to this, but this is another problem
In another question , I asked something similar but I ended up just posting
Perhaps something similar has already been asked, and sure, it's a nitpick... I have
Several months ago I asked something similar , but I was using JavaScript to
Ok, I asked something similar before, but I've researched it and haven't found this
Well, I have this question and I see that someone already asked something similar

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.