private static bool IsStructType(Type type, out bool isValueTuple) { isValueTuple = false; if (type.GetTypeInfo().IsEnum) { return(false); } if (s_mappedTypes.Contains(type)) { return(false); } if (ArgTypeInspector.IsDBusObjectType(type, isCompileTimeType: true)) { return(false); } Type elementType; if (ArgTypeInspector.InspectEnumerableType(type, out elementType, isCompileTimeType: true) != ArgTypeInspector.EnumerableType.NotEnumerable) { return(false); } return(ArgTypeInspector.IsStructType(type, out isValueTuple)); }
public static MethodInfo CreateReadMethodForType(Type type) // Type Read(MessageReader) { if (type.GetTypeInfo().IsEnum) { return(s_messageReaderReadEnum.MakeGenericMethod(new[] { type })); } if (type == typeof(bool)) { return(s_messageReaderReadBoolean); } else if (type == typeof(byte)) { return(s_messageReaderReadByte); } else if (type == typeof(double)) { return(s_messageReaderReadDouble); } else if (type == typeof(short)) { return(s_messageReaderReadInt16); } else if (type == typeof(int)) { return(s_messageReaderReadInt32); } else if (type == typeof(long)) { return(s_messageReaderReadInt64); } else if (type == typeof(ObjectPath)) { return(s_messageReaderReadObjectPath); } else if (type == typeof(Signature)) { return(s_messageReaderReadSignature); } else if (type == typeof(string)) { return(s_messageReaderReadString); } else if (type == typeof(float)) { return(s_messageReaderReadSingle); } else if (type == typeof(ushort)) { return(s_messageReaderReadUInt16); } else if (type == typeof(uint)) { return(s_messageReaderReadUInt32); } else if (type == typeof(ulong)) { return(s_messageReaderReadUInt64); } else if (type == typeof(object)) { return(s_messageReaderReadVariant); } else if (type == typeof(IDBusObject)) { return(s_messageReaderReadBusObject); } if (ArgTypeInspector.IsDBusObjectType(type, isCompileTimeType: true)) { return(s_messageReaderReadDBusInterface.MakeGenericMethod(new[] { type })); } Type elementType; var enumerableType = ArgTypeInspector.InspectEnumerableType(type, out elementType, isCompileTimeType: true); if (enumerableType != ArgTypeInspector.EnumerableType.NotEnumerable) { if (enumerableType == ArgTypeInspector.EnumerableType.GenericDictionary) { TypeInfo elementTypeInfo = elementType.GetTypeInfo(); Type keyType = elementTypeInfo.GenericTypeArguments[0]; Type valueType = elementTypeInfo.GenericTypeArguments[1]; return(s_messageReaderReadDictionary.MakeGenericMethod(new[] { keyType, valueType })); } else if (enumerableType == ArgTypeInspector.EnumerableType.AttributeDictionary) { return(s_messageReaderReadDictionaryObject.MakeGenericMethod(new[] { type })); } else // Enumerable, EnumerableKeyValuePair { return(s_messageReaderReadArray.MakeGenericMethod(new[] { elementType })); } } bool isValueTuple; if (ArgTypeInspector.IsStructType(type, out isValueTuple)) { if (isValueTuple) { return(s_messageReaderReadValueTupleStruct.MakeGenericMethod(type)); } else { return(s_messageReaderReadStruct.MakeGenericMethod(type)); } } if (ArgTypeInspector.IsSafeHandleType(type)) { return(s_messageReaderReadSafeHandle.MakeGenericMethod(type)); } throw new ArgumentException($"Cannot (de)serialize Type '{type.FullName}'"); }
public static MethodInfo CreateWriteMethodForType(Type type, bool isCompileTimeType) // void Write(MessageWriter, T) { if (type.GetTypeInfo().IsEnum) { type = Enum.GetUnderlyingType(type); } if (type == typeof(bool)) { return(s_messageWriterWriteBoolean); } else if (type == typeof(byte)) { return(s_messageWriterWriteByte); } else if (type == typeof(double)) { return(s_messageWriterWriteDouble); } else if (type == typeof(short)) { return(s_messageWriterWriteInt16); } else if (type == typeof(int)) { return(s_messageWriterWriteInt32); } else if (type == typeof(long)) { return(s_messageWriterWriteInt64); } else if (type == typeof(ObjectPath)) { return(s_messageWriterWriteObjectPath); } else if (type == typeof(Signature)) { return(s_messageWriterWriteSignature); } else if (type == typeof(string)) { return(s_messageWriterWriteString); } else if (type == typeof(float)) { return(s_messageWriterWriteSingle); } else if (type == typeof(ushort)) { return(s_messageWriterWriteUInt16); } else if (type == typeof(uint)) { return(s_messageWriterWriteUInt32); } else if (type == typeof(ulong)) { return(s_messageWriterWriteUInt64); } else if (type == typeof(object)) { return(s_messageWriterWriteVariant); } else if (type == typeof(IDBusObject)) { return(s_messageWriterWriteBusObject); } if (ArgTypeInspector.IsDBusObjectType(type, isCompileTimeType)) { return(s_messageWriterWriteBusObject); } Type elementType; var enumerableType = ArgTypeInspector.InspectEnumerableType(type, out elementType, isCompileTimeType); if (enumerableType != ArgTypeInspector.EnumerableType.NotEnumerable) { if ((enumerableType == ArgTypeInspector.EnumerableType.EnumerableKeyValuePair) || (enumerableType == ArgTypeInspector.EnumerableType.GenericDictionary)) { return(s_messageWriterWriteDict.MakeGenericMethod(elementType.GenericTypeArguments)); } else if (enumerableType == ArgTypeInspector.EnumerableType.AttributeDictionary) { return(s_messageWriterWriteDictionaryObject.MakeGenericMethod(type)); } else // Enumerable { return(s_messageWriterWriteArray.MakeGenericMethod(new[] { elementType })); } } bool isValueTuple; if (ArgTypeInspector.IsStructType(type, out isValueTuple)) { if (isValueTuple) { return(s_messageWriterWriteValueTupleStruct.MakeGenericMethod(type)); } else { return(s_messageWriterWriteStruct.MakeGenericMethod(type)); } } if (ArgTypeInspector.IsSafeHandleType(type)) { return(s_messageWriterWriteSafeHandle); } throw new ArgumentException($"Cannot (de)serialize Type '{type.FullName}'"); }