Google.VersionHandler.InvokeMethod C# (CSharp) Method

InvokeMethod() public static method

Call a method on an object with named arguments.
public static InvokeMethod ( Type type, object objectInstance, string methodName, object args, object>.Dictionary namedArgs = null ) : object
type System.Type Class to call the method on.
objectInstance object Object to call a method on.
methodName string Name of the method to call.
args object
namedArgs object>.Dictionary Named arguments of the method.
return object
    public static object InvokeMethod(
            Type type, object objectInstance, string methodName,
            object[] args, Dictionary<string, object> namedArgs = null) {
        MethodInfo method = type.GetMethod(methodName);
        ParameterInfo[] parameters = method.GetParameters();
        int numParameters = parameters.Length;
        object[] parameterValues = new object[numParameters];
        int numPositionalArgs = args != null ? args.Length : 0;
        foreach (var parameter in parameters) {
            int position = parameter.Position;
            if (position < numPositionalArgs) {
                parameterValues[position] = args[position];
                continue;
            }
            object namedValue = parameter.RawDefaultValue;
            if (namedArgs != null) {
                object overrideValue;
                if (namedArgs.TryGetValue(parameter.Name, out overrideValue)) {
                    namedValue = overrideValue;
                }
            }
            parameterValues[position] = namedValue;
        }
        return method.Invoke(objectInstance, parameterValues);
    }
}

Usage Example

Beispiel #1
0
 public static object InvokeStaticMethod(Type type, string methodName, object[] args, Dictionary <string, object> namedArgs = null)
 {
     return(VersionHandler.InvokeMethod(type, null, methodName, args, namedArgs));
 }
All Usage Examples Of Google.VersionHandler::InvokeMethod