/// <summary> /// Invoke the best available match for the supplied parameters. If no method can be called /// using the supplied parameters, an exception is thrown. /// </summary> /// <param name="obj">The object on which to invoke a method.</param> /// <param name="mustUseAllParameters">Specifies whether all supplied parameters must be used in the /// invocation. Unless you know what you are doing you should pass true for this parameter.</param> /// <param name="sample">The object whose public properties will be used as parameters.</param> /// <returns>The return value of the invocation.</returns> public object Invoke(object obj, bool mustUseAllParameters, object sample) { if (obj == null || sample == null) { throw new ArgumentException("Missing or invalid argument: " + (obj == null ? "obj" : "sample")); } SourceInfo sourceInfo = SourceInfo.CreateFromType(sample.GetType()); // check to see if we already have a map for best match MethodMap map = mapCache.Get(sourceInfo); object[] values = sourceInfo.GetParameterValues(sample); if (map == null) { string[] names = sourceInfo.ParamNames; Type[] types = sourceInfo.ParamTypes; if (names.Length != values.Length || names.Length != types.Length) { throw new ArgumentException("Mismatching name, type and value arrays (must be of identical length)."); } map = MapFactory.DetermineBestMethodMatch(methodPool, mustUseAllParameters, names, types, values); mapCache.Insert(sourceInfo, map); } bool isStatic = obj is Type; return(isStatic ? map.Invoke(values) : map.Invoke(obj, values)); }
/// <summary> /// Obtains a list of all methods with the given <paramref name="methodName"/> on the given /// <paramref name="obj" />, and invokes the best match for the parameters obtained from the /// public properties of the supplied <paramref name="sample"/> object. /// TryCallMethod is very liberal and attempts to convert values that are not otherwise /// considered compatible, such as between strings and enums or numbers, Guids and byte[16], etc. /// </summary> /// <returns>The result of the invocation.</returns> public static object TryCallMethod( this object obj, string methodName, bool mustUseAllParameters, object sample ) { Type sourceType = sample.GetType(); var sourceInfo = new SourceInfo( sourceType ); var paramValues = sourceInfo.GetParameterValues( sample ); return obj.TryCallMethod( methodName, mustUseAllParameters, sourceInfo.ParamNames, sourceInfo.ParamTypes, paramValues ); }
/// <summary> /// Invoke the best available match for the supplied parameters. /// If no method can be called using the supplied parameters, an exception is thrown. /// </summary> /// <param name="obj">The object on which to invoke a method.</param> /// <param name="mustUseAllParameters">Specifies whether all supplied parameters must be used in the /// invocation. Unless you know what you are doing you should pass true for this parameter.</param> /// <param name="sample">The object whose public properties will be used as parameters.</param> /// <returns>The return value of the invocation.</returns> public object Invoke( object obj, bool mustUseAllParameters, object sample ) { Type sourceType = sample.GetType(); var sourceInfo = new SourceInfo( sourceType ); bool isStatic = obj is Type; string[] names = sourceInfo.ParamNames; Type[] types = sourceInfo.ParamTypes; object[] values = sourceInfo.GetParameterValues( sample ); if( names.Length != values.Length || names.Length != types.Length ) { throw new ArgumentException( "Mismatching name, type and value arrays (must be of identical length)." ); } MethodMap map = MapFactory.DetermineBestMethodMatch( methodPool, mustUseAllParameters, names, types, values ); return isStatic ? map.Invoke( values ) : map.Invoke( obj, values ); }
/// <summary> /// Invoke the best available match for the supplied parameters. /// If no method can be called using the supplied parameters, an exception is thrown. /// </summary> /// <param name="obj">The object on which to invoke a method.</param> /// <param name="mustUseAllParameters">Specifies whether all supplied parameters must be used in the /// invocation. Unless you know what you are doing you should pass true for this parameter.</param> /// <param name="sample">The object whose public properties will be used as parameters.</param> /// <returns>The return value of the invocation.</returns> public object Invoke(object obj, bool mustUseAllParameters, object sample) { Type sourceType = sample.GetType(); var sourceInfo = new SourceInfo(sourceType); bool isStatic = obj is Type; string[] names = sourceInfo.ParamNames; Type[] types = sourceInfo.ParamTypes; object[] values = sourceInfo.GetParameterValues(sample); if (names.Length != values.Length || names.Length != types.Length) { throw new ArgumentException("Mismatching name, type and value arrays (must be of identical length)."); } MethodMap map = MapFactory.DetermineBestMethodMatch(methodPool, mustUseAllParameters, names, types, values); return(isStatic ? map.Invoke(values) : map.Invoke(obj, values)); }