/// <summary> /// Invokes a static factory method. /// </summary> /// <param name="objectType">Business class where the factory is defined.</param> /// <param name="method">Name of the factory method</param> /// <param name="parameters">Parameters passed to factory method.</param> /// <returns>Result of the factory method invocation.</returns> public static object CallFactoryMethod(Type objectType, string method, params object[] parameters) { object returnValue; System.Reflection.MethodInfo factory = objectType.GetMethod( method, factoryFlags, null, MethodCaller.GetParameterTypes(parameters), null); if (factory == null) { // strongly typed factory couldn't be found // so find one with the correct number of // parameters int parameterCount = parameters.Length; System.Reflection.MethodInfo[] methods = objectType.GetMethods(factoryFlags); foreach (System.Reflection.MethodInfo oneMethod in methods) { if (oneMethod.Name == method && oneMethod.GetParameters().Length == parameterCount) { factory = oneMethod; break; } } } if (factory == null) { // no matching factory could be found // so throw exception throw new InvalidOperationException( string.Format(Resources.NoSuchFactoryMethod, method)); } try { returnValue = factory.Invoke(null, parameters); } catch (Exception ex) { Exception inner = null; if (ex.InnerException == null) { inner = ex; } else { inner = ex.InnerException; } throw new CallMethodException(factory.Name + " " + Resources.MethodCallFailed, inner); } return(returnValue); }
/// <summary> /// Invokes a method using the await keyword /// if the method returns Task, /// otherwise synchronously invokes the method. /// </summary> /// <param name="methodName">Name of the method.</param> /// <param name="parameters"> /// Parameters to pass to method. /// </param> public async Task CallMethodTryAsync(string methodName, params object[] parameters) { try { await MethodCaller.CallMethodTryAsync(this.Instance, methodName, parameters); } catch (CallMethodException) { throw; } catch (Exception ex) { throw new CallMethodException(Instance.GetType().Name + "." + methodName + " " + Resources.MethodCallFailed, ex); } }
// /// <summary> // /// Gets a value indicating whether the specified method // /// returns a Task of object. // /// </summary> // /// <param name="methodName">Name of the method.</param> // /// <returns>True if the method returns a Task of object.</returns> // public bool IsMethodAsync(string methodName) // { // var info = this.Instance.GetType().GetMethod(methodName); //#if NETFX_CORE // var isgeneric = info.ReturnType.IsGenericType(); //#else // var isgeneric = info.ReturnType.IsGenericType; //#endif // return (info.ReturnType.Equals(typeof(Task))); // } ///// <summary> ///// Uses reflection to dynamically invoke a method, ///// throwing an exception if it is not ///// implemented on the target object. ///// </summary> ///// <param name="method"> ///// Name of the method. ///// </param> //public async Task CallMethodAsync(string method) //{ // try // { // await (Task)MethodCaller.CallMethod(this.Instance, method); // } // catch (InvalidCastException ex) // { // throw new NotSupportedException( // string.Format(Resources.TaskOfObjectException, this.Instance.GetType().Name + "." + method), // ex); // } //} ///// <summary> ///// Uses reflection to dynamically invoke a method, ///// throwing an exception if it is not ///// implemented on the target object. ///// </summary> ///// <param name="method"> ///// Name of the method. ///// </param> ///// <param name="parameters"> ///// Parameters to pass to method. ///// </param> //public async Task CallMethodAsync(string method, params object[] parameters) //{ // try // { // await (Task)MethodCaller.CallMethod(this.Instance, method, parameters); // } // catch (InvalidCastException ex) // { // throw new NotSupportedException( // string.Format(Resources.TaskOfObjectException, this.Instance.GetType().Name + "." + method), // ex); // } //} /// <summary> /// Invokes a method using the await keyword /// if the method returns Task, /// otherwise synchronously invokes the method. /// </summary> /// <param name="methodName">Name of the method.</param> /// <returns></returns> public async Task CallMethodTryAsync(string methodName) { try { await MethodCaller.CallMethodTryAsync(this.Instance, methodName); } catch (Csla.Reflection.CallMethodException) { throw; } catch (Exception ex) { throw new Csla.Reflection.CallMethodException(Instance.GetType().Name + "." + methodName + " " + Resources.MethodCallFailed, ex); } }
/// <summary> /// Uses reflection to dynamically invoke a method, /// throwing an exception if it is not /// implemented on the target object. /// </summary> /// <param name="method"> /// Name of the method. /// </param> public object CallMethod(string method) { return(MethodCaller.CallMethod(this.Instance, method)); }
/// <summary> /// Uses reflection to dynamically invoke a method /// if that method is implemented on the target object. /// </summary> /// <param name="method"> /// Name of the method. /// </param> /// <param name="parameters"> /// Parameters to pass to method. /// </param> public object CallMethodIfImplemented(string method, params object[] parameters) { return(MethodCaller.CallMethodIfImplemented(this.Instance, method, parameters)); }
/// <summary> /// Creates an instance of the specified /// type and contains it within a new /// LateBoundObject. /// </summary> /// <param name="objectType"> /// Type of object to create. /// </param> /// <remarks> /// The specified type must implement a /// default constructor. /// </remarks> public LateBoundObject(Type objectType) : this(MethodCaller.CreateInstance(objectType)) { }