Ejemplo n.º 1
0
 private void GenerateValidator(StringBuilder sb, EnumValidationInfo info, string indent)
 {
     sb.AppendLine($"{indent}int intValue = (int)enumToValidate;");
     foreach ((int min, int max) in GetElementSets(info.Elements))
     {
         sb.AppendLine($"{indent}if (intValue >= {min} && intValue <= {max}) return;");
     }
     sb.AppendLine($"{indent}throw new System.ComponentModel.InvalidEnumArgumentException(\"{info.ArgumentName}\", intValue, typeof({info.EnumType}));");
 }
        private static void GenerateFlagsValidationMethodBody(GeneratorExecutionContext context, StringBuilder sb, EnumValidationInfo info, string indent)
        {
            int total = 0;

            foreach (int value in info.Values)
            {
                total |= value;
            }
            sb.AppendLine($"{indent}if ((intValue & {total}) == intValue) return;");
        }
 private static void GenerateValidateMethodBody(GeneratorExecutionContext context, StringBuilder sb, EnumValidationInfo info, string indent)
 {
     sb.AppendLine($"{indent}int intValue = (int)enumToValidate;");
     if (info.IsFlags)
     {
         GenerateFlagsValidationMethodBody(context, sb, info, indent);
     }
     else
     {
         GenerateSequenceValidationMethodBody(context, sb, info, indent);
     }
     sb.AppendLine($"{indent}throw new System.ComponentModel.InvalidEnumArgumentException(parameterName, intValue, typeof({info.EnumType}));");
 }
 private static void GenerateSequenceValidationMethodBody(GeneratorExecutionContext context, StringBuilder sb, EnumValidationInfo info, string indent)
 {
     foreach ((int min, int max) in GetElementSets(context, info.Values))
     {
         if (min == max)
         {
             sb.AppendLine($"{indent}if (intValue == {min}) return;");
         }
         else
         {
             sb.AppendLine($"{indent}if (intValue >= {min} && intValue <= {max}) return;");
         }
     }
 }