通过反射获取C#类型的.Net对应类型
是否有一个函数,在给定C#type的字符串表示形式的情况下,返回相应的.Net类型或.Net类型的字符串表示形式;或者任何实现这一点的方法 例如: “bool”->System.Boolean或“System.Boolean” “int”->System.Int32或“System.Int32” 谢谢通过反射获取C#类型的.Net对应类型,c#,.net,reflection,types,C#,.net,Reflection,Types,是否有一个函数,在给定C#type的字符串表示形式的情况下,返回相应的.Net类型或.Net类型的字符串表示形式;或者任何实现这一点的方法 例如: “bool”->System.Boolean或“System.Boolean” “int”->System.Int32或“System.Int32” 谢谢 编辑:真的很抱歉,我想要的不是“类型到类型”的映射,而是“字符串到字符串”的映射或“字符串到类型”的映射。你的问题并不完全清楚:我不确定你的C#别名是什么形式。如果您在编译时知道它,您可以像平常
编辑:真的很抱歉,我想要的不是“类型到类型”的映射,而是“字符串到字符串”的映射或“字符串到类型”的映射。你的问题并不完全清楚:我不确定你的C#别名是什么形式。如果您在编译时知道它,您可以像平常一样使用
typeof()
——C#别名实际上只是别名,因此typeof(int)==typeof(System.Int32)
。发出的代码没有区别
如果您有一个字符串,例如“int”
,只需构建一个映射:
Dictionary<string,Type> CSharpAliasToType = new Dictionary<string,Type>
{
{ "string", typeof(string) },
{ "int", typeof(int) },
// etc
};
代码很短,不太可能更改,因此我认为有一个字典或一个大的switch
语句来映射这些代码应该不难维护
如果您想支持可为null的类型,我相信您除了解析输入字符串之外别无选择:
static Type GetTypeFromNullableAlias(string name)
{
if (name.EndsWith("?"))
return typeof(Nullable<>).MakeGenericType(
GetTypeFromAlias(name.Substring(0, name.Length - 1)));
else
return GetTypeFromAlias(name);
}
static Type GetTypeFromAlias(string name)
{
switch (name)
{
case "bool": return typeof(System.Boolean);
case "byte": return typeof(System.Byte);
case "sbyte": return typeof(System.SByte);
case "char": return typeof(System.Char);
case "decimal": return typeof(System.Decimal);
case "double": return typeof(System.Double);
case "float": return typeof(System.Single);
case "int": return typeof(System.Int32);
case "uint": return typeof(System.UInt32);
case "long": return typeof(System.Int64);
case "ulong": return typeof(System.UInt64);
case "object": return typeof(System.Object);
case "short": return typeof(System.Int16);
case "ushort": return typeof(System.UInt16);
case "string": return typeof(System.String);
default: throw new ArgumentException();
}
}
别这么复杂
试一试
…到目前为止,我一直在使用字典进行映射,但它很重,而且很难维护。此外,我想自动查找可空类型:“bool?”->System.nullable`1[System.Boolean]以什么方式维护它“重”或困难?当然,除非C#团队添加一个新的类型别名,否则不会进行任何维护,我认为这是不太可能的。(对于C#4,我不会在其中包含
dynamic
,因为这并不构成真正的CLR类型。)至于“?”-只需检查字符串是否以它结尾。。。(将提供示例代码。)嗨,认真点-我更新了你的标题,删除了你在开头放的标签。因为您正确地标记了您的问题,所以不需要它们。@Michael:谢谢,当时我习惯于在传统论坛中,您在标题中明确指定标记:)
static Type GetTypeFromNullableAlias(string name)
{
if (name.EndsWith("?"))
return typeof(Nullable<>).MakeGenericType(
GetTypeFromAlias(name.Substring(0, name.Length - 1)));
else
return GetTypeFromAlias(name);
}
static Type GetTypeFromAlias(string name)
{
switch (name)
{
case "bool": return typeof(System.Boolean);
case "byte": return typeof(System.Byte);
case "sbyte": return typeof(System.SByte);
case "char": return typeof(System.Char);
case "decimal": return typeof(System.Decimal);
case "double": return typeof(System.Double);
case "float": return typeof(System.Single);
case "int": return typeof(System.Int32);
case "uint": return typeof(System.UInt32);
case "long": return typeof(System.Int64);
case "ulong": return typeof(System.UInt64);
case "object": return typeof(System.Object);
case "short": return typeof(System.Int16);
case "ushort": return typeof(System.UInt16);
case "string": return typeof(System.String);
default: throw new ArgumentException();
}
}
GetTypeFromNullableAlias("int?").Equals(typeof(int?)); // true
typeof(bool).ToString()
typeof(string).ToString()
typeof(int).ToString()
typeof(char).ToString()