C#Microsoft.CodeAnalysis中的System.IntPtr和nint
在类型的正常比较中C#Microsoft.CodeAnalysis中的System.IntPtr和nint,c#,code-analysis,intptr,microsoft.codeanalysis,sourcegenerators,C#,Code Analysis,Intptr,Microsoft.codeanalysis,Sourcegenerators,在类型的正常比较中 typeof(nint)==typeof(System.IntPtr)为真 所以我希望System.IntPtr的SpecialType是nin,就像System.Int32和int一样,在使用Microsoft.codesanalysis进行代码分析时 但事实证明System.IntPtr的特殊类型仍然是System.IntPtr 这是我的密码 string GetSpecialTypeForDisplayIfAny(ISymbol symbol) { return
typeof(nint)==typeof(System.IntPtr)
为真
所以我希望System.IntPtr
的SpecialType
是nin
,就像System.Int32
和int
一样,在使用Microsoft.codesanalysis
进行代码分析时
但事实证明System.IntPtr的特殊类型仍然是System.IntPtr
这是我的密码
string GetSpecialTypeForDisplayIfAny(ISymbol symbol)
{
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat
.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier)
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.UseSpecialTypes)
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNullableReferenceTypeModifier)
.AddMiscellaneousOptions(SymbolDisplayMiscellaneousOptions.IncludeNotNullableReferenceTypeModifier))
);
}
所以我的问题是为什么在代码分析中,System.IntPtr
的特殊类型不是nin
还有,我已经经历过了。我理解“.关键字不仅仅是类型的别名。”但是为什么这是真的呢
typeof(nint)==typeof(System.IntPtr)
?nint
是一个映射到IntPtr的C#v9关键字,运行时理解的类型。就像int
映射到Int32和string
映射到string等。它确实不是像int这样简单的别名,它们删除了危险的成员,如ToInt32()、topenter(),等等。让代码分析在SpecialType中表示它是明智的,只是需要天灾才能修改。你可以在github上问他,但可能性不大。这很令人伤心,所以我想我现在能做的最好的事情就是用“replace”(“System.IntPtr”,“nint”).replace(“System.UIntPtr”,“nuint”)手动替换“System.IntPtr”和“System.uintpttr”?