从字符串的.Net C#获取完全限定的类型名称 [英] Getting a fully qualified type name from a string .Net C#

查看:128
本文介绍了从字符串的.Net C#获取完全限定的类型名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如内部,双,日期时间等我如何从该字符串得到完全合格的名称?

I have a string such as "Int", "Double", "DateTime" etc. How do I get the fully qualified name from this string?

推荐答案

这是值得铭记 INT 不是一个类型。这是该类型 System.Int32的提供C#的别名。考虑到这一点,并假设你只关心核心类型,你可以试试:

It's worth bearing in mind that int isn't a type. It's an alias provided by C# for the type System.Int32. Bearing that in mind, and assuming you only care about core types, you can try:

var typeName = "DateTime";

var systemTypesAssembly = Assembly.Load("mscorlib");

var type = (from t in systemTypesAssembly.GetTypes()
           where t.Name == typeName
           select t).FirstOrDefault();

Console.WriteLine(type.FullName);

正如我所说的,这个不会工作诠释,但将努力为的Int32 这是它的别名类型。 这code也没有包含错误处理的地方,例如,没有类型在mscorlib中匹配您在的typeName

As I said, this will not work for Int, but would work for Int32 which is the type it's aliased to. This code also contains no error handling for where, for example, no type is found in mscorlib that matches the one you've entered in typeName

这篇关于从字符串的.Net C#获取完全限定的类型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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