C# 动态创建 Type 数组
在 C# 中,我需要能够在运行时基于以字符串形式传递给函数的逗号分隔数据类型列表创建 Type 对象数组。基本上,这就是我想要完成的任务:
// create array of types
Type[] paramTypes = { typeof(uint), typeof(string), typeof(string), typeof(uint) };
但是我需要能够像这样调用我的函数:
MyFunction("uint, string, string, uint");
并让它根据传入的字符串动态生成数组。这是我的第一次尝试:
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>();
}
此代码只是创建一个数组 System.Type 类型的空对象。我很确定问题出在这里:
listTypes.Add(Type.GetType(t));
关于为什么这种语法不起作用的建议?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题是 .NET 中没有
uint
和string
类型。这些是实际 System.UInt32 的 C# 类型别名和 < a href="http://msdn.microsoft.com/en-us/library/system.string.aspx" rel="noreferrer">System.String 类型。所以你应该这样调用你的函数:The problem is that there are no
uint
andstring
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:传入
System.String
、System.Int32
而不是string
和int
。“string”只是 System.String 的简写。
Type.GetType
将不接受类型的简写符号。Pass in
System.String
,System.Int32
instead ofstring
andint
."string" is just shorthand for System.String.
Type.GetType
will not accept shorthand notation for types.使用每种类型的全名,包括命名空间。就像这样:
Use the full name for each type, including namespace. Like so:
它不起作用,因为
uint
、string
等不是 .net 类型的正式名称。它们是System.UInt32
、System.String
等的 C# 别名。如果您想像这样动态创建类型,则需要使用 .net 类型名称。It doesn't work because
uint
,string
, etc are not the official names of the .net types. They're C# aliases forSystem.UInt32
,System.String
, etc. You'll need to use the .net type names if you want to create types dynamically like that.我知道这很旧,但这就是我破解它的方法:
I know this is old but this is how i cracked it: