Exemple #1
0
        public static object ApplyProperties(this MethodInfo method, object thisArg, IProperties properties)
        {
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            properties = properties ?? Properties.Empty;
            var methodParameters = method.GetParameters();
            var parms            = new object[methodParameters.Length];
            int paramIndex       = 0;

            if (method.IsStatic)
            {
                if (methodParameters.Length == 0)
                {
                    throw RuntimeFailure.ApplyPropertiesStaticMethodRequiresArg("method");
                }
                if (thisArg == null)
                {
                    throw new ArgumentNullException(nameof(thisArg));
                }
                parms[0]   = thisArg;
                paramIndex = 1;
                if (!methodParameters[0].ParameterType.IsInstanceOfType(thisArg))
                {
                    throw RuntimeFailure.ThisArgumentIncorrectType(thisArg.GetType());
                }
            }

            for (; paramIndex < methodParameters.Length; paramIndex++)
            {
                var pi = methodParameters[paramIndex];
                parms[paramIndex] = properties.GetProperty(pi.Name);
            }

            return(method.Invoke(thisArg, parms));
        }