AnyToJsValue() public method

public AnyToJsValue ( object obj ) : Espresso.JsValue
obj object
return Espresso.JsValue
Example #1
0
        internal bool TryGetMemberValue(Type type, object obj, string name, out JsValue value)
        {
            object result;

            // dictionaries.
            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                IDictionary dictionary = (IDictionary)obj;
                if (dictionary.Contains(name))
                {
                    result = dictionary[name];
                    value  = _convert.AnyToJsValue(result);
                }
                else
                {
                    value = JsValue.Null;
                }
                return(true);
            }

            BindingFlags flags;

            if (type == obj)
            {
                flags = BindingFlags.Public | BindingFlags.Static;
            }
            else
            {
                flags = BindingFlags.Public | BindingFlags.Instance;
            }

            // First of all try with a public property (the most common case).
            PropertyInfo pi = type.GetProperty(name, flags | BindingFlags.GetProperty);

            if (pi != null)
            {
                result = pi.GetValue(obj, null);
                value  = _convert.AnyToJsValue(result);
                return(true);
            }

            // try field.
            FieldInfo fi = type.GetField(name, flags | BindingFlags.GetProperty);

            if (fi != null)
            {
                result = fi.GetValue(obj);
                value  = _convert.AnyToJsValue(result);
                return(true);
            }

            // Then with an instance method: the problem is that we don't have a list of
            // parameter types so we just check if any method with the given name exists
            // and then keep alive a "weak delegate", i.e., just a name and the target.
            // The real method will be resolved during the invokation itself.
            BindingFlags mFlags = flags | BindingFlags.InvokeMethod | BindingFlags.FlattenHierarchy;

            // TODO: This is probably slooow.
            foreach (var met in type.GetMembers(flags))
            {
                if (met.Name == name)
                {
                    if (type == obj)
                    {
                        result = new WeakDelegate(type, name);
                    }
                    else
                    {
                        result = new WeakDelegate(obj, name);
                    }
                    value = _convert.AnyToJsValue(result);
                    return(true);
                }
            }
            //if (type.GetMethods(mFlags).Any(x => x.Name == name))
            //{
            //    if (type == obj)
            //    {
            //        result = new WeakDelegate(type, name);
            //    }
            //    else
            //    {
            //        result = new WeakDelegate(obj, name);
            //    }
            //    value = _convert.ToJsValue(result);
            //    return true;
            //}

            value = JsValue.Null;
            return(false);
        }
Example #2
0
        internal bool TryGetMemberValue(Type type, object obj, string name, ref JsValue value)
        {
            object result;

            // dictionaries.
            if (typeof(IDictionary).ExtIsAssignableFrom(type))
            {
                //this implement IDic
                IDictionary dictionary = (IDictionary)obj;
                if (dictionary.Contains(name))
                {
                    result = dictionary[name];
                    _convert.AnyToJsValue(result, ref value);
                }
                else
                {
                    value.Type = JsValueType.Null;
                }
                return(true);
            }


            //try public property
            PropertyInfo pi = type.ExtGetProperty(obj, name);

            if (pi != null)
            {
                result = pi.GetValue(obj, null);
                _convert.AnyToJsValue(result, ref value);
                return(true);
            }
            // try field.
            FieldInfo fi = type.ExtGetField(obj, name);

            if (fi != null)
            {
                result = fi.GetValue(obj);
                _convert.AnyToJsValue(result, ref value);
                return(true);
            }

            // Then with an instance method: the problem is that we don't have a list of
            // parameter types so we just check if any method with the given name exists
            // and then keep alive a "weak delegate", i.e., just a name and the target.
            // The real method will be resolved during the invokation itself.

            //TODO: check if we should use 'method-group' instead of first found method
            throw new NotSupportedException();

            //BindingFlags mFlags = flags | BindingFlags.InvokeMethod | BindingFlags.FlattenHierarchy;

            //// TODO: This is probably slooow.
            //foreach (var met in type.GetMembers(flags))
            //{
            //    //TODO: review here
            //    //only 1 ?
            //    //or list of all method with the same name
            //    if (met.Name == name)
            //    {
            //        if (type == obj)
            //        {
            //            result = new WeakDelegate(type, name);
            //        }
            //        else
            //        {
            //            result = new WeakDelegate(obj, name);
            //        }
            //        _convert2.AnyToJsValue(result, ref value);
            //        return true;
            //    }
            //}
            ////if (type.GetMethods(mFlags).Any(x => x.Name == name))
            ////{
            ////    if (type == obj)
            ////    {
            ////        result = new WeakDelegate(type, name);
            ////    }
            ////    else
            ////    {
            ////        result = new WeakDelegate(obj, name);
            ////    }
            ////    value = _convert.ToJsValue(result);
            ////    return true;
            ////}

            value.Type = JsValueType.Null;
            return(false);
        }