Inheritance: System.ComponentModel.TypeConverter
Exemple #1
0
        public static object ConvertObject(PropertyInfo prop, object val, Type objectType)
        {
            // Should i return default(T) if property.PropertyType is a valuetype?
            if (val == null)
            {
                return(val);
            }

            if (prop.PropertyType.IsAssignableFrom(val.GetType()))
            {
                return(val);
            }

            if (prop.PropertyType == typeof(string))
            {
                return(val.ToString());
            }

            TypeConverter tc = Helper.GetConverterFor(prop, prop.PropertyType);

            if (tc == null)
            {
                tc = new MoonlightTypeConverter(prop.Name, prop.PropertyType);
            }

            return(tc.ConvertFrom(null, Helper.DefaultCulture, val));
        }
Exemple #2
0
        public static object ConvertObject(DependencyProperty dp, object val, Type objectType, bool doToStringConversion)
        {
            // Should i return default(T) if property.PropertyType is a valuetype?
            if (val == null)
            {
                return(val);
            }

            if (dp.PropertyType.IsAssignableFrom(val.GetType()))
            {
                return(val);
            }

            if (dp.PropertyType == typeof(string))
            {
                return(doToStringConversion ? val.ToString() : "");
            }

            TypeConverter tc = null;

            if (dp.IsAttached)
            {
                tc = Helper.GetConverterFor(GetGetterMethodForAttachedDP(dp, val), dp.PropertyType);
            }
            else
            {
                PropertyInfo pi = dp.DeclaringType.GetProperty(dp.Name);
                if (pi != null)
                {
                    tc = Helper.GetConverterFor(pi, pi.PropertyType);
                    if (tc == null)
                    {
                        tc = new MoonlightTypeConverter(pi.Name, pi.PropertyType);
                    }
                }
            }

            if (tc == null)
            {
                tc = new MoonlightTypeConverter(dp.Name, dp.PropertyType);
            }

            return(tc.ConvertFrom(val));
        }
Exemple #3
0
		public static object ConvertObject (DependencyProperty dp, object val, Type objectType, bool doToStringConversion)
		{
			// Should i return default(T) if property.PropertyType is a valuetype?
			if (val == null)
				return val;
			
			if (dp.PropertyType.IsAssignableFrom (val.GetType ()))
				return val;

			if (dp.PropertyType == typeof (string))
				return doToStringConversion ? val.ToString() : "";
			
			TypeConverter tc = null;
			
			if (dp.IsAttached) {
				tc = Helper.GetConverterFor (GetGetterMethodForAttachedDP (dp, val), dp.PropertyType);
			}
			else {
				PropertyInfo pi = dp.DeclaringType.GetProperty (dp.Name);
				if (pi != null) {
					tc = Helper.GetConverterFor (pi, pi.PropertyType);
					if (tc == null)
						tc = new MoonlightTypeConverter (pi.Name, pi.PropertyType);
				}
			}
			
			if (tc == null)
				tc = new MoonlightTypeConverter (dp.Name, dp.PropertyType);
			
			if (!tc.CanConvertFrom (val.GetType()))
				throw new Exception (string.Format ("type converter {0} can't convert from type {1} to type {2}", tc.GetType(), val.GetType(), dp.PropertyType));

			return tc.ConvertFrom (val);
		}
Exemple #4
0
		public static object ConvertObject (PropertyInfo prop, object val, Type objectType)
		{
			// Should i return default(T) if property.PropertyType is a valuetype?
			if (val == null)
				return val;
			
			if (prop.PropertyType.IsAssignableFrom (val.GetType ()))
				return val;

			if (prop.PropertyType == typeof (string))
				return val.ToString ();
			
			TypeConverter tc = Helper.GetConverterFor (prop, prop.PropertyType);
			if (tc == null)
				tc = new MoonlightTypeConverter (prop.Name, prop.PropertyType);
			
			if (!tc.CanConvertFrom (val.GetType()))
				throw new Exception (string.Format ("type converter {0} can't convert from type {1}", tc.GetType(), val.GetType()));

			return tc.ConvertFrom (null, Helper.DefaultCulture, val);
		}
		public static object ConvertObject (PropertyInfo prop, object val, Type objectType)
		{
			// Should i return default(T) if property.PropertyType is a valuetype?
			if (val == null)
				return val;
			
			if (prop.PropertyType.IsAssignableFrom (val.GetType ()))
				return val;

			if (prop.PropertyType == typeof (string))
				return val.ToString ();
			
			TypeConverter tc = Helper.GetConverterFor (prop, prop.PropertyType);
			if (tc == null)
				tc = new MoonlightTypeConverter (prop.Name, prop.PropertyType);

			return tc.ConvertFrom (null, Helper.DefaultCulture, val);
		}
Exemple #6
0
		private static object ConvertType (MemberInfo pi, Type t, object value)
		{
			if (value == null)
				return null;

			Type valueType = value.GetType ();
			if (valueType == t)
				return value;

			try {
				if (t.IsEnum) {
					string str_value = value as string;
					if (str_value != null)
						return Enum.Parse (t, str_value, true);
					if (Enum.IsDefined (t, value))
						return Enum.ToObject (t, value);
				}
			} catch {
			}

			TypeConverter converter = Helper.GetConverterFor (pi, t);
			if (converter == null) {
				try {
					converter = new MoonlightTypeConverter (pi == null ? null : pi.Name, t);
				} catch {
					converter = null;
				}
			}

			if (converter != null && converter.CanConvertFrom (value.GetType ()))
				return converter.ConvertFrom (value);

			try {
				if (!valueType.IsSubclassOf (t))
					value = Convert.ChangeType (value, t, System.Globalization.CultureInfo.CurrentCulture);
			} catch {
			}
			
			// This will just let things fail
			return value;
		}
Exemple #7
0
		// refactor to reduce [SSC] code duplication
		TypeConverter GetConverter (Type type, string name, Type propertyType)
		{
			TypeConverter converter = Helper.GetConverterFor (type);
			if (converter == null)
				converter = new MoonlightTypeConverter (name, propertyType);
			return converter;
		}