private static SyntaxNode CreateNonFlagsEnumConstantValue(INamedTypeSymbol enumType, object constantValue) { var underlyingSpecialType = enumType.EnumUnderlyingType.SpecialType; var constantValueULong = EnumUtilities.ConvertEnumUnderlyingTypeToUInt64(constantValue, underlyingSpecialType); // See if there's a member with this value. If so, then use that. foreach (var member in enumType.GetMembers()) { if (member.Kind == SymbolKind.Field) { var field = (IFieldSymbol)member; if (field.HasConstantValue) { var fieldValue = EnumUtilities.ConvertEnumUnderlyingTypeToUInt64(field.ConstantValue, underlyingSpecialType); if (constantValueULong == fieldValue) { return(CreateMemberAccessExpression(field, enumType, underlyingSpecialType)); } } } } // Otherwise, just add the enum as a literal. return(CreateExplicitlyCastedLiteralValue(enumType, underlyingSpecialType, constantValue)); }
private static SyntaxNode CreateExplicitlyCastedLiteralValue( INamedTypeSymbol enumType, SpecialType underlyingSpecialType, object constantValue) { var expression = ExpressionGenerator.GenerateNonEnumValueExpression( enumType.EnumUnderlyingType, constantValue, true); var constantValueULong = EnumUtilities.ConvertEnumUnderlyingTypeToUInt64(constantValue, underlyingSpecialType); if (constantValueULong == 0) { // 0 is always convertible to an enum type without needing a cast. return(expression); } return(SyntaxFactory.CastExpression((TypeSyntax)enumType, expression.Parenthesize())); }