C# System.Type.FullName的意外值
我最近需要为任意类型构建C#特定名称(必须始终包含global::specifier),并遇到以下问题:C# System.Type.FullName的意外值,c#,.net,multidimensional-array,typeof,gettype,C#,.net,Multidimensional Array,Typeof,Gettype,我最近需要为任意类型构建C#特定名称(必须始终包含global::specifier),并遇到以下问题: // 1 - value: System.String[,,,][,,][,] string unexpectedFullName = typeof( string[,][,,][,,,] ).FullName; // 2 - value: System.String[,][,,][,,,] string expectedFullName = Type.GetType( "Sy
// 1 - value: System.String[,,,][,,][,]
string unexpectedFullName = typeof( string[,][,,][,,,] ).FullName;
// 2 - value: System.String[,][,,][,,,]
string expectedFullName = Type.GetType( "System.String[,][,,][,,,]" ).FullName;
我希望在这两种情况下返回的值是相同的。但是,由于某种原因,值中与数组相关的部分似乎颠倒了(情况1)。这是预期的反转行为吗?虽然和C类型标识符返回的值有时恰好相同,但这不能保证。请记住,
Type.FullName
返回相同的值,而不管它是从哪种CLI语言调用的,无论是C#、VB.NET、Oxygene还是其他任何语言
对于多维数组和交错数组,C#语法按稍后写入的顺序列出索引,而反射语法返回与数组逻辑结构匹配的内容。(C#)string[,][,][,][,][,][,][,][,][,][/code>毕竟是string
类型的值,其为四维数组(即string[,,,][,][,][,][,][,][,][code>),其为三维数组(即string[,][,][,][,][,][,][,][,][,][,][code>),其为二维数组(即)
在分析类型时,您可能需要检查Type
类的属性,而不是依赖FullName
返回的反射语法名称。可以从中检索诸如或的信息
构造类型时,还可以使用或等方法在运行时创建复杂类型,而无需构造包含新类型成分的字符串
这个答案的一些内容是由——谢谢 注意:这并不能直接解决您的问题
这是预期的行为吗
但我觉得这会增加它
您可以使用返回字符串,该字符串可用于生成代码以为您生成类型,例如,使用此代码(由修改):
使用FriendlyName
// returns "string[,][,,][,,,]"
typeof(string[,][, ,][, , ,]).FriendlyName();
// returns "string[,,,][,,][,]"
typeof(string[, , ,][, ,][,]).FriendlyName();
// returns "System.Collections.Generic.List<int>"
typeof(List<int>).FriendlyName();
//返回“字符串[,][,,][,,][,,,,]”
typeof(字符串[,][,][,]).FriendlyName();
//返回“字符串[,,][,][,][,]”
typeof(字符串[,][,][,]).FriendlyName();
//返回“System.Collections.Generic.List”
typeof(List.FriendlyName();
我以前遇到过这个问题;似乎在C#中,当您按照读取顺序声明数组索引/维度时,反射返回与其逻辑结构匹配的类型名。(C#)string[,][,],[,],,]
毕竟是一个string
类型的值,它是一个四维数组(即string[,,,],
),它是一个三维数组(即string[,],[,,],
),它是一个二维数组(即string[,,,,],[,,],,[,,],
)。不需要c#语法()来匹配反射命名约定。因此,如果您正在处理反射:请使用反射命名。在其他新闻中,嵌套类型不是Outer.Inner
,而是Outer+Inner
,泛型不是Foo
,而是Foo`3
。我根据一些输入类型生成代码,因此我肯定需要反转通过System.Type instance提供的索引/维度信息。@O.R.Mapper:你们中有人可以将此作为答案发布,以便我可以接受吗?@MarcGravel:你们中有人可以将此作为答案发布吗我可以接受吗?
// returns "System.String[,,,][,,][,]"
typeof(string[,][, ,][, , ,]).FullName;
// returns "System.String[,][,,][,,,]"
typeof(string[, , ,][, ,][,]).FullName;
// returns "System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]"
typeof(List<int>).FullName;
// returns "string[,][,,][,,,]"
typeof(string[,][, ,][, , ,]).FriendlyName();
// returns "string[,,,][,,][,]"
typeof(string[, , ,][, ,][,]).FriendlyName();
// returns "System.Collections.Generic.List<int>"
typeof(List<int>).FriendlyName();