예제 #1
0
        public static object GetMemberValue(this object target, string name, Type explicitType, ScriptValues.ValueType valueType)
        {
            if (valueType == ScriptValues.ValueType.Method || valueType == ScriptValues.ValueType.Constructor)
            {
                var bv  = target;
                var olt = bv as ObjectLiteral;
                if (valueType == ScriptValues.ValueType.Constructor && olt != null)
                {
                    return(olt[name]);
                }

                return(bv);
            }

            object          targetObject;
            bool            isEnum;
            MemberInfo      mi  = FindMember(target, name, explicitType, out targetObject, out isEnum);
            ObjectLiteral   ojl = targetObject as ObjectLiteral;
            FunctionLiteral ful = targetObject as FunctionLiteral;
            IDictionary <string, object> odi = targetObject as IDictionary <string, object>;
            IBasicKeyValueProvider       iba = targetObject as IBasicKeyValueProvider;

            if (mi == null)
            {
                if (ojl != null)
                {
                    return(ojl[name]);
                }

                if (ful != null)
                {
                    return(ful.GetInitialScopeValue(name));
                }

                if (odi != null && odi.ContainsKey(name))
                {
                    return(odi[name]);
                }
                else if (odi != null)
                {
                    return(null);
                }

                if (iba != null && iba.ContainsKey(name))
                {
                    return(iba[name]);
                }
                else if (iba != null)
                {
                    return(null);
                }
            }

            if (
                isEnum)
            {
                return(Enum.Parse((Type)targetObject, name));
            }

            if (mi == null)
            {
                throw new ScriptException(string.Format("Member {0} is not declared on {1}", name,
                                                        targetObject));
            }

            if (mi is PropertyInfo)
            {
                PropertyInfo pi = (PropertyInfo)mi;
                if (pi.CanRead)
                {
                    return(pi.GetValue(targetObject, null));
                }

                return(null);
            }

            if (mi is FieldInfo)
            {
                return(((FieldInfo)mi).GetValue(targetObject));
            }

            if (mi is EventInfo)
            {
                return(null);
            }

            throw new ScriptException(string.Format("GetValue is not supported for MemberType {0}", mi.MemberType));
        }
예제 #2
0
        public static Type GetMemberType(this object target, string name, Type explicitType, ScriptValues.ValueType valueType)
        {
            if (valueType == ScriptValues.ValueType.Method || valueType == ScriptValues.ValueType.Constructor)
            {
                return(null);
            }

            object          targetObject;
            bool            isEnum;
            MemberInfo      mi  = FindMember(target, name, explicitType, out targetObject, out isEnum);
            ObjectLiteral   ojl = targetObject as ObjectLiteral;
            FunctionLiteral ful = targetObject as FunctionLiteral;
            IDictionary <string, object> odi = targetObject as IDictionary <string, object>;
            IBasicKeyValueProvider       iba = targetObject as IBasicKeyValueProvider;

            if (mi == null)
            {
                if (ojl != null)
                {
                    return(ojl[name]?.GetType() ?? typeof(object));
                }

                if (ful != null)
                {
                    return(ful.GetInitialScopeValue(name)?.GetType() ?? typeof(object));
                }

                if (odi != null && odi.ContainsKey(name))
                {
                    return(odi[name]?.GetType() ?? typeof(object));
                }
                else if (odi != null)
                {
                    return(null);
                }

                if (iba != null && iba.ContainsKey(name))
                {
                    return(iba[name]?.GetType() ?? typeof(object));
                }
                else if (iba != null)
                {
                    return(null);
                }
            }

            if (isEnum)
            {
                return((Type)targetObject);
            }

            if (mi == null)
            {
                throw new ScriptException(string.Format("Member {0} is not declared on {1}", name,
                                                        targetObject));
            }

            if (mi is PropertyInfo pi)
            {
                if (pi.CanRead)
                {
                    return(pi.PropertyType);
                }

                return(null);
            }

            if (mi is FieldInfo fi)
            {
                return(fi.FieldType);
            }

            if (mi is EventInfo ev)
            {
                return(ev.EventHandlerType);
            }

            throw new ScriptException(string.Format("GetValue is not supported for MemberType {0}", mi.MemberType));
        }