In C #, I need to be able to create an array of Type objects at runtime based on a list of data separated by commas, data types passed to the 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 as follows:
MyFunction("uint, string, string, uint");
and make it generate an array dynamically based on the passed string. Here is 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 am sure the problem is here:
listTypes.Add(Type.GetType(t));
Suggestions on why this syntax doesn't do the trick?
arraylist arrays list c # runtime
acordner
source share