コード例 #1
0
ファイル: VmBase.cs プロジェクト: devbharat/apm_coop_fl
        // Common method for populating properties, using a hardcoded
        // property update order, and reflection to get the property type
        public static void PopulatePropsFromUpdate(ISupportsExternalInvokedInpc obj,IList<string> propertiesToUpdate, string strRx, bool fireInpc)
        {
            var strs = strRx.Split(',');

            if (propertiesToUpdate.Count != strs.Length)
            {
                Console.WriteLine("Processing update with " + strs.Length
                                  + " values, but have " + propertiesToUpdate.Count
                                  + " properties to populate. Ignoring this update");
                return;
            }

            for (int i = 0; i < propertiesToUpdate.Count; i++)
            {
                var prop = obj.GetType().GetProperty(propertiesToUpdate[i]);
                var s = strs[i];
                object value = null;

                if (prop == null)
                {
                    Console.WriteLine("Trying to set non existant property: " + propertiesToUpdate[i]);
                    break;
                }

                if (prop.PropertyType == typeof(float))
                {
                    float val;
                    if (!float.TryParse(s, out val))
                    {
                        Console.WriteLine("Error parsing float: '{0}', VM: {1}" ,s, obj);
                        break;
                    }
                    value = val;
                }
                if (prop.PropertyType == typeof(bool))
                {
                    float val;
                    if (!float.TryParse(s, out val))
                    {
                        Console.WriteLine("Error parsing float (bool): '{0}', VM: {1}" ,s, obj);
                        break;
                    }
                    value = val != 0.0;
                }

                if (prop.PropertyType == typeof(int))
                {
                    int val;
                    if (!int.TryParse(s, out val))
                    {
                        Console.WriteLine("Error parsing int: '{0}', VM: {1}"  ,s, obj);
                        break;
                    }
                    value = val;
                }

                prop.SetValue(obj, value, null);

                if (fireInpc)
                    obj.FirePropertyChanged(propertiesToUpdate[i]);
            }
        }
コード例 #2
0
        // Common method for populating properties, using a hardcoded
        // property update order, and reflection to get the property type
        public static void PopulatePropsFromUpdate(ISupportsExternalInvokedInpc obj, IList <string> propertiesToUpdate, string strRx, bool fireInpc)
        {
            var strs = strRx.Split(',');

            if (propertiesToUpdate.Count != strs.Length)
            {
                Console.WriteLine("Processing update with " + strs.Length
                                  + " values, but have " + propertiesToUpdate.Count
                                  + " properties to populate. Ignoring this update");
                return;
            }

            for (int i = 0; i < propertiesToUpdate.Count; i++)
            {
                var    prop  = obj.GetType().GetProperty(propertiesToUpdate[i]);
                var    s     = strs[i];
                object value = null;

                if (prop == null)
                {
                    Console.WriteLine("Trying to set non existant property: " + propertiesToUpdate[i]);
                    break;
                }

                if (prop.PropertyType == typeof(float))
                {
                    float val;
                    if (!float.TryParse(s, out val))
                    {
                        Console.WriteLine("Error parsing float: '{0}', VM: {1}", s, obj);
                        break;
                    }
                    value = val;
                }
                if (prop.PropertyType == typeof(bool))
                {
                    float val;
                    if (!float.TryParse(s, out val))
                    {
                        Console.WriteLine("Error parsing float (bool): '{0}', VM: {1}", s, obj);
                        break;
                    }
                    value = val != 0.0;
                }

                if (prop.PropertyType == typeof(int))
                {
                    int val;
                    if (!int.TryParse(s, out val))
                    {
                        Console.WriteLine("Error parsing int: '{0}', VM: {1}", s, obj);
                        break;
                    }
                    value = val;
                }

                prop.SetValue(obj, value, null);

                if (fireInpc)
                {
                    obj.FirePropertyChanged(propertiesToUpdate[i]);
                }
            }
        }