public XamlObjectReader(object instance, XamlSchemaContext schemaContext, XamlObjectReaderSettings settings) { if (schemaContext == null) { throw new ArgumentNullException("schemaContext"); } // FIXME: special case? or can it be generalized? In .NET, For Type instance Instance returns TypeExtension at root StartObject, while for Array it remains to return Array. if (instance is Type) { instance = new TypeExtension((Type)instance); } // See also Instance property for this weirdness. this.root_raw = instance; instance = TypeExtensionMethods.GetExtensionWrapped(instance); this.root = instance; sctx = schemaContext; this.settings = settings ?? new XamlObjectReaderSettings(); // check type validity. Note that some checks also needs done at Read() phase. (it is likely FIXME:) if (instance != null) { var type = new InstanceContext(instance).GetRawValue().GetType(); if (!type.GetTypeInfo().IsPublic) { throw new XamlObjectReaderException(String.Format("instance type '{0}' must be public and non-nested.", type)); } var xt = SchemaContext.GetXamlType(type); if (xt.ConstructionRequiresArguments && xt.GetConstructorArguments().Count == 0 && xt.TypeConverter == null) { throw new XamlObjectReaderException(String.Format("instance type '{0}' has no default constructor.", type)); } } value_serializer_context = new ValueSerializerContext(new PrefixLookup(sctx), sctx, null, null, null, null); new XamlObjectNodeIterator(instance, sctx, value_serializer_context, this.settings).PrepareReading(); }
public void TestNullableParameter() { var parameterType = typeof(TestClass).GetMethod(nameof(TestClass.SetNullableValue)).GetParameters()[0].ParameterType; Assert.AreEqual("System.Nullable<System.Int16>", TypeExtensionMethods.GetTypeNameInCSharpClass(parameterType), false); }
private string SerializeValue(object value, Type valueType) { if (value == null) { return(string.Empty); } if (valueType == typeof(byte[])) { var bytes = value as byte[]; if (bytes != null) { return("_" + Convert.ToBase64String(bytes) + "_"); } } switch (TypeExtensionMethods.GetTypeCode(valueType)) { case TypeCode.Empty: break; case TypeCode.Boolean: return(value.ToString().ToLower()); case TypeCode.Byte: return(value.ToString() + "b"); case TypeCode.Int16: return(value.ToString() + "s"); case TypeCode.Int32: return(value.ToString()); case TypeCode.Int64: return(value.ToString() + "l"); case TypeCode.Single: return(((float)value).ToString(CultureInfo.InvariantCulture) + "f"); case TypeCode.Double: return(((double)value).ToString(CultureInfo.InvariantCulture) + "d"); case TypeCode.Decimal: return(((decimal)value).ToString(CultureInfo.InvariantCulture) + "c"); case TypeCode.DateTime: DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return(((long)((DateTime)value - unixEpoch).TotalMilliseconds).ToString() + "t"); case TypeCode.String: case TypeCode.Char: // strings must escape these characters: // " -> \" // \ -> \\ string stringValue = value.ToString(); // escape quotes stringValue = stringValue.Replace("\\", "\\\\"); // escape backslashes stringValue = stringValue.Replace("\"", "\\" + "\""); return("\"" + stringValue + "\""); case TypeCode.Object: return(SerializeObjectValue(value, valueType)); } throw new NotImplementedException(); }
public void TestNullableReturnValue() { var returnType = typeof(TestClass).GetMethod(nameof(TestClass.GetNullableValue)).ReturnType; Assert.AreEqual("System.Nullable<System.Double>", TypeExtensionMethods.GetTypeNameInCSharpClass(returnType), false); }