/// <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> /// 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> internal static object TryCallMethod(this object obj, string methodName, bool mustUseAllParameters, object sample) { Type sourceType = sample.GetType(); SourceInfo sourceInfo = SourceInfo.CreateFromType(sourceType); object[] paramValues = sourceInfo.GetParameterValues(sample); return(obj.TryCallMethod(methodName, mustUseAllParameters, sourceInfo.ParamNames, sourceInfo.ParamTypes, paramValues)); }
/// <summary> /// Creates an instance of the given <paramref name="type"/> using the public properties of the /// supplied <paramref name="sample"/> object as input. /// This method will try to determine the least-cost route to constructing the instance, which /// implies mapping as many properties as possible to constructor parameters. Remaining properties /// on the source are mapped to properties on the created instance or ignored if none matches. /// TryCreateInstance 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[], etc. /// </summary> /// <returns>An instance of <paramref name="type"/>.</returns> public static object TryCreateInstance(this Type type, object sample) { Type sourceType = sample.GetType(); SourceInfo sourceInfo = sourceInfoCache.Get(sourceType); if (sourceInfo == null) { sourceInfo = SourceInfo.CreateFromType(sourceType); sourceInfoCache.Insert(sourceType, sourceInfo); } object[] paramValues = sourceInfo.GetParameterValues(sample); MethodMap map = MapFactory.PrepareInvoke(type, sourceInfo.ParamNames, sourceInfo.ParamTypes, paramValues); return(map.Invoke(paramValues)); }