Пример #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 void SetMemberValue(this object target, string name, object value, Type explicitType, ScriptValues.ValueType valueType)
        {
            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>;

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

                if (ful != null)
                {
                    ful.SetInitialScopeValue(name, value);
                    return;
                }

                if (odi != null)
                {
                    odi[name] = value;
                    return;
                }

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

            PropertyInfo pi;
            FieldInfo    fi;

            if (mi is PropertyInfo && (pi = (PropertyInfo)mi).CanWrite)
            {
                pi.SetValue(targetObject, value, null);
            }
            else if (mi is FieldInfo && !(fi = (FieldInfo)mi).IsLiteral)
            {
                fi.SetValue(targetObject, value);
            }
            else if (mi is EventInfo && value is FunctionLiteral)
            {
                FunctionLiteral fl = value as FunctionLiteral;
                EventInfo       ev = mi as EventInfo;
                ev.AddEventHandler(targetObject, fl.CreateDelegate(ev.EventHandlerType));
            }
            else
            {
                throw new ScriptException(string.Format("SetValue is not supported for this Member ({0}", name));
            }
        }
Пример #3
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));
        }