9

In C#, I need to be able to create an array of Type objects at run-time based on a comma-delimited list of data types passed in to a function as a string. Basically, here is what I am trying to accomplish:

// create array of types
Type[] paramTypes = { typeof(uint), typeof(string), typeof(string), typeof(uint) };

But I need to be able to call my function like this:

MyFunction("uint, string, string, uint");

and have it generate the array dynamically based on the string passed in. Here was my first attempt:

void MyFunction(string dataTypes)
{
    //out or in parameters of your function.   
    char[] charSeparators = new char[] {',', ' '};
    string[] types = dataTypes.Split(charSeparators,
                        stringSplitOptions.RemoveEmptyEntries);

    // create a list of data types for each argument
    List<Type> listTypes = new List<Type>();
    foreach (string t in types)
    {
        listTypes.Add(Type.GetType(t)); 
    }
    // convert the list to an array
    Type [] paramTypes = listTypes.ToArray<Type>();

}

This code simply creates an array of null objects of type System.Type. I'm pretty sure the problem lies here:

listTypes.Add(Type.GetType(t));

Suggestions on why this syntax does not do the trick?

2
  • 1
    You may find this C#-aliases table useful for a switch statement or similar. msdn.microsoft.com/en-us/library/ya5y69ds.aspx Commented Jan 15, 2011 at 0:20
  • In Addition to the assemblyname-issue, consider to use the overloaded version of Type.GetType that ignores the case. Commented Jan 15, 2011 at 0:40

5 Answers 5

5

The problem is that there are no uint and string types in .NET. Those are C# type aliases to the actual System.UInt32 and System.String types. So you should call your function like this:

MyFunction("System.UInt32, System.String, System.String, System.UInt32");
Sign up to request clarification or add additional context in comments.

Comments

5

Pass in System.String, System.Int32 instead of string and int.

"string" is just shorthand for System.String. Type.GetType will not accept shorthand notation for types.

Comments

4

Use the full name for each type, including namespace. Like so:

class Program
{
    static void Main(string[] args)
    {
        var dataTypes = "System.UInt32, System.String, System.String, System.UInt32";

        //out or in parameters of your function.   
        char[] charSeparators = new char[] { ',', ' ' };
        string[] types = dataTypes.Split(charSeparators,
                            StringSplitOptions.RemoveEmptyEntries);

        // create a list of data types for each argument
        List<Type> listTypes = new List<Type>();
        foreach (string t in types)
        {
            listTypes.Add(Type.GetType(t));
        }
        // convert the list to an array
        Type[] paramTypes = listTypes.ToArray<Type>();
    }
}

Comments

2

It doesn't work because uint, string, etc are not the official names of the .net types. They're C# aliases for System.UInt32, System.String, etc. You'll need to use the .net type names if you want to create types dynamically like that.

Comments

0

I know this is old but this is how i cracked it:

private void Build_Files_DT()
    {
        String[] CLM_Header_Name_Array; Object[] Data_Type_Array;
        CLM_Header_Name_Array = new String[] { "File_Name_CLM", "Created_Date_Time_CLM", "Log_Read_CLM" };
        Data_Type_Array = new Object[] { typeof(System.String), typeof(System.DateTime), typeof(System.Boolean) };
        DT = new DataTable();
        Build_DT(DT, CLM_Header_Name_Array, Data_Type_Array);
    }

private void Build_DT(DataTable DT,  String[] CLM_Header_Name_Array, Object[] Data_Type_Array)
    {

        for (int i = 0; i < CLM_Header_Name_Array.Length; i++)
        {
            DT.Columns.Add(new DataColumn(CLM_Header_Name_Array[i], (Type)Data_Type_Array[i]));
        }
    }//Build_DT

Comments

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.