/// <summary>
        /// Returns the value of the property or field with the specified <paramref name="name"/>.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="name">The name of the property or field whose value is to be returned.</param>
        /// <param name="index">Optional index values for indexed properties.  This value should be <c>null</c> for non-indexed properties.</param>
        public static object GetValue(this IPairable obj, string name, object[] index)
        {
            if (obj == null)
            {
                return(null);
            }

            var type     = obj.GetType();
            var property = Device.Reflector.GetProperty(type, name);

            if (property != null)
            {
                return(property.GetValue(obj, index));
            }

            var field = Device.Reflector.GetField(type, name);

            if (field != null)
            {
                return(field.GetValue(obj));
            }

            var pairable = obj as IPairable;

            if (pairable == null || pairable.Pair == null)
            {
                return(null);
            }

            obj      = pairable.Pair;
            type     = obj.GetType();
            property = Device.Reflector.GetProperty(type, name);
            if (property != null)
            {
                return(property.GetValue(obj, index));
            }

            field = Device.Reflector.GetField(type, name);
            if (field != null)
            {
                return(field.GetValue(obj));
            }

            return(null);
        }
示例#2
0
        internal static bool RaiseEvent(this IPairable obj, string eventName, EventArgs args)
        {
            var type = obj.GetType();
            var evt  = type.GetEvent(eventName, BindingFlags.Instance | BindingFlags.Public);

            if (evt != null)
            {
                var attribute = evt.GetCustomAttributes <EventDelegateAttribute>().FirstOrDefault();
                if (attribute != null)
                {
                    eventName = attribute.DelegateName;
                }

                FieldInfo info = null;
                do
                {
                    info = type.GetField(eventName, BindingFlags.Instance | BindingFlags.NonPublic);
                    type = type.BaseType;
                }while (info == null && type != null);

                if (info != null)
                {
                    var del = info.GetValue(obj) as MulticastDelegate;
                    if (del != null)
                    {
                        var invocationList = del.GetInvocationList();
                        foreach (var method in invocationList)
                        {
                            method.Method.Invoke(method.Target, new object[] { obj.Pair ?? obj, EventArgs.Empty });
                        }
                        return(true);
                    }
                }
            }

            return(false);
        }
        /// <summary>
        /// Attempts to raise the event with the specified name and <see cref="EventArgs"/> on the current instance.
        /// </summary>
        /// <param name="obj">The current object.</param>
        /// <param name="eventName">The name of the event to be raised.</param>
        /// <param name="args">The <see cref="EventArgs"/> instance containing the event data.</param>
        /// <returns><c>true</c> if the event is successfully raised, otherwise <c>false</c>.</returns>
        public static bool RaiseEvent(this IPairable obj, string eventName, EventArgs args)
        {
            if (obj == null)
            {
                return(false);
            }
            var type = obj.GetType();
            var evt  = Device.Reflector.GetEvent(type, eventName);

            if (evt == null)
            {
                return(false);
            }
            var attribute = evt.GetCustomAttributes(typeof(EventDelegateAttribute), true).FirstOrDefault() as EventDelegateAttribute;

            if (attribute != null)
            {
                eventName = attribute.DelegateName;
            }

            FieldInfo info;

            do
            {
                info = Device.Reflector.GetField(type, eventName);
                type = Device.Reflector.GetBaseType(type);
            }while (info == null && type != null);

            if (info == null && obj.Pair != null)
            {
                obj  = obj.Pair;
                type = obj.GetType();
                evt  = Device.Reflector.GetEvent(type, eventName);
                if (evt == null)
                {
                    return(false);
                }
                attribute = evt.GetCustomAttributes(typeof(EventDelegateAttribute), true).FirstOrDefault() as EventDelegateAttribute;
                if (attribute != null)
                {
                    eventName = attribute.DelegateName;
                }

                do
                {
                    info = Device.Reflector.GetField(type, eventName);
                    type = Device.Reflector.GetBaseType(type);
                }while (info == null && type != null);
            }

            if (info == null)
            {
                return(false);
            }
            var del = info.GetValue(obj) as MulticastDelegate;

            if (del == null)
            {
                return(false);
            }
            var invocationList = del.GetInvocationList();

            foreach (var method in invocationList)
            {
                method.Raise(obj, args);
            }
            return(true);
        }
        /// <summary>
        /// Sets the value of the property or field with the specified <paramref name="name"/> when running on one of the specified <see cref="MobileTarget"/>s.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="name">The name of the property or field whose value is to be set.</param>
        /// <param name="value">The value to set to the property or field.</param>
        /// <param name="index">Optional index values for indexed properties.  This value should be <c>null</c> for non-indexed properties.</param>
        /// <param name="converter">An optional converter to use on the value prior to setting the property or field.</param>
        /// <param name="targets">The targets on which the code must be running for the value to be set.</param>
        public static void SetValue(this IPairable obj, string name, object value, object[] index, IValueConverter converter, MobileTarget targets)
        {
            if (obj == null || !targets.HasFlag(iApp.Factory.Target))
            {
                return;
            }

            var type = obj.GetType();

            try
            {
                var property = Device.Reflector.GetProperty(type, name);
                if (property != null)
                {
                    if (converter != null)
                    {
                        value = converter.Convert(value, property.PropertyType, null);
                    }
                    else
                    {
                        value = Binding.GetConvertedValue(value, property.PropertyType);
                    }
                    property.SetValue(obj, value, index);
                    return;
                }

                var field = Device.Reflector.GetField(type, name);
                if (field != null)
                {
                    if (converter != null)
                    {
                        value = converter.Convert(value, field.FieldType, null);
                    }
                    else
                    {
                        value = Binding.GetConvertedValue(value, field.FieldType);
                    }
                    field.SetValue(obj, value);
                    return;
                }

                if (obj != null && obj.Pair != null)
                {
                    type     = obj.Pair.GetType();
                    property = Device.Reflector.GetProperty(type, name);
                    if (property != null)
                    {
                        if (converter != null)
                        {
                            value = converter.Convert(value, property.PropertyType, null);
                        }
                        else
                        {
                            value = Binding.GetConvertedValue(value, property.PropertyType);
                        }
                        property.SetValue(obj.Pair, value, index);
                        return;
                    }

                    field = Device.Reflector.GetField(type, name);
                    if (field != null)
                    {
                        if (converter != null)
                        {
                            value = converter.Convert(value, field.FieldType, null);
                        }
                        else
                        {
                            value = Binding.GetConvertedValue(value, field.FieldType);
                        }
                        field.SetValue(obj.Pair, value);
                    }
                }
            }
            catch (Exception e)
            {
                iApp.Log.Warn("Unable to set value of member on {0}.", e, obj.ToString());
            }
        }