Example #1
0
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;
            var tLookup = binder.Name;
            var tIndex  = Array.FindIndex(_fieldNames,
                                          it => it.Equals(tLookup, StringComparison.InvariantCultureIgnoreCase));

            if (tIndex < 0)
            {
                return(false);
            }


            result = _wrappedArray[tIndex];


            Type outType;

            if (TryTypeForName(tLookup, out outType))
            {
                result = Impromptu.CoerceConvert(result, outType);
            }

            return(true);
        }
Example #2
0
        public void TestCoerceConverterDBNULL()
        {
            var tEl = DBNull.Value;

            var tCast = Impromptu.CoerceConvert(tEl, typeof(long));

            Assert.AreEqual(typeof(long), tCast.GetType());

            var tCast2 = Impromptu.CoerceConvert(tEl, typeof(string));

            Assert.AreEqual(null, tCast2);

            Assert.AreNotEqual(null, tEl);
        }
        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)
                {
                    result = Impromptu.CoerceConvert(result, tType);
                }
            }
            else
            {
                result = null;
                if (!tTryType)
                {
                    return(false);
                }
                if (tType.GetTypeInfo().IsValueType)
                {
                    result = Impromptu.InvokeConstructor(tType);
                }
            }
            return(true);
        }
Example #4
0
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            var tLookup = binder.Name;
            var tIndex  = Array.FindIndex(_fieldNames,
                                          it => it.Equals(tLookup, StringComparison.InvariantCultureIgnoreCase));

            if (tIndex < 0)
            {
                return(false);
            }

            Type outType;

            if (TryTypeForName(tLookup, out outType))
            {
                value = Impromptu.CoerceConvert(value, outType);
            }

            _wrappedArray[tIndex] = value;

            return(true);
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var  tGroup = _match.Groups[binder.Name];
            Type outType;

            if (!TryTypeForName(binder.Name, out outType))
            {
                outType = typeof(string);
            }

            if (!tGroup.Success)
            {
                result = null;
                if (outType.GetTypeInfo().IsValueType)
                {
                    result = Impromptu.InvokeConstructor(outType);
                }
                return(true);
            }

            result = Impromptu.CoerceConvert(tGroup.Value, outType);
            return(true);
        }