///<summary> /// Dynamically Invokes a member method using the DLR ///</summary> ///<param name="target"> The target. </param> ///<param name="name"> The name. Can be a string it will be implicitly converted </param> ///<param name="args"> The args. </param> ///<returns> The result </returns> ///<example> /// Unit test that exhibits usage: <code> /// <![CDATA[ /// dynamic tExpando = new ExpandoObject(); /// tExpando.Func = new Func<int, string>(it => it.ToString()); /// /// var tValue = 1; /// var tOut = Impromptu.InvokeMember(tExpando, "Func", tValue); /// /// Assert.AreEqual(tValue.ToString(), tOut); /// ]]> /// </code> ///</example> public static dynamic InvokeMember(object target, String_OR_InvokeMemberName name, params object[] args) { string[] tArgNames; Type tContext; bool tStaticContext; target = target.GetTargetContext(out tContext, out tStaticContext); args = Util.GetArgsAndNames(args, out tArgNames); CallSite tCallSite = null; return(InvokeHelper.InvokeMemberCallSite(target, name, args, tArgNames, tContext, tStaticContext, ref tCallSite)); }
/// <summary> /// Creates the call site. /// </summary> /// <typeparam name="T"> </typeparam> /// <param name="binder"> The binder. </param> /// <param name="name"> The name. </param> /// <param name="context"> The context. </param> /// <param name="argNames"> The arg names. </param> /// <param name="staticContext"> if set to <c>true</c> [static context]. </param> /// <param name="isEvent"> if set to <c>true</c> [is event]. </param> /// <returns> </returns> /// /// /// <example> /// Unit test that exhibits usage <code><![CDATA[ /// string tResult = String.Empty; /// var tPoco = new MethOutPoco(); /// var tBinder = /// Binder.InvokeMember(BinderFlags.None, "Func", null, GetType(), /// new[] /// { /// Info.Create( /// InfoFlags.None, null), /// Info.Create( /// InfoFlags.IsOut | /// InfoFlags.UseCompileTimeType, null) /// }); /// var tSite = Impromptu.CreateCallSite<DynamicTryString>(tBinder); /// tSite.Target.Invoke(tSite, tPoco, out tResult); /// Assert.AreEqual("success", tResult); /// ]]></code> /// </example> /// <seealso cref="CreateCallSite" /> public static CallSite <T> CreateCallSite <T>(CallSiteBinder binder, String_OR_InvokeMemberName name, Type context, string[] argNames = null, bool staticContext = false, bool isEvent = false) where T : class { return(InvokeHelper.CreateCallSite <T>(binder.GetType(), () => binder, name, context, argNames, staticContext, isEvent)); }
/// <summary> /// Creates a cached call site at runtime. /// </summary> /// <param name="delegateType"> Type of the delegate. </param> /// <param name="binder"> The CallSite binder. </param> /// <param name="name"> Member Name </param> /// <param name="context"> Permissions Context type </param> /// <param name="argNames"> The arg names. </param> /// <param name="staticContext"> if set to <c>true</c> [static context]. </param> /// <param name="isEvent"> if set to <c>true</c> [is event]. </param> /// <returns> The CallSite </returns> /// <remarks> /// Advanced usage only for serious custom dynamic invocation. /// </remarks> /// <seealso cref="CreateCallSite{T}" /> public static CallSite CreateCallSite(Type delegateType, CallSiteBinder binder, String_OR_InvokeMemberName name, Type context, string[] argNames = null, bool staticContext = false, bool isEvent = false) { return(InvokeHelper.CreateCallSite(delegateType, binder.GetType(), () => binder, name, context, argNames, staticContext, isEvent)); }