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 ?

3
  • See stackoverflow.com/questions/493490/… Commented Oct 21, 2014 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<T> but I'd like to generate an array. Commented Oct 21, 2014 at 10:39
  • 1
    Then replace the generic type with an array: stackoverflow.com/questions/400900/… Commented Oct 21, 2014 at 10:41

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

2 Comments

Well I thought there might be a way without creating an instance :)
Funny that I haven't seen this method before :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.