public static int GetAlignment(Type type)
        {
            if (type.GetTypeInfo().IsEnum)
            {
                type = Enum.GetUnderlyingType(type);
            }

            if (type == typeof(bool))
            {
                return(GetAlignment(DType.Boolean));
            }
            else if (type == typeof(byte))
            {
                return(GetAlignment(DType.Byte));
            }
            else if (type == typeof(double))
            {
                return(GetAlignment(DType.Double));
            }
            else if (type == typeof(short))
            {
                return(GetAlignment(DType.Int16));
            }
            else if (type == typeof(int))
            {
                return(GetAlignment(DType.Int32));
            }
            else if (type == typeof(long))
            {
                return(GetAlignment(DType.Int64));
            }
            else if (type == typeof(ObjectPath))
            {
                return(GetAlignment(DType.ObjectPath));
            }
            else if (type == typeof(Signature))
            {
                return(GetAlignment(DType.Signature));
            }
            else if (type == typeof(string))
            {
                return(GetAlignment(DType.String));
            }
            else if (type == typeof(float))
            {
                return(GetAlignment(DType.Single));
            }
            else if (type == typeof(ushort))
            {
                return(GetAlignment(DType.UInt16));
            }
            else if (type == typeof(uint))
            {
                return(GetAlignment(DType.UInt32));
            }
            else if (type == typeof(ulong))
            {
                return(GetAlignment(DType.UInt64));
            }
            else if (type == typeof(object))
            {
                return(GetAlignment(DType.Variant));
            }
            else if (type == typeof(IDBusObject))
            {
                return(GetAlignment(DType.Variant));
            }

            if (ArgTypeInspector.IsDBusObjectType(type, isCompileTimeType: true))
            {
                return(GetAlignment(DType.Variant));
            }

            Type elementType;

            if (ArgTypeInspector.InspectEnumerableType(type, out elementType, isCompileTimeType: true)
                != ArgTypeInspector.EnumerableType.NotEnumerable)
            {
                return(GetAlignment(DType.Array));
            }

            if (ArgTypeInspector.IsStructType(type))
            {
                return(GetAlignment(DType.StructBegin));
            }

            if (ArgTypeInspector.IsSafeHandleType(type))
            {
                return(GetAlignment(DType.UnixFd));
            }

            throw new ArgumentException($"Cannot (de)serialize Type '{type.FullName}'");
        }
Exemple #2
0
        public void Write(Type type, object val, bool isCompileTimeType)
        {
            if (type.GetTypeInfo().IsEnum)
            {
                type = Enum.GetUnderlyingType(type);
            }

            if (type == typeof(bool))
            {
                WriteBoolean((bool)val);
                return;
            }
            else if (type == typeof(byte))
            {
                WriteByte((byte)val);
                return;
            }
            else if (type == typeof(double))
            {
                WriteDouble((double)val);
                return;
            }
            else if (type == typeof(short))
            {
                WriteInt16((short)val);
                return;
            }
            else if (type == typeof(int))
            {
                WriteInt32((int)val);
                return;
            }
            else if (type == typeof(long))
            {
                WriteInt64((long)val);
                return;
            }
            else if (type == typeof(ObjectPath))
            {
                WriteObjectPath((ObjectPath)val);
                return;
            }
            else if (type == typeof(Signature))
            {
                WriteSignature((Signature)val);
                return;
            }
            else if (type == typeof(string))
            {
                WriteString((string)val);
                return;
            }
            else if (type == typeof(float))
            {
                WriteSingle((float)val);
                return;
            }
            else if (type == typeof(ushort))
            {
                WriteUInt16((ushort)val);
                return;
            }
            else if (type == typeof(uint))
            {
                WriteUInt32((uint)val);
                return;
            }
            else if (type == typeof(ulong))
            {
                WriteUInt64((ulong)val);
                return;
            }
            else if (type == typeof(object))
            {
                WriteVariant(val);
                return;
            }
            else if (type == typeof(IDBusObject))
            {
                WriteBusObject((IDBusObject)val);
                return;
            }

            if (ArgTypeInspector.IsDBusObjectType(type, isCompileTimeType))
            {
                WriteBusObject((IDBusObject)val);
                return;
            }

            if (ArgTypeInspector.IsSafeHandleType(type))
            {
                WriteSafeHandle((SafeHandle)val);
                return;
            }

            MethodInfo method = WriteMethodFactory.CreateWriteMethodForType(type, isCompileTimeType);

            if (method.IsStatic)
            {
                method.Invoke(null, new object[] { this, val });
            }
            else
            {
                method.Invoke(this, new object[] { val });
            }
        }