Example #1
0
        private void WriteType(Type type)
        {
            // optimization. primitive types
            if (type == typeof(byte))
            {
                _writer.Write((byte)DataKind.UInt8);
                return;
            }

            if (type == typeof(char))
            {
                _writer.Write((byte)DataKind.Char);
                return;
            }

            int id;

            if (_dataMap.TryGetId(type, out id))
            {
                Debug.Assert(id >= 0);
                if (id <= byte.MaxValue)
                {
                    _writer.Write((byte)DataKind.TypeRef_B);
                    _writer.Write((byte)id);
                }
                else if (id <= ushort.MaxValue)
                {
                    _writer.Write((byte)DataKind.TypeRef_S);
                    _writer.Write((ushort)id);
                }
                else
                {
                    _writer.Write((byte)DataKind.TypeRef);
                    _writer.Write(id);
                }
            }
            else
            {
                _dataMap.Add(type);

                _binder?.Record(type);

                _writer.Write((byte)DataKind.Type);

                string assemblyName = type.GetTypeInfo().Assembly.FullName;
                string typeName     = type.FullName;

                // assembly name
                this.WriteString(assemblyName);

                // type name
                this.WriteString(typeName);
            }
        }
Example #2
0
        private void WriteType(Type type)
        {
            int id;

            if (_dataMap.TryGetId(type, out id))
            {
                Debug.Assert(id >= 0);
                if (id <= byte.MaxValue)
                {
                    _writer.Write((byte)DataKind.TypeRef_B);
                    _writer.Write((byte)id);
                }
                else if (id <= ushort.MaxValue)
                {
                    _writer.Write((byte)DataKind.TypeRef_S);
                    _writer.Write((ushort)id);
                }
                else
                {
                    _writer.Write((byte)DataKind.TypeRef);
                    _writer.Write(id);
                }
            }
            else
            {
                _dataMap.Add(type);

                if (_binder != null)
                {
                    _binder.Record(type);
                }

                _writer.Write((byte)DataKind.Type);

                string assemblyName = type.GetTypeInfo().Assembly.FullName;
                string typeName     = type.FullName;

                // assembly name
                this.WriteString(assemblyName);

                // type name
                this.WriteString(typeName);
            }
        }