gpt4 book ai didi

c# - 使用 roslyn 确定基元类型

转载 作者:行者123 更新时间:2023-11-30 17:45:57 31 4
gpt4 key购买 nike

使用 Roslyn,如何确定 ITypeSymbol 是否为原始类型?

ITypeSymbol 没有 IsPrimitive 属性,如 Type.IsPrimitive

有没有办法将 ITypeSymbol 转换为 Type 或其他一些方法来确定 ITypeSymbol 是否为原始类型?

最佳答案

看起来像确定 TypedConstant class 中的代码,但它是内部的,我找不到可以很好地让我到达那里的公共(public) API。我最终将该方法复制到我的项目中。

    /// <remarks>
/// TypedConstant isn't computing its own kind from the type symbol because it doesn't
/// have a way to recognize the well-known type System.Type.
/// </remarks>
internal static TypedConstantKind GetTypedConstantKind(ITypeSymbol type, Compilation compilation)
{
Debug.Assert(type != null);

switch (type.SpecialType)
{
case SpecialType.System_Boolean:
case SpecialType.System_SByte:
case SpecialType.System_Int16:
case SpecialType.System_Int32:
case SpecialType.System_Int64:
case SpecialType.System_Byte:
case SpecialType.System_UInt16:
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
case SpecialType.System_Single:
case SpecialType.System_Double:
case SpecialType.System_Char:
case SpecialType.System_String:
case SpecialType.System_Object:
return TypedConstantKind.Primitive;
default:
switch (type.TypeKind)
{
case TypeKind.Array:
return TypedConstantKind.Array;
case TypeKind.Enum:
return TypedConstantKind.Enum;
case TypeKind.Error:
return TypedConstantKind.Error;
}

if (compilation != null &&
compilation.IsSystemTypeReference(type))
{
return TypedConstantKind.Type;
}

return TypedConstantKind.Error;
}
}

关于c# - 使用 roslyn 确定基元类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958069/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com