Google.ProtocolBuffers.ProtoGen.GeneratorOptions.TryValidate C# (CSharp) Méthode

TryValidate() public méthode

Attempts to validate the options, but doesn't throw an exception if they're invalid. Instead, when this method returns false, the output variable will contain a collection of reasons for the validation failure.
public TryValidate ( IList &reasons ) : bool
reasons IList Variable to receive a list of reasons in case of validation failure.
Résultat bool
        public bool TryValidate(out IList<string> reasons)
        {
            List<string> tmpReasons = new List<string>();

              // Output directory validation
              if (string.IsNullOrEmpty(OutputDirectory)) {
            tmpReasons.Add("No output directory specified");
              } else {
            if (!Directory.Exists(OutputDirectory)) {
              tmpReasons.Add("Specified output directory (" + OutputDirectory + " doesn't exist.");
            }
              }

              // Input file validation (just in terms of presence)
              if (InputFiles == null || InputFiles.Count == 0) {
            tmpReasons.Add("No input files specified");
              } else {
            foreach (string input in InputFiles) {
              FileInfo fi = new FileInfo(input);
              if (!fi.Exists) {
            tmpReasons.Add("Input file " + input + " doesn't exist.");
              }
            }
              }

              if (tmpReasons.Count != 0) {
            reasons = tmpReasons;
            return false;
              }

              reasons = null;
              return true;
        }

Usage Example

        internal static void Run(CodeGeneratorRequest request, CodeGeneratorResponse.Builder response)
        {
            var arguments = new List<string>();
            foreach (var arg in request.Parameter.Split(' '))
            {
                var timmedArg = (arg ?? "").Trim();
                if (!string.IsNullOrEmpty(timmedArg))
                {
                    arguments.Add(timmedArg);
                }
            }
            // Adding fake input file to make TryValidate happy.
            arguments.Add(System.Reflection.Assembly.GetExecutingAssembly().Location);

            GeneratorOptions options = new GeneratorOptions
            {
                Arguments = arguments
            };
            IList<string> validationFailures;
            if (!options.TryValidate(out validationFailures))
            {
                response.Error += new InvalidOptionsException(validationFailures).Message;
                return;
            }

            Generator generator = Generator.CreateGenerator(options);
            generator.Generate(request, response);
        }
All Usage Examples Of Google.ProtocolBuffers.ProtoGen.GeneratorOptions::TryValidate
GeneratorOptions