Пример #1
0
		private static object DefaultValueForCorlibType (Type t)
		{
			object res = false;
			
			if (t == typeof (string))
				res = String.Empty;
			else if (t == typeof (int))
				res = 0;
			else if (t == typeof (double))
				res = 0.0;
			else if (t == typeof (bool))
				res = false;

			if (res != null)
				res = new MutableObject (res);

			return res;
		}
Пример #2
0
		private object InstantiateType (Type type)
		{
			object o;

			if (IsTopElement && HydrateObject != null) {
				if (!type.IsAssignableFrom (HydrateObject.GetType ()))
					throw ParseException ("Invalid top-level element found {0}, expecting {1}", type, HydrateObject.GetType ());
				return HydrateObject;
			}
			
			// Returns null if the type isn't a collection type.
			o = InstantiateCollectionType (type);

			if (o == null && HasDefaultConstructor (type))
				o = Activator.CreateInstance (type);

			if (o == null && IsSpecialInternalType (type))
			{
				o = Activator.CreateInstance (type,
					BindingFlags.CreateInstance | BindingFlags.NonPublic |
					BindingFlags.Instance | BindingFlags.OptionalParamBinding,
					null, new object[] {}, null);
			}

			if (o == null && IsLegalCorlibType (type))
				o = DefaultValueForCorlibType (type);

			if (o == null && IsLegalStructType (type))
				o = DefaultValueForStructType (type);

			if (o == null && IsSpecialCasedType (type))
				o = DefaultValueForSpecialCasedType (type);

			// If an object does not have a content property, we can use
			// a class level type converter to generate an object of this type
			if (!(o is MutableObject) && IsTypeConvertedType (type))
				o = new MutableObject (o);

			return o;
		}