Example #1
0
        private Dictionary <string, ValidationMethodPair> LoadValidationAssemblies(IEnumerable <string> validatorPaths)
        {
            var ruleToMethodMap = new Dictionary <string, ValidationMethodPair>();

            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

            foreach (string validatorPath in validatorPaths)
            {
                Assembly assembly = null;

                if (_fileSystem.FileExists(validatorPath))
                {
                    try
                    {
                        assemblyBaseFolder = Path.GetDirectoryName(validatorPath);
                        assembly           = _fileSystem.AssemblyLoadFrom(validatorPath);
                    }
                    catch (ReflectionTypeLoadException)
                    {
                        // TODO log something here.
                    }

                    if (assembly == null)
                    {
                        continue;
                    }

                    foreach (Type type in assembly.GetTypes())
                    {
                        string typeName = type.Name;

                        if (!typeName.EndsWith("Validator") || typeName.Equals("Validator"))
                        {
                            continue;
                        }

                        MethodInfo isValidStatic = type.GetMethod(
                            "IsValidStatic",
                            new[]
                        {
                            typeof(string).MakeByRefType(),                      // Matched pattern.
                            typeof(Dictionary <string, string>).MakeByRefType(), // Regex groups.
                            typeof(string).MakeByRefType(),                      // FailureLevel.
                            typeof(string).MakeByRefType(),                      // Fingerprint.
                            typeof(string).MakeByRefType(),                      // Message.
                        },
                            null);

                        if (isValidStatic == null || isValidStatic?.ReturnType != typeof(string))
                        {
                            continue;
                        }

                        MethodInfo isValidDynamic = type.GetMethod(
                            "IsValidDynamic",
                            new[]
                        {
                            typeof(string).MakeByRefType(),     // Fingerprint.
                            typeof(string).MakeByRefType(),     // Message.
                        },
                            null);

                        if (isValidDynamic?.ReturnType != typeof(string))
                        {
                            isValidDynamic = null;
                        }

                        ruleToMethodMap[typeName] = new ValidationMethodPair
                        {
                            IsValidStatic  = isValidStatic,
                            IsValidDynamic = isValidDynamic,
                        };
                    }
                }
            }

            return(ruleToMethodMap);
        }