Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2006

Tip: Looking for answers? Try searching our database.

create the array using reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roy - 01 Aug 2006 16:41 GMT
I have a type of System.Type. It indicates an primitive type such as Int32. I
try to use the following code to create an array

Type arrayType = type.MakeArrayType(5);
object objArray = Activator.CreateInstance(arrayType);

but I got the following error:
{"No parameterless constructor defined for this object."}

Apparently, this is caused by the fact that the array type does not have a
default constructor.
How do I create the array using reflection?
Jon Shemitz - 01 Aug 2006 19:35 GMT

> I have a type of System.Type. It indicates an primitive type such as Int32. I
> try to use the following code to create an array
[quoted text clipped - 8 lines]
> default constructor.
> How do I create the array using reflection?

I wrote the following app to examine the array's constructors:

using System;
using System.Reflection;

namespace NewArray
{
 class Program
 {
   static void Main(string[] args)
   {
     Type IntType = typeof(int);
     Type IntArrayType = IntType.MakeArrayType(5);
     ConstructorInfo[] Constructors = IntArrayType.GetConstructors();
     foreach (ConstructorInfo Constructor in Constructors)
     {
       ParameterInfo[] Parameters = Constructor.GetParameters();
       string[] Types = new string[Parameters.Length];
       for (int Index = 0; Index < Parameters.Length; Index++)
         Types[Index] = Parameters[Index].ParameterType.Name;
       Console.WriteLine("({0})", String.Join(", ", Types));
     }
     Console.ReadLine();
   }
 }
}

//

It gives the following output:

(Int32, Int32, Int32, Int32, Int32)
(Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32)

//

That is, you can pass either five lengths (one for each dimension) or
five pairs of start/stop indices. Thus,

 int[, , , ,] A = (int[, , , ,])
   Activator.CreateInstance(IntArrayType, 3, 3, 3, 3, 3);

is exactly analagous to (but slower than)

 int[, , , ,] B = new int[3, 3, 3, 3, 3];

Signature

.NET 2.0 for Delphi Programmers        www.midnightbeach.com/.net
Delphi skills make .NET easy to learn  In print, in stores.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.