@@ -28,7 +28,6 @@ public static BinaryStorage Get(string filePath)
2828 public static Builder Construct ( string filePath )
2929 {
3030 ThrowIfFilePathLocked ( filePath ) ;
31- LockFilePathInEditor ( filePath ) ;
3231 return new Builder ( filePath ) ;
3332 }
3433
@@ -114,12 +113,21 @@ public Builder AddPrimitiveTypes()
114113 /// <param name="typeSerializer">The serializer for the specified type.</param>
115114 /// <returns>The current <see cref="Builder"/> instance for method chaining.</returns>
116115 /// <exception cref="DuplicateTypeSerializerException">Thrown if a serializer for the specified type already exists.</exception>
116+ /// <exception cref="DuplicateTypeSerializerNameException">Thrown if a serializer with the same type name already exists.</exception>
117117 public Builder AddTypeSerializer < T > ( TypeSerializer < T > typeSerializer )
118118 {
119- if ( _serializers . Any ( c => c is TypedBinarySection < T > ) )
119+ var otherSerializer = _serializers . FirstOrDefault ( c => c is TypedBinarySection < T > ) ? . Serializer ;
120+ if ( otherSerializer != null )
120121 {
121- throw new DuplicateTypeSerializerException ( typeof ( T ) , typeSerializer . TypeName , _filePath ) ;
122+ throw new DuplicateTypeSerializerException ( typeSerializer , otherSerializer , _filePath ) ;
122123 }
124+
125+ otherSerializer = _serializers . FirstOrDefault ( c => c . Serializer . TypeName == typeSerializer . TypeName ) ? . Serializer ;
126+ if ( otherSerializer != null )
127+ {
128+ throw new DuplicateTypeSerializerNameException ( typeSerializer , otherSerializer , _filePath ) ;
129+ }
130+
123131 _serializers . Add ( new TypedBinarySection < T > ( typeSerializer ) ) ;
124132 return this ;
125133 }
@@ -217,20 +225,13 @@ public Builder SupportDictionariesOf<TKey, TValue>()
217225 /// <exception cref="IOException"> An I/O error occurred </exception>
218226 public BinaryStorage Build ( KeyLoadFailedBehaviour keyLoadFailedBehaviour = KeyLoadFailedBehaviour . IgnoreWithWarning )
219227 {
220- try
221- {
222- var storage = new BinaryStorage ( _filePath , _serializers ) ;
223- storage . AutoSave = _autoSave ;
224- storage . MissingKeyBehavior = _missingKeyBehavior ;
225- storage . TypeMismatchBehaviour = _typeMismatchBehaviour ;
226- storage . LoadDataFromDisk ( keyLoadFailedBehaviour ) ;
227- return storage ;
228- }
229- catch
230- {
231- UnlockFilePathInEditor ( _filePath ) ;
232- throw ;
233- }
228+ var storage = new BinaryStorage ( _filePath , _serializers ) ;
229+ storage . AutoSave = _autoSave ;
230+ storage . MissingKeyBehavior = _missingKeyBehavior ;
231+ storage . TypeMismatchBehaviour = _typeMismatchBehaviour ;
232+ storage . LoadDataFromDisk ( keyLoadFailedBehaviour ) ;
233+ LockFilePathInEditor ( _filePath ) ;
234+ return storage ;
234235 }
235236 }
236237 }
0 commit comments