private dynamic CastSchemaItemType(object arg, ValueType itemType) { if ((arg is string) && ((itemType == ValueType.Int) || itemType == ValueType.Decimal || itemType == ValueType.Long || itemType == ValueType.Double)) { if (arg.ToString().StartsWith("(") && (arg.ToString().EndsWith(")"))) { arg = arg.ToString().Replace("(", "-").Replace(")", ""); } } var type = ConvertValueType(itemType); if (type == null) { return(arg); } var argType = arg.GetType(); if (argType == type) { return(arg); } var cast = _castProvider.Value.GetCast(argType, type); return(cast != null ? cast(arg) : Impromptu.InvokeConvert(arg, type, true)); }
public void TestImplicitConvert() { var tEl = 45; var tCast = Impromptu.InvokeConvert(tEl, typeof(long)); Assert.AreEqual(typeof(long), tCast.GetType()); }
public void TestConvert() { var tEl = new XElement("Test", "45"); var tCast = Impromptu.InvokeConvert(tEl, typeof(int), @explicit: true); Assert.AreEqual(typeof(int), tCast.GetType()); Assert.AreEqual(45, tCast); }
/// <summary> /// Forwards the convert operation. /// </summary> /// <param name="binder">the binder</param> /// <param name="result">the result</param> /// <returns>true when successfull</returns> public override bool TryConvert(System.Dynamic.ConvertBinder binder, out object result) { result = null; try { result = Impromptu.InvokeConvert(CallTarget, binder.Type, binder.Explicit); return(true); } catch (RuntimeBinderException) { return(false); } }
public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) { object[] tArgs = args; if (_overloadTypes.ContainsKey(args.Length)) { tArgs = _overloadTypes[args.Length].Zip(args, Tuple.Create) .Select(it => it.Item2 != null ? Impromptu.InvokeConvert(it.Item2, it.Item1, @explicit: true) : null).ToArray(); } var name = InvokeMemberName.Create(_name, _genericMethodParameters); result = _parent.InvokeStaticMethod(name, tArgs); return(true); }
/// <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); } }
internal static bool MassageResultBasedOnInterface(this ImpromptuObject target, string binderName, bool resultFound, ref object result) { if (result is ImpromptuForwarderAddRemove) //Don't massage AddRemove Proxies { return(true); } Type tType; var tTryType = target.TryTypeForName(binderName, out tType); if (tTryType && tType == typeof(void)) { return(true); } if (resultFound) { if (result is IDictionary <string, object> && !(result is ImpromptuDictionaryBase) && (!tTryType || tType == typeof(object))) { result = new ImpromptuDictionary((IDictionary <string, object>)result); } else if (tTryType) { if (result != null && !tType.IsAssignableFrom(result.GetType())) { if (tType.IsInterface) { if (result is IDictionary <string, object> && !(result is ImpromptuDictionaryBase)) { result = new ImpromptuDictionary((IDictionary <string, object>)result); } else { result = new ImpromptuGet(result); } result = Impromptu.DynamicActLike(result, tType); } else { try { object tResult; tResult = Impromptu.InvokeConvert(target, tType, explict: true); result = tResult; } catch (RuntimeBinderException) { Type tReducedType = tType; if (tType.IsGenericType && tType.GetGenericTypeDefinition().Equals(typeof(Nullable <>))) { tReducedType = tType.GetGenericArguments().First(); } if (result is IConvertible && typeof(IConvertible).IsAssignableFrom(tReducedType)) { result = Convert.ChangeType(result, tReducedType, Thread.CurrentThread.CurrentCulture); } else { //finally check type converter since it's the slowest. #if !SILVERLIGHT var tConverter = TypeDescriptor.GetConverter(tType); #else TypeConverter tConverter = null; var tAttributes = tType.GetCustomAttributes(typeof(TypeConverterAttribute), false); var tAttribute = tAttributes.OfType <TypeConverterAttribute>().FirstOrDefault(); if (tAttribute != null) { tConverter = Impromptu.InvokeConstructor(Type.GetType(tAttribute.ConverterTypeName)); } #endif if (tConverter != null && tConverter.CanConvertFrom(result.GetType())) { result = tConverter.ConvertFrom(result); } #if SILVERLIGHT else if (result is string) { var tDC = new SilverConvertertDC(result as String); var tFE = new SilverConverterFE { DataContext = tDC }; var tProp = SilverConverterFE.GetProperty(tType); tFE.SetBinding(tProp, new System.Windows.Data.Binding("StringValue")); var tResult = tFE.GetValue(tProp); if (tResult != null) { result = tResult; } } #endif } } } } else if (result == null && tType.IsValueType) { result = Impromptu.InvokeConstructor(tType); } } } else { result = null; if (!tTryType) { return(false); } if (tType.IsValueType) { result = Impromptu.InvokeConstructor(tType); } } return(true); }
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result) { result = new Invoker(_name, _genericParams, _genericMethodParameters, _parent, indexes.Select(it => Impromptu.InvokeConvert(it, typeof(Type), @explicit: true)).Cast <Type>().ToArray()); return(true); }