@@ -12,13 +12,13 @@ public class BinaryStream : IDisposable
12
12
public double Version ;
13
13
public bool Is32Bit ;
14
14
public ulong ImageBase ;
15
- private Stream stream ;
16
- private BinaryReader reader ;
17
- private BinaryWriter writer ;
18
- private MethodInfo readClass ;
19
- private MethodInfo readClassArray ;
20
- private Dictionary < Type , MethodInfo > genericMethodCache = new Dictionary < Type , MethodInfo > ( ) ;
21
- private Dictionary < FieldInfo , VersionAttribute [ ] > attributeCache = new Dictionary < FieldInfo , VersionAttribute [ ] > ( ) ;
15
+ private readonly Stream stream ;
16
+ private readonly BinaryReader reader ;
17
+ private readonly BinaryWriter writer ;
18
+ private readonly MethodInfo readClass ;
19
+ private readonly MethodInfo readClassArray ;
20
+ private readonly Dictionary < Type , MethodInfo > genericMethodCache ;
21
+ private readonly Dictionary < FieldInfo , VersionAttribute [ ] > attributeCache ;
22
22
23
23
public BinaryStream ( Stream input )
24
24
{
@@ -27,6 +27,8 @@ public BinaryStream(Stream input)
27
27
writer = new BinaryWriter ( stream , Encoding . UTF8 , true ) ;
28
28
readClass = GetType ( ) . GetMethod ( "ReadClass" , Type . EmptyTypes ) ;
29
29
readClassArray = GetType ( ) . GetMethod ( "ReadClassArray" , new [ ] { typeof ( long ) } ) ;
30
+ genericMethodCache = new ( ) ;
31
+ attributeCache = new ( ) ;
30
32
}
31
33
32
34
public bool ReadBoolean ( ) => reader . ReadBoolean ( ) ;
@@ -91,26 +93,17 @@ public ulong Position
91
93
92
94
private object ReadPrimitive ( Type type )
93
95
{
94
- var typename = type . Name ;
95
- switch ( typename )
96
+ return type . Name switch
96
97
{
97
- case "Int32" :
98
- return ReadInt32 ( ) ;
99
- case "UInt32" :
100
- return ReadUInt32 ( ) ;
101
- case "Int16" :
102
- return ReadInt16 ( ) ;
103
- case "UInt16" :
104
- return ReadUInt16 ( ) ;
105
- case "Byte" :
106
- return ReadByte ( ) ;
107
- case "Int64" :
108
- return ReadIntPtr ( ) ;
109
- case "UInt64" :
110
- return ReadUIntPtr ( ) ;
111
- default :
112
- throw new NotSupportedException ( ) ;
113
- }
98
+ "Int32" => ReadInt32 ( ) ,
99
+ "UInt32" => ReadUInt32 ( ) ,
100
+ "Int16" => ReadInt16 ( ) ,
101
+ "UInt16" => ReadUInt16 ( ) ,
102
+ "Byte" => ReadByte ( ) ,
103
+ "Int64" => ReadIntPtr ( ) ,
104
+ "UInt64" => ReadUIntPtr ( ) ,
105
+ _ => throw new NotSupportedException ( )
106
+ } ;
114
107
}
115
108
116
109
public T ReadClass < T > ( ulong addr ) where T : new ( )
@@ -243,15 +236,16 @@ protected virtual void Dispose(bool disposing)
243
236
{
244
237
if ( disposing )
245
238
{
246
- reader . Close ( ) ;
247
- writer . Close ( ) ;
239
+ reader . Dispose ( ) ;
240
+ writer . Dispose ( ) ;
248
241
stream . Close ( ) ;
249
242
}
250
243
}
251
244
252
245
public void Dispose ( )
253
246
{
254
247
Dispose ( true ) ;
248
+ GC . SuppressFinalize ( this ) ;
255
249
}
256
250
}
257
251
}
0 commit comments