System.Type.FullName意想不到的价值 [英] Unexpected value of System.Type.FullName

查看:920
本文介绍了System.Type.FullName意想不到的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近需要一个任意类型编译C#具体名称(必须始终包括全球::说明),并来到翻过以下问题:

I recently needed to build C# specific name (which must always include global:: specifier) for an arbitrary type and have come accross following issue:

// 1 - value: System.String[,,,][,,][,]
string unexpectedFullName = typeof( string[,][,,][,,,] ).FullName;      

// 2 - value: System.String[,][,,][,,,]
string expectedFullName = Type.GetType( "System.String[,][,,][,,,]" ).FullName;



我期待一个返回值会在两种情况下相同。然而,由于某种原因值的数组相关的部分似乎被反转(情况1)。这是反转预期的行为?

I was expecting that returned value would be same in both cases. However, for some reason the array related part of the value seems to be reversed (case 1). Is this reversal expected behavior?

推荐答案

尽管通过的 Type.FullName 和C#类型标识符有时碰巧是相同的,这是不能保证。请记住, Type.FullName 不管什么CLI语言是从所谓的返回值相同,无论是C#,VB.NET,OXYGENE或其他任何东西。

While the value returned by Type.FullName and the C# type identifier sometimes happen to be the same, this is not guaranteed. Keep in mind that Type.FullName returns the same value regardless of what CLI language it is called from, be it C#, VB.NET, Oxygene or anything else.

有关多维数组和交错数组,C#语法列出都写在后面的顺序指数,而反射语法返回该数组的逻辑结构相匹配的东西。和(C#)字符串[,] [,,] [,,,] 毕竟,类型的值字符串,其4维​​数组(即字符串[,,,] ),其3维数组(即字符串[ ,,,] [,,] )及其二维阵列(即字符串[,,,] [,,] [,] )。

For multidimensional and jagged arrays, C# syntax lists the indices in the order they are written later on, while reflection syntax returns something that matches the logical structure of the array. And a (C#) string[,][,,][,,,] is, after all, a value of type string, thereof a 4-dimensional array (i.e. string[,,,]), thereof a 3-dimensional array (i.e. string[,,,][,,]) and thereof a 2-dimensional array (i.e. string[,,,][,,][,]).

而不是依靠>按全名键入类的分析类型时。例如尺寸的数量的信息或的generic参数可以从那里取回。

Rather than relying on the reflection syntax name returned by FullName, you might want to examine the properties of the Type class when analyzing types. Information such as the number of dimensions or the generic arguments can be retrieved from there.

当构造类型,您还可以使用诸如 MakeArrayType MakeGenericType 创建复杂的类型在运行时无需构建包含新类型的成分的字符串。

When constructing types, you can also use methods such as MakeArrayType or MakeGenericType to create complex types at runtime without constructing a string that contains the ingredients for the new types.

一些这个答案的内容,指出通过出马克Gravell - 谢谢你

Some of the contents of this answer was pointed out by Marc Gravell - thank you!

这篇关于System.Type.FullName意想不到的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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