Пример #1
0
        internal static void ParseProperties(Rainmeter.API api, object obj)
        {
            LogHelper log  = new LogHelper(api);
            Type      type = obj.GetType();

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            EventInfo[]    events     = type.GetEvents(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo pinfo in properties)
            {
                if (pinfo.CanWrite)
                {
                    if (pinfo.PropertyType == typeof(string))
                    {
                        //log.LogDebug("Found string Property: " + pinfo.Name);
                        string propval = api.ReadString(pinfo.Name, "");
                        if (propval != "")
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(int))
                    {
                        int val = (int)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found int Property: " + pinfo.Name + " with value: " + val);
                        int propval = api.ReadInt(pinfo.Name, val);
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(bool))
                    {
                        bool val = (bool)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found " + pinfo.PropertyType + " Property: " + pinfo.Name + " with value: " + val);
                        bool propval = api.ReadInt(pinfo.Name, Convert.ToInt32(val)) > 0;
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(float))
                    {
                        float val = (float)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found float Property: " + pinfo.Name + " with value: " + val);
                        float propval = (float)api.ReadDouble(pinfo.Name, val);
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else if (pinfo.PropertyType == typeof(double))
                    {
                        double val = (double)pinfo.GetValue(obj, null);
                        //log.LogDebug("Found double Property: " + pinfo.Name + " with value: " + val);
                        double propval = api.ReadDouble(pinfo.Name, val);
                        if (propval != val)
                        {
                            log.LogDebug("Setting " + pinfo.PropertyType.Name + " Property " + pinfo.Name + " to " + propval);
                            pinfo.SetValue(obj, propval, null);
                        }
                    }
                    else
                    {
                        string propval = api.ReadString(pinfo.Name, "");
                        if (propval != "")
                        {
                            object val = null;

                            if (pinfo.PropertyType == typeof(Color))
                            {
                                val = Util.ColorFromString(propval);
                            }
                            else
                            {
                                val = pinfo.PropertyType.IsEnum ?
                                      Util.EnumFromString(pinfo.PropertyType, propval) :
                                      Util.ObjectFromString(api, pinfo.PropertyType, propval);
                            }

                            if (val != null)
                            {
                                pinfo.SetValue(obj, val, null);
                            }
                            else
                            {
                                log.LogPropertyValueNotValid(propval, pinfo);
                            }
                        }
                    }
                }
            }

            foreach (EventInfo einfo in events)
            {
                // winforms syntax for events
                string evntval = api.ReadString(einfo.Name, "");
                if (evntval == "")
                {
                    // rainmeter syntax for events
                    evntval = api.ReadString("On" + einfo.Name, "");
                    if (evntval == "")
                    {
                        continue;
                    }
                }

                // remove existing event
                cEventHelper.RemoveEventHandler(obj, einfo.Name);

                // create new event
                EventHandler untypedHandler = delegate(object sender, EventArgs args)
                {
                    api.Execute(evntval);
                };

                Delegate typedHandler = Delegate.CreateDelegate(einfo.EventHandlerType,
                                                                untypedHandler.Target, untypedHandler.Method);
                einfo.AddEventHandler(obj, typedHandler);
            }
        }