C# - 通过反射获取简单类型的用户友好的名称? [英] C# - Get user-friendly name of simple types through reflection?

查看:128
本文介绍了C# - 通过反射获取简单类型的用户友好的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 键入T = typeof运算(布尔);
字符串的typeName = t.Name;

在这个简单的例子,的typeName 将具有值布尔。我想知道,如果/我怎么才能得到它说布尔来代替。

同为INT /的Int32,双/双,串/串。


解决方案

 使用codeDOM;
使用Microsoft.CSharp;// ...类型T = typeof运算(布尔);字符串的typeName;
使用(VAR提供商=新CSHARP codeProvider())
{
    VAR typeRef =新的codeTypeReference(T);
    的typeName = provider.GetTypeOutput(typeRef);
}Console.WriteLine(typeName的); //布尔

Type t = typeof(bool);
string typeName = t.Name;

In this simple example, typeName would have the value "Boolean". I'd like to know if/how I can get it to say "bool" instead.

Same for int/Int32, double/Double, string/String.

解决方案

using CodeDom;
using Microsoft.CSharp;

// ...

Type t = typeof(bool);

string typeName;
using (var provider = new CSharpCodeProvider())
{
    var typeRef = new CodeTypeReference(t);
    typeName = provider.GetTypeOutput(typeRef);
}

Console.WriteLine(typeName);    // bool

这篇关于C# - 通过反射获取简单类型的用户友好的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆