/// <summary> /// Creates the cacheable method or indexer or property call. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="callInfo">The callInfo.</param> /// <param name="context">The context.</param> /// <returns></returns> public static CacheableInvocation CreateCall(InvocationKind kind, String_OR_InvokeMemberName name = null, CallInfo callInfo = null, object context = null) { var tArgCount = callInfo?.ArgumentCount ?? 0; var tArgNames = callInfo?.ArgumentNames.ToArray(); return(new CacheableInvocation(kind, name, tArgCount, tArgNames, context)); }
/// <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(), InvokeHelper.Unknown, () => binder, (InvokeMemberName)name, context, argNames, staticContext, isEvent)); }
/// <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) { target = target.GetTargetContext(out var context, out var staticContext); args = Util.GetArgsAndNames(args, out var argNames); CallSite callSite = null; return(InvokeHelper.InvokeMemberCallSite(target, (InvokeMemberName)name, args, argNames, context, staticContext, ref callSite)); }
/// <summary> /// Dynamically Invokes a member method which returns void using the DLR /// </summary> /// <param name="target">The target.</param> /// <param name="name">The name.</param> /// <param name="args">The args.</param> /// <example> /// Unit test that exhibits usage: /// <code> /// <![CDATA[ /// var tTest = "Wrong"; /// var tValue = "Correct"; /// /// dynamic tExpando = new ExpandoObject(); /// tExpando.Action = new Action<string>(it => tTest = it); /// /// Impromptu.InvokeMemberAction(tExpando, "Action", tValue); /// /// Assert.AreEqual(tValue, tTest); /// ]]> /// </code> /// </example> public static void InvokeMemberAction(object target, String_OR_InvokeMemberName name, params object[] args) { target = target.GetTargetContext(out var tContext, out var tStaticContext); args = Util.GetArgsAndNames(args, out var tArgNames); CallSite tCallSite = null; InvokeHelper.InvokeMemberActionCallSite(target, (InvokeMemberName)name, args, tArgNames, tContext, tStaticContext, ref tCallSite); }
/// <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, (InvokeMemberName)name, args, tArgNames, tContext, tStaticContext, ref tCallSite)); }
/// <summary> /// Initializes a new instance of the <see cref="CacheableInvocation"/> class. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="argCount">The arg count.</param> /// <param name="argNames">The arg names.</param> /// <param name="context">The context.</param> /// <param name="convertType">Type of the convert.</param> /// <param name="convertExplicit">if set to <c>true</c> [convert explict].</param> /// <param name="storedArgs">The stored args.</param> public CacheableInvocation(InvocationKind kind, String_OR_InvokeMemberName name=null, int argCount =0, string[] argNames =null, object context = null, Type convertType = null, bool convertExplicit = false, object[] storedArgs = null) : base(kind, name, storedArgs) { _convertType = convertType; _convertExplicit = convertExplicit; _argNames = argNames ?? new string[] {}; if (storedArgs != null) { _argCount = storedArgs.Length; string[] tArgNames; Args = Util.GetArgsAndNames(storedArgs, out tArgNames); if (_argNames.Length < tArgNames.Length) { _argNames = tArgNames; } } switch (kind) //Set required argcount values { case InvocationKind.GetIndex: if (argCount < 1) { throw new ArgumentException("Arg Count must be at least 1 for a GetIndex", "argCount"); } _argCount = argCount; break; case InvocationKind.SetIndex: if (argCount < 2) { throw new ArgumentException("Arg Count Must be at least 2 for a SetIndex", "argCount"); } _argCount = argCount; break; case InvocationKind.Convert: _argCount = 0; if(convertType==null) throw new ArgumentNullException("convertType"," Convert Requires Convert Type "); break; case InvocationKind.SubtractAssign: case InvocationKind.AddAssign: case InvocationKind.Set: _argCount = 1; break; case InvocationKind.Get: case InvocationKind.IsEvent: _argCount = 0; break; default: _argCount = Math.Max(argCount, _argNames.Length); break; } if (_argCount > 0)//setup argname array { var tBlank = new string[_argCount]; if (_argNames.Length != 0) Array.Copy(_argNames, 0, tBlank, tBlank.Length - _argNames.Length, tBlank.Length); else tBlank = null; _argNames = tBlank; } if (context != null) { #pragma warning disable 168 var tDummy = context.GetTargetContext(out _context, out _staticContext); #pragma warning restore 168 } else { _context = typeof (object); } }
/// <summary> /// Creates the cacheable method or indexer or property call. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="callInfo">The callInfo.</param> /// <param name="context">The context.</param> /// <returns></returns> public static CacheableInvocation CreateCall(InvocationKind kind, String_OR_InvokeMemberName name = null, CallInfo callInfo = null,object context = null) { var tArgCount = callInfo != null ? callInfo.ArgumentCount : 0; var tArgNames = callInfo != null ? callInfo.ArgumentNames.ToArray() : null; return new CacheableInvocation(kind, name, tArgCount, tArgNames, context); }
/// <summary> /// Creates the invocation. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="storedArgs">The args.</param> /// <returns></returns> public static Invocation Create(InvocationKind kind, String_OR_InvokeMemberName name, params object[] storedArgs) { return new Invocation(kind,name,storedArgs); }
/// <summary> /// Initializes a new instance of the <see cref="Invocation"/> class. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="storedArgs">The args.</param> public Invocation(InvocationKind kind, String_OR_InvokeMemberName name, params object[] storedArgs) { Kind = kind; Name = name; Args = storedArgs; }
/// <summary> /// Initializes a new instance of the <see cref="Invocation"/> class. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="storedArgs">The args.</param> public Invocation(InvocationKind kind, String_OR_InvokeMemberName name, params object[] storedArgs) { this.Kind = kind; this.Name = name; this.Args = storedArgs; }
/// <summary> /// Creates the invocation. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="storedArgs">The args.</param> /// <returns></returns> public static Invocation Create(InvocationKind kind, String_OR_InvokeMemberName name, params object[] storedArgs) { return(new Invocation(kind, name, storedArgs)); }
/// <summary> /// Initializes a new instance of the <see cref="CacheableInvocation"/> class. /// </summary> /// <param name="kind">The kind.</param> /// <param name="name">The name.</param> /// <param name="argCount">The arg count.</param> /// <param name="argNames">The arg names.</param> /// <param name="context">The context.</param> /// <param name="convertType">Type of the convert.</param> /// <param name="convertExplicit">if set to <c>true</c> [convert explict].</param> /// <param name="storedArgs">The stored args.</param> public CacheableInvocation(InvocationKind kind, String_OR_InvokeMemberName name = null, int argCount = 0, string[] argNames = null, object context = null, Type convertType = null, bool convertExplicit = false, object[] storedArgs = null) : base(kind, name, storedArgs) { _convertType = convertType; _convertExplicit = convertExplicit; _argNames = argNames ?? new string[] {}; if (storedArgs != null) { _argCount = storedArgs.Length; Args = Util.GetArgsAndNames(storedArgs, out var tArgNames); if (_argNames.Length < tArgNames.Length) { _argNames = tArgNames; } } switch (kind) //Set required argcount values { case InvocationKind.GetIndex: if (argCount < 1) { throw new ArgumentException("Arg Count must be at least 1 for a GetIndex", nameof(argCount)); } _argCount = argCount; break; case InvocationKind.SetIndex: if (argCount < 2) { throw new ArgumentException("Arg Count Must be at least 2 for a SetIndex", nameof(argCount)); } _argCount = argCount; break; case InvocationKind.Convert: _argCount = 0; if (convertType == null) { throw new ArgumentNullException(nameof(convertType), " Convert Requires Convert Type "); } break; case InvocationKind.SubtractAssign: case InvocationKind.AddAssign: case InvocationKind.Set: _argCount = 1; break; case InvocationKind.Get: case InvocationKind.IsEvent: _argCount = 0; break; default: _argCount = Math.Max(argCount, _argNames.Length); break; } if (_argCount > 0)//setup argName array { var tBlank = new string[_argCount]; if (_argNames.Length != 0) { Array.Copy(_argNames, 0, tBlank, tBlank.Length - _argNames.Length, tBlank.Length); } else { tBlank = null; } _argNames = tBlank; } if (context != null) { var dummy = context.GetTargetContext(out _context, out _staticContext); //lgtm [cs/useless-assignment-to-local] } else { _context = typeof(object); } }
/// <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) => InvokeHelper.CreateCallSite(delegateType, binder.GetType(), InvokeHelper.Unknown, () => binder, (InvokeMemberName)name, context, argNames, staticContext, isEvent);