Ejemplo n.º 1
0
        public T GetIndexAs <T> (object o, string key)
        {
            Stats.Increment(StatsCounter.GetIndexBinderInvoked);
            Stats.Increment(StatsCounter.GetIndexBinder_Key_Invoked);

            // handle dictionaries
            var dict = o as IDictionary;

            if (dict != null)
            {
                Stats.Increment(StatsCounter.GetIndexBinder_Key_Dictionary_Invoked);

                var ro = dict[key];
                if (ro is T)
                {
                    return((T)ro);
                }
                else
                {
                    return(Dynamic.ConvertValue <T>(ro));
                }
            }

            // fallback on getmemberbinder to do the hard work
            Stats.Increment(StatsCounter.GetIndexBinder_Key_Property_Invoked);

            // create a get member binder here
            if (mGetMember == null)
            {
                mGetMember = new PSGetMember(key);
            }

            // get member value
            return(mGetMember.GetNamedMember <T>(o, key));
        }
Ejemplo n.º 2
0
        public T GetIndexAs <T> (object o, string key)
        {
            Stats.Increment(StatsCounter.GetIndexBinderInvoked);
            Stats.Increment(StatsCounter.GetIndexBinder_Key_Invoked);

            // get accessor for value type T
            var accessor = o as IDynamicAccessor <T>;

            if (accessor != null)
            {
                if (key != mLastKey)
                {
                    mLastHint = 0;
                }
                mLastKey = key;
                return(accessor.GetMember(key, ref mLastHint));
            }

            // fallback on object accessor and cast it to T
            var untypedAccessor = o as IDynamicAccessorUntyped;

            if (untypedAccessor != null)
            {
                if (key != mLastKey)
                {
                    mLastHint = 0;
                }
                mLastKey = key;
                object value = untypedAccessor.GetMember(key, ref mLastHint);
                if (value == null)
                {
                    return(default(T));
                }
                if (value is T)
                {
                    return((T)value);
                }
                else
                {
                    return(PlayScript.Dynamic.ConvertValue <T>(value));
                }
            }

            // handle dictionaries
            var dict = o as IDictionary;

            if (dict != null)
            {
                Stats.Increment(StatsCounter.GetIndexBinder_Key_Dictionary_Invoked);

                var ro = dict[key];
                if (ro is T)
                {
                    return((T)ro);
                }
                else
                {
                    return(Dynamic.ConvertValue <T>(ro));
                }
            }

            // fallback on getmemberbinder to do the hard work
            Stats.Increment(StatsCounter.GetIndexBinder_Key_Property_Invoked);

            // create a get member binder here
            if (mGetMember == null)
            {
                mGetMember = new PSGetMember(key);
            }

            // get member value
            return(mGetMember.GetNamedMember <T>(o, key));
        }