示例#1
0
    private static bool ValidateBaseEnum(
        SymbolAnalysisContext context,
        INamedTypeSymbol enumType,
        AttributeData?enumAttribute,
        Location location,
        bool isValidatable)
    {
        if (!enumType.BaseType.IsEnum(out var baseEnumInterfaces))
        {
            return(false);
        }

        var isBaseEnumValidatable = baseEnumInterfaces.GetValidEnumInterface(enumType.BaseType)?.IsValidatableEnumInterface() ?? false;

        if (isValidatable != isBaseEnumValidatable)
        {
            ReportDiagnostic(context, DiagnosticsDescriptors.ExtendedEnumCannotBeValidatableIfBaseEnumIsNot, location, enumType);
        }

        var baseEnumAttr         = enumType.BaseType.FindEnumGenerationAttribute();
        var isBaseEnumExtensible = baseEnumAttr?.IsExtensible() ?? false;

        if (!isBaseEnumExtensible)
        {
            ReportDiagnostic(context, DiagnosticsDescriptors.BaseEnumMustBeExtensible, location, enumType.BaseType);
        }

        var baseKeyPropName = baseEnumAttr?.FindKeyPropertyName();
        var enumKeyPropName = enumAttribute?.FindKeyPropertyName();

        if (!String.IsNullOrWhiteSpace(baseKeyPropName) &&
            !String.IsNullOrWhiteSpace(enumKeyPropName) &&
            baseKeyPropName != enumKeyPropName)
        {
            ReportDiagnostic(context, DiagnosticsDescriptors.ExtendedEnumMustHaveSameKeyPropertyName, location, enumType, enumType.BaseType);
        }

        return(true);
    }
 public EnumSettings(AttributeData?attribute)
 {
     KeyComparer     = attribute?.FindKeyComparer().TrimAndNullify();
     KeyPropertyName = attribute?.FindKeyPropertyName().TrimAndNullify();
     IsExtensible    = attribute?.IsExtensible() ?? false;
 }