private void CheckImplictString(Type valType, MappingStack mappingStack) { if (valType == null || valType == typeof(string) || valType.IsEnum) { return; } throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains implicit string value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valType) + " expected " + StackDump(mappingStack)); }
public void Double() { Type type = typeof(double); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tDouble, rpcType, "Double doesn't map to XmlRpcType.tDouble"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "double", "Double doesn't map to 'double'"); }
public void String() { Type type = typeof(string); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tString, rpcType, "String doesn't map to XmlRpcType.tString"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "string", "String doesn't map to 'string'"); }
public void Int64() { Type type = typeof(long); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tInt64, rpcType, "Int64 doesn't map to XmlRpcType.tInt64"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "i8", "Int64 doesn't map to 'i8'"); }
public void Void() { Type type = typeof(void); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tVoid, rpcType, "void doesn't map to XmlRpcType.tVoid"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "void", "void doesn't map to 'void'"); }
public void JaggedIntArray() { Type type = typeof(Int32[][]); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tArray, rpcType, "Int32[] doesn't map to XmlRpcType.tArray"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "array", "Int32[] doesn't map to 'array'"); }
public void Array() { Type type = typeof(Array); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tArray, rpcType, "Array doesn't map to XmlRpcType.tArray"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "array", "Array doesn't map to 'array'"); }
public void Base64() { Type type = typeof(byte[]); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tBase64, rpcType, "Byte[] doesn't map to XmlRpcType.tBase64"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "base64", "Byte[] doesn't map to 'base64'"); }
public void Int32() { Type type = typeof(int); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tInt32, rpcType, "Int32 doesn't map to XmlRpcType.tInt32"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "integer", "Int32 doesn't map to 'integer'"); }
public void XmlRpcBoolean() { Type type = typeof(bool?); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tBoolean, rpcType, "XmlRpcBoolean doesn't map to XmlRpcType.tBoolean"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "boolean", "XmlRpcBoolean doesn't map to 'boolean'"); }
public void NullableStruct() { Type type = typeof(TestStruct?); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tStruct, rpcType, "TestStruct? doesn't map to XmlRpcType.tStruct"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "struct", "TestStruct? doesn't map to 'struct'"); }
public void NullableDateTime() { Type type = typeof(DateTime?); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tDateTime, rpcType, "DateTime? doesn't map to XmlRpcType.tDateTime"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "dateTime", "DateTime? doesn't map to 'dateTime'"); }
public void XmlRpcStruct() { Type type = typeof(XmlRpcStruct); XmlRpcType rpcType = XmlRpcTypeInfo.GetXmlRpcType(type); Assert.AreEqual(XmlRpcType.tHashtable, rpcType, "XmlRpcStruct doesn't map to XmlRpcType.tHashtable"); string rpcString = XmlRpcTypeInfo.GetXmlRpcTypeString(type); Assert.AreEqual(rpcString, "struct", "XmlRpcStruct doesn't map to 'struct'"); }
private void CheckExpectedType(Type expectedType, Type actualType, MappingStack mappingStack) { if (expectedType != null && expectedType.IsEnum) { var array1 = new[] { typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int) }; var array2 = new[] { typeof(uint), typeof(long) }; Type underlyingType = Enum.GetUnderlyingType(expectedType); if (Array.IndexOf(array1, underlyingType) >= 0) { expectedType = typeof(int); } else if (Array.IndexOf(array2, underlyingType) >= 0) { expectedType = typeof(long); } else { throw new XmlRpcInvalidEnumValue(mappingStack.MappingType + " contains " + XmlRpcTypeInfo.GetXmlRpcTypeString(actualType) + " which cannot be mapped to " + XmlRpcTypeInfo.GetXmlRpcTypeString(expectedType) + " " + StackDump(mappingStack)); } } if (expectedType == null || expectedType == typeof(object) || (expectedType == actualType || !actualType.IsValueType)) { return; } if (expectedType == typeof(Nullable <>).MakeGenericType(new Type[] { actualType })) { return; } throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains " + XmlRpcTypeInfo.GetXmlRpcTypeString(actualType) + " value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(expectedType) + " expected " + StackDump(mappingStack)); }
private object MapArray(IEnumerator <Node> iter, Type valType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType) { mappedType = null; if (valType != null && !valType.IsArray && (valType != typeof(Array) && valType != typeof(object))) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains array value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valType) + " expected " + StackDump(mappingStack)); } if (valType != null) { if (XmlRpcTypeInfo.GetXmlRpcType(valType) == XmlRpcType.tMultiDimArray) { (mappingStack).Push("array mapped to type " + valType.Name); return(MapMultiDimArray(iter, valType, mappingStack, mappingAction)); } else { (mappingStack).Push("array mapped to type " + valType.Name); } } else { (mappingStack).Push("array"); } List <object> list = new List <object>(); Type valType1 = DetermineArrayItemType(valType); bool flag = false; Type elementType = null; while (iter.MoveNext() && iter.Current is ValueNode) { (mappingStack).Push(string.Format("element {0}", list.Count)); object obj = MapValueNode(iter, valType1, mappingStack, mappingAction); list.Add(obj); mappingStack.Pop(); } foreach (object obj in list) { if (obj != null) { if (!flag) { elementType = obj.GetType(); flag = true; } else if (elementType != obj.GetType()) { elementType = null; } } } object[] args = new object[1] { list.Count }; object obj1 = valType == null || valType == typeof(Array) || valType == typeof(object) ? (elementType != null ? Array.CreateInstance(elementType, (int)args[0]) : CreateArrayInstance(typeof(object[]), args)) : CreateArrayInstance(valType, args); for (int index = 0; index < list.Count; ++index) { ((Array)obj1).SetValue(list[index], index); } mappingStack.Pop(); return(obj1); }
private object MapStruct(IEnumerator <Node> iter, Type valueType, MappingStack mappingStack, MappingAction mappingAction, out Type mappedType) { mappedType = null; if (valueType.IsPrimitive) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains struct value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valueType) + " expected " + StackDump(mappingStack)); } if (valueType.IsGenericType) { if (valueType.GetGenericTypeDefinition() == typeof(Nullable <>)) { valueType = valueType.GetGenericArguments()[0]; } } object instance; try { instance = Activator.CreateInstance(valueType); } catch (Exception ex) { throw new XmlRpcTypeMismatchException(mappingStack.MappingType + " contains struct value where " + XmlRpcTypeInfo.GetXmlRpcTypeString(valueType) + " expected (as type " + valueType.Name + ") " + StackDump(mappingStack)); } MappingAction mappingAction1 = mappingAction; if (valueType != null) { (mappingStack).Push("struct mapped to type " + valueType.Name); mappingAction1 = StructMappingAction(valueType, mappingAction); } else { (mappingStack).Push("struct"); } List <string> names = new List <string>(); CreateFieldNamesMap(valueType, names); int num = 0; List <string> list = new List <string>(); try { while (iter.MoveNext()) { if (iter.Current is StructMember) { string XmlRpcName = (iter.Current as StructMember).Value; if (list.Contains(XmlRpcName)) { if (!IgnoreDuplicateMembers) { throw new XmlRpcInvalidXmlRpcException(mappingStack.MappingType + " contains struct value with duplicate member " + XmlRpcName + " " + StackDump(mappingStack)); } } else { list.Add(XmlRpcName); string name = GetStructName(valueType, XmlRpcName) ?? XmlRpcName; MemberInfo element = valueType.GetField(name) ?? (MemberInfo)valueType.GetProperty(name); if (element == null) { iter.MoveNext(); //If this is a table object, then we need to populate // the value in the custom fields dictionary. if (valueType.IsSubclassOf(typeof(Table))) { AddCustomField(iter, mappingStack, mappingAction, name, instance); } if (iter.Current is ComplexValueNode) { int depth = iter.Current.Depth; while (!(iter.Current is EndComplexValueNode) || iter.Current.Depth != depth) { iter.MoveNext(); } } } else { if (names.Contains(name)) { names.Remove(name); } else if (Attribute.IsDefined(element, typeof(NonSerializedAttribute))) { (mappingStack).Push(string.Format("member {0}", name)); throw new XmlRpcNonSerializedMember( "Cannot map XML-RPC struct member onto member marked as [NonSerialized]: " + StackDump(mappingStack)); } Type memberType = element.MemberType == MemberTypes.Field ? (element as FieldInfo).FieldType : (element as PropertyInfo).PropertyType; string p = valueType == null ? string.Format("member {0}", name) : string.Format("member {0} mapped to type {1}", name, memberType.Name); iter.MoveNext(); object obj = OnStack(p, mappingStack, (() => MapValueNode(iter, memberType, mappingStack, mappingAction))); if (element.MemberType == MemberTypes.Field) { (element as FieldInfo).SetValue(instance, obj); } else { (element as PropertyInfo).SetValue(instance, obj, null); } ++num; } } } else { break; } } if (mappingAction1 == MappingAction.Error && names.Count > 0) { ReportMissingMembers(valueType, names, mappingStack); } return(instance); } finally { mappingStack.Pop(); } }