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?
switchstatement or similar. msdn.microsoft.com/en-us/library/ya5y69ds.aspxType.GetTypethat ignores the case.