private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory) { switch (kind) { case PathKind.Empty: return(null); case PathKind.Relative: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } // with no search paths relative paths are relative to the base directory: return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); case PathKind.RelativeToCurrentDirectory: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } if (path.Length == 1) { // "." return(baseDirectory); } else { // ".\path" return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); } case PathKind.RelativeToCurrentParent: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } // ".." return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); case PathKind.RelativeToCurrentRoot: string baseRoot; if (basePath != null) { baseRoot = PathUtilities.GetPathRoot(basePath); } else if (baseDirectory != null) { baseRoot = PathUtilities.GetPathRoot(baseDirectory); } else { return(null); } if (string.IsNullOrEmpty(baseRoot)) { return(null); } Debug.Assert(PathUtilities.IsDirectorySeparator(path[0])); Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1])); return(PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1))); case PathKind.RelativeToDriveDirectory: // drive relative paths not supported, can't resolve: return(null); case PathKind.Absolute: return(path); default: throw ExceptionUtilities.UnexpectedValue(kind); } }
private object ReadValueWorker() { var kind = (EncodingKind)_reader.ReadByte(); switch (kind) { case EncodingKind.Null: return(null); case EncodingKind.Boolean_True: return(true); case EncodingKind.Boolean_False: return(false); case EncodingKind.Int8: return(_reader.ReadSByte()); case EncodingKind.UInt8: return(_reader.ReadByte()); case EncodingKind.Int16: return(_reader.ReadInt16()); case EncodingKind.UInt16: return(_reader.ReadUInt16()); case EncodingKind.Int32: return(_reader.ReadInt32()); case EncodingKind.Int32_1Byte: return((int)_reader.ReadByte()); case EncodingKind.Int32_2Bytes: return((int)_reader.ReadUInt16()); case EncodingKind.Int32_0: case EncodingKind.Int32_1: case EncodingKind.Int32_2: case EncodingKind.Int32_3: case EncodingKind.Int32_4: case EncodingKind.Int32_5: case EncodingKind.Int32_6: case EncodingKind.Int32_7: case EncodingKind.Int32_8: case EncodingKind.Int32_9: case EncodingKind.Int32_10: return((int)kind - (int)EncodingKind.Int32_0); case EncodingKind.UInt32: return(_reader.ReadUInt32()); case EncodingKind.UInt32_1Byte: return((uint)_reader.ReadByte()); case EncodingKind.UInt32_2Bytes: return((uint)_reader.ReadUInt16()); case EncodingKind.UInt32_0: case EncodingKind.UInt32_1: case EncodingKind.UInt32_2: case EncodingKind.UInt32_3: case EncodingKind.UInt32_4: case EncodingKind.UInt32_5: case EncodingKind.UInt32_6: case EncodingKind.UInt32_7: case EncodingKind.UInt32_8: case EncodingKind.UInt32_9: case EncodingKind.UInt32_10: return((uint)((int)kind - (int)EncodingKind.UInt32_0)); case EncodingKind.Int64: return(_reader.ReadInt64()); case EncodingKind.UInt64: return(_reader.ReadUInt64()); case EncodingKind.Float4: return(_reader.ReadSingle()); case EncodingKind.Float8: return(_reader.ReadDouble()); case EncodingKind.Decimal: return(_reader.ReadDecimal()); case EncodingKind.Char: // read as ushort because BinaryWriter fails on chars that are unicode surrogates return((char)_reader.ReadUInt16()); case EncodingKind.StringUtf8: case EncodingKind.StringUtf16: case EncodingKind.StringRef_4Bytes: case EncodingKind.StringRef_1Byte: case EncodingKind.StringRef_2Bytes: return(ReadStringValue(kind)); case EncodingKind.ObjectRef_4Bytes: return(_objectReferenceMap.GetValue(_reader.ReadInt32())); case EncodingKind.ObjectRef_1Byte: return(_objectReferenceMap.GetValue(_reader.ReadByte())); case EncodingKind.ObjectRef_2Bytes: return(_objectReferenceMap.GetValue(_reader.ReadUInt16())); case EncodingKind.Object: return(ReadObject()); case EncodingKind.DateTime: return(DateTime.FromBinary(_reader.ReadInt64())); case EncodingKind.Array: case EncodingKind.Array_0: case EncodingKind.Array_1: case EncodingKind.Array_2: case EncodingKind.Array_3: return(ReadArray(kind)); default: throw ExceptionUtilities.UnexpectedValue(kind); } }
private void WritePrimitiveTypeArrayElements(Type type, EncodingKind kind, Array instance) { Debug.Assert(s_typeMap[type] == kind); // optimization for type underlying binary writer knows about if (type == typeof(byte)) { _writer.Write((byte[])instance); } else if (type == typeof(char)) { _writer.Write((char[])instance); } else if (type == typeof(string)) { // optimization for string which object writer has // its own optimization to reduce repeated string WriteStringArrayElements((string[])instance); } else if (type == typeof(bool)) { // optimization for bool array WriteBooleanArrayElements((bool[])instance); } else { // otherwise, write elements directly to underlying binary writer switch (kind) { case EncodingKind.Int8: WriteInt8ArrayElements((sbyte[])instance); return; case EncodingKind.Int16: WriteInt16ArrayElements((short[])instance); return; case EncodingKind.Int32: WriteInt32ArrayElements((int[])instance); return; case EncodingKind.Int64: WriteInt64ArrayElements((long[])instance); return; case EncodingKind.UInt16: WriteUInt16ArrayElements((ushort[])instance); return; case EncodingKind.UInt32: WriteUInt32ArrayElements((uint[])instance); return; case EncodingKind.UInt64: WriteUInt64ArrayElements((ulong[])instance); return; case EncodingKind.Float4: WriteFloat4ArrayElements((float[])instance); return; case EncodingKind.Float8: WriteFloat8ArrayElements((double[])instance); return; case EncodingKind.Decimal: WriteDecimalArrayElements((decimal[])instance); return; default: throw ExceptionUtilities.UnexpectedValue(kind); } } }
public void WriteValue(object value) { Debug.Assert(value == null || !value.GetType().GetTypeInfo().IsEnum, "Enum should not be written with WriteValue. Write them as ints instead."); if (value == null) { _writer.Write((byte)EncodingKind.Null); return; } var type = value.GetType(); var typeInfo = type.GetTypeInfo(); Debug.Assert(!typeInfo.IsEnum, "Enums should not be written with WriteObject. Write them out as integers instead."); // Perf: Note that JIT optimizes each expression value.GetType() == typeof(T) to a single register comparison. // Also the checks are sorted by commonality of the checked types. // The primitive types are // Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, // Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. if (typeInfo.IsPrimitive) { // Note: int, double, bool, char, have been chosen to go first as they're they // common values of literals in code, and so would be hte likely hits if we do // have a primitive type we're serializing out. if (value.GetType() == typeof(int)) { WriteEncodedInt32((int)value); } else if (value.GetType() == typeof(double)) { _writer.Write((byte)EncodingKind.Float8); _writer.Write((double)value); } else if (value.GetType() == typeof(bool)) { _writer.Write((byte)((bool)value ? EncodingKind.Boolean_True : EncodingKind.Boolean_False)); } else if (value.GetType() == typeof(char)) { _writer.Write((byte)EncodingKind.Char); _writer.Write((ushort)(char)value); // written as ushort because BinaryWriter fails on chars that are unicode surrogates } else if (value.GetType() == typeof(byte)) { _writer.Write((byte)EncodingKind.UInt8); _writer.Write((byte)value); } else if (value.GetType() == typeof(short)) { _writer.Write((byte)EncodingKind.Int16); _writer.Write((short)value); } else if (value.GetType() == typeof(long)) { _writer.Write((byte)EncodingKind.Int64); _writer.Write((long)value); } else if (value.GetType() == typeof(sbyte)) { _writer.Write((byte)EncodingKind.Int8); _writer.Write((sbyte)value); } else if (value.GetType() == typeof(float)) { _writer.Write((byte)EncodingKind.Float4); _writer.Write((float)value); } else if (value.GetType() == typeof(ushort)) { _writer.Write((byte)EncodingKind.UInt16); _writer.Write((ushort)value); } else if (value.GetType() == typeof(uint)) { WriteEncodedUInt32((uint)value); } else if (value.GetType() == typeof(ulong)) { _writer.Write((byte)EncodingKind.UInt64); _writer.Write((ulong)value); } else { throw ExceptionUtilities.UnexpectedValue(value.GetType()); } } else if (value.GetType() == typeof(decimal)) { _writer.Write((byte)EncodingKind.Decimal); _writer.Write((decimal)value); } else if (value.GetType() == typeof(DateTime)) { _writer.Write((byte)EncodingKind.DateTime); _writer.Write(((DateTime)value).ToBinary()); } else if (value.GetType() == typeof(string)) { WriteStringValue((string)value); } else if (type.IsArray) { var instance = (Array)value; if (instance.Rank > 1) { throw new InvalidOperationException(Resources.Arrays_with_more_than_one_dimension_cannot_be_serialized); } WriteArray(instance); } else { WriteObject(instance: value, instanceAsWritableOpt: null); } }
private static string BuildDisplayName( string name, Version version, string cultureName, ImmutableArray <byte> publicKeyOrToken, bool hasPublicKey, bool isRetargetable, AssemblyContentType contentType) { PooledStringBuilder pooledBuilder = PooledStringBuilder.GetInstance(); var sb = pooledBuilder.Builder; EscapeName(sb, name); sb.Append(", Version="); sb.Append(version.Major); sb.Append("."); sb.Append(version.Minor); sb.Append("."); sb.Append(version.Build); sb.Append("."); sb.Append(version.Revision); sb.Append(", Culture="); if (cultureName.Length == 0) { sb.Append(InvariantCultureDisplay); } else { EscapeName(sb, cultureName); } if (hasPublicKey) { sb.Append(", PublicKey="); AppendKey(sb, publicKeyOrToken); } else { sb.Append(", PublicKeyToken="); if (publicKeyOrToken.Length > 0) { AppendKey(sb, publicKeyOrToken); } else { sb.Append("null"); } } if (isRetargetable) { sb.Append(", Retargetable=Yes"); } switch (contentType) { case AssemblyContentType.Default: break; case AssemblyContentType.WindowsRuntime: sb.Append(", ContentType=WindowsRuntime"); break; default: throw ExceptionUtilities.UnexpectedValue(contentType); } string result = sb.ToString(); pooledBuilder.Free(); return(result); }
/// <summary> /// Read a value from the stream. The value must have been written using ObjectWriter.WriteValue. /// </summary> public object ReadValue() { DataKind kind = (DataKind)reader.ReadByte(); switch (kind) { case DataKind.Null: return(null); case DataKind.Boolean_T: return(BooleanTrue); case DataKind.Boolean_F: return(BooleanFalse); case DataKind.Int8: return(reader.ReadSByte()); case DataKind.UInt8: return(reader.ReadByte()); case DataKind.Int16: return(reader.ReadInt16()); case DataKind.UInt16: return(reader.ReadUInt16()); case DataKind.Int32: return(reader.ReadInt32()); case DataKind.Int32_B: return((int)reader.ReadByte()); case DataKind.Int32_S: return((int)reader.ReadUInt16()); case DataKind.Int32_Z: return(Int32Zero); case DataKind.UInt32: return(reader.ReadUInt32()); case DataKind.Int64: return(reader.ReadInt64()); case DataKind.UInt64: return(reader.ReadUInt64()); case DataKind.Float4: return(reader.ReadSingle()); case DataKind.Float8: return(reader.ReadDouble()); case DataKind.Decimal: return(reader.ReadDecimal()); case DataKind.DateTime: return(this.ReadDateTime()); case DataKind.Char: return(this.ReadChar()); case DataKind.String: case DataKind.StringRef: case DataKind.StringRef_B: case DataKind.StringRef_S: return(ReadString(kind)); case DataKind.Object_W: case DataKind.ObjectRef: case DataKind.ObjectRef_B: case DataKind.ObjectRef_S: return(ReadObject(kind)); case DataKind.Type: case DataKind.TypeRef: case DataKind.TypeRef_B: case DataKind.TypeRef_S: return(ReadType(kind)); case DataKind.Enum: return(ReadEnum()); case DataKind.Array: case DataKind.Array_0: case DataKind.Array_1: case DataKind.Array_2: case DataKind.Array_3: return(ReadArray(kind)); default: throw ExceptionUtilities.UnexpectedValue(kind); } }