2

I am working on a code generator written in C#. This code generator reads a xml file and generate a .cs file. So far we have assumed that all fields are string which is not true and I have changed the code so it can generate a type according to the specified type in our xml :

<MyField>
   <Name>Name</Name>
   <Type>String</Type>
</MyField>

But there might be some fields that can have more than one values (Multiselectable fields) that have another attribute (Options for example) that list all possible options. What I would like to do is to generate the array type for any given type .So that I can process my fields first and if there are some fields optional I can convert their type to an array.

Here's what I want to do :

Type fieldType=typeof(string);
Type fieldType=GetArrayTypeof(fieldType); 

How should I implement GetArrayTypeOf function ?

Beatles1692
  • 5,214
  • 34
  • 65
  • See http://stackoverflow.com/questions/493490/converting-a-string-to-a-class-name – BCdotWEB Oct 21 '14 at 10:32
  • @BCdotNET I think this question is not the same as mine because I don't want to make a generic type like List but I'd like to generate an array. – Beatles1692 Oct 21 '14 at 10:39
  • 1
    Then replace the generic type with an array: http://stackoverflow.com/questions/400900/how-can-i-create-an-instance-of-an-arbitrary-array-type-at-runtime – BCdotWEB Oct 21 '14 at 10:41

1 Answers1

2

To get type from its string name you can use Type.GetType method.

To get array type from item use Type.MakeArrayType instance method:

 string itemTypeName = "System.Int32";

 Type itemType = Type.GetType(itemTypeName);

 Console.WriteLine(itemType.MakeArrayType());
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53