public void TestSetIndexer() { dynamic tSetValue = "3"; var tAnon = new List <string> { "1", "2" }; Impromptu.InvokeSetIndex(tAnon, 0, tSetValue); Assert.AreEqual(tSetValue, tAnon[0]); }
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value) { if (CallTarget == null) { return(false); } var tCombinedArgs = indexes.Concat(new[] { value }).ToArray(); object[] tArgs = Util.NameArgsIfNecessary(binder.CallInfo, tCombinedArgs); Impromptu.InvokeSetIndex(CallTarget, tArgs); return(true); }
public override object Invoke(object target, params object[] args) { var tIContext = target as InvokeContext; if (tIContext != null) { target = tIContext.Target; } if (args == null) { args = new object[] { null }; } if (args.Length != _argCount) { switch (Kind) { case InvocationKind.Convert: if (args.Length > 0) { if (!Equals(args[0], _convertType)) { throw new ArgumentException("CacheableInvocation can't change conversion type on invoke.", "args"); } } if (args.Length > 1) { if (!Equals(args[1], _convertExplicit)) { throw new ArgumentException("CacheableInvocation can't change explict/implict conversion on invoke.", "args"); } } if (args.Length > 2) { goto default; } break; default: throw new ArgumentException("args", string.Format("Incorrect number of Arguments for CachedInvocation, Expected:{0}", _argCount)); } } switch (Kind) { case InvocationKind.Constructor: var tTarget = (Type)target; return(InvokeHelper.InvokeConstructorCallSite(tTarget, tTarget.IsValueType, args, _argNames, ref _callSite)); case InvocationKind.Convert: return(InvokeHelper.InvokeConvertCallSite(target, _convertExplicit, _convertType, _context, ref _callSite)); case InvocationKind.Get: return(InvokeHelper.InvokeGetCallSite(target, Name.Name, _context, _staticContext, ref _callSite)); case InvocationKind.Set: InvokeHelper.InvokeSetCallSite(target, Name.Name, args[0], _context, _staticContext, ref _callSite); return(null); case InvocationKind.GetIndex: return(InvokeHelper.InvokeGetIndexCallSite(target, args, _argNames, _context, _staticContext, ref _callSite)); case InvocationKind.SetIndex: Impromptu.InvokeSetIndex(target, args); return(null); case InvocationKind.InvokeMember: return(InvokeHelper.InvokeMemberCallSite(target, Name, args, _argNames, _context, _staticContext, ref _callSite)); case InvocationKind.InvokeMemberAction: InvokeHelper.InvokeMemberActionCallSite(target, Name, args, _argNames, _context, _staticContext, ref _callSite); return(null); case InvocationKind.InvokeMemberUnknown: { try { var tObj = InvokeHelper.InvokeMemberCallSite(target, Name, args, _argNames, _context, _staticContext, ref _callSite); return(tObj); } catch (RuntimeBinderException) { InvokeHelper.InvokeMemberActionCallSite(target, Name, args, _argNames, _context, _staticContext, ref _callSite2); return(null); } } case InvocationKind.Invoke: return(InvokeHelper.InvokeDirectCallSite(target, args, _argNames, _context, _staticContext, ref _callSite)); case InvocationKind.InvokeAction: InvokeHelper.InvokeDirectActionCallSite(target, args, _argNames, _context, _staticContext, ref _callSite); return(null); case InvocationKind.InvokeUnknown: { try { var tObj = InvokeHelper.InvokeDirectCallSite(target, args, _argNames, _context, _staticContext, ref _callSite); return(tObj); } catch (RuntimeBinderException) { InvokeHelper.InvokeDirectActionCallSite(target, args, _argNames, _context, _staticContext, ref _callSite2); return(null); } } case InvocationKind.AddAssign: InvokeHelper.InvokeAddAssignCallSite(target, Name.Name, args, _argNames, _context, _staticContext, ref _callSite, ref _callSite2, ref _callSite3, ref _callSite4); return(null); case InvocationKind.SubtractAssign: InvokeHelper.InvokeSubtractAssignCallSite(target, Name.Name, args, _argNames, _context, _staticContext, ref _callSite, ref _callSite2, ref _callSite3, ref _callSite4); return(null); case InvocationKind.IsEvent: return(InvokeHelper.InvokeIsEventCallSite(target, Name.Name, _context, ref _callSite)); default: throw new InvalidOperationException("Unknown Invocation Kind: " + Kind); } }
/// <summary> /// Invokes the invocation on specified target with specific args. /// </summary> /// <param name="target">The target.</param> /// <param name="args">The args.</param> /// <returns></returns> public virtual object Invoke(object target, params object[] args) { switch (Kind) { case InvocationKind.Constructor: return(Impromptu.InvokeConstructor((Type)target, args)); case InvocationKind.Convert: bool tExplict = false; if (Args.Length == 2) { tExplict = (bool)args[1]; } return(Impromptu.InvokeConvert(target, (Type)args[0], tExplict)); case InvocationKind.Get: return(Impromptu.InvokeGet(target, Name.Name)); case InvocationKind.Set: Impromptu.InvokeSet(target, Name.Name, args.FirstOrDefault()); return(null); case InvocationKind.GetIndex: return(Impromptu.InvokeGetIndex(target, args)); case InvocationKind.SetIndex: Impromptu.InvokeSetIndex(target, args); return(null); case InvocationKind.InvokeMember: return(Impromptu.InvokeMember(target, Name, args)); case InvocationKind.InvokeMemberAction: Impromptu.InvokeMemberAction(target, Name, args); return(null); case InvocationKind.InvokeMemberUnknown: { try { return(Impromptu.InvokeMember(target, Name, args)); } catch (RuntimeBinderException) { Impromptu.InvokeMemberAction(target, Name, args); return(null); } } case InvocationKind.Invoke: return(Impromptu.Invoke(target, args)); case InvocationKind.InvokeAction: Impromptu.InvokeAction(target, args); return(null); case InvocationKind.InvokeUnknown: { try { return(Impromptu.Invoke(target, args)); } catch (RuntimeBinderException) { Impromptu.InvokeAction(target, args); return(null); } } case InvocationKind.AddAssign: Impromptu.InvokeAddAssignMember(target, Name.Name, args.FirstOrDefault()); return(null); case InvocationKind.SubtractAssign: Impromptu.InvokeSubtractAssignMember(target, Name.Name, args.FirstOrDefault()); return(null); case InvocationKind.IsEvent: return(Impromptu.InvokeIsEvent(target, Name.Name)); default: throw new InvalidOperationException("Unknown Invocation Kind: " + Kind); } }