如何从字符串“ string []”中获取.NET数组类型? [英] How to get .NET array type from the string "string[]"?
问题描述
给出字符串 string []
,并要求获取该类的基础类型,可能以以下内容开头:
Given the string "string[]"
and asked to get the underlying Type for this class, one might start with:
private Type getTypeByName(string typeName)
{
if (typeName.EndsWith("[]"))
{
return something; // But what?
}
return Type.GetType(typeName);
}
string []是什么类型,如何反映出该类型
What type is "string[]" and how does one reflect the type out of it?
显然有一个 System.String
类型和一个 System.Array
类型,但我看不到它们如何像在 Nullable< T>
及其T与 MakeGenericType
方法。
Obviously there's a System.String
type and a System.Array
type, but I can't see how they can be reflected "together" as you would normally do for Nullable<T>
and its T with the MakeGenericType
method.
任何帮助打破我陷入困境的思路的人,将不胜感激!
Any help to break the mind-loop I've gotten myself into will be greatly appreciated!
推荐答案
究竟是什么问题? Type.GetType
可以正常工作:
What exactly is your problem? Type.GetType
works fine:
Console.WriteLine(typeof(string[]));
var type = Type.GetType("System.String[]");
Console.WriteLine(type);
打印:
System.String[]
System.String[]
这按预期工作。
这篇关于如何从字符串“ string []”中获取.NET数组类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!