Exemple #1
0
        public void SetProperty(object target, string propertyName, object propertyValue)
        {
            try
            {
                IGetterSetter hSet = (IGetterSetter)_properties[propertyName];
                if (hSet == null)
                {
                    throw new DynaAccessException("No Set method for property " + propertyName + " on instance of " + _proxyType.Name);
                }

                // 尽最大努力传递正确的值
                if (!hSet.PropertyType.IsInstanceOfType(propertyValue) && propertyValue != null)
                {
                    if (hSet.PropertyType.Equals(typeof(string)))
                    {
                        hSet.Set(target, propertyValue.ToString());
                    }
                    else if (hSet.PropertyType.Equals(typeof(Guid)))
                    {
                        hSet.Set(target, new Guid(propertyValue.ToString()));
                    }
                    else
                    {
                        hSet.Set(target, Convert.ChangeType(propertyValue, hSet.PropertyType));
                    }
                }
                else
                {
                    hSet.Set(target, propertyValue);
                }
            }
            catch (DynaAccessException dae)
            {
                throw dae;
            }
            catch (Exception e)
            {
                throw new DynaAccessException(e);
            }
        }