Skip to content

Commit bd340f2

Browse files
committed
ComparisonRule: Manual serialization
Due to CultureInfo is not marked as Serializable we have to serialize it manually
1 parent 6b4454a commit bd340f2

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

Orm/Xtensive.Orm/Comparison/ComparisonRule.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// Copyright (C) 2003-2010 Xtensive LLC.
1+
// Copyright (C) 2008-2021 Xtensive LLC.
22
// All rights reserved.
33
// For conditions of distribution and use, see license.
44
// Created by: Alex Yakunin
55
// Created: 2008.02.05
66

77
using System;
88
using System.Globalization;
9+
using System.Runtime.Serialization;
910
using Xtensive.Core;
1011

1112

@@ -16,8 +17,9 @@ namespace Xtensive.Comparison
1617
/// Describes how to compare values of comparable objects.
1718
/// </summary>
1819
[Serializable]
19-
public struct ComparisonRule :
20-
IEquatable<ComparisonRule>
20+
public struct ComparisonRule :
21+
IEquatable<ComparisonRule>,
22+
ISerializable
2123
{
2224
/// <summary>
2325
/// Predefined rule with <see cref="Direction"/> = <see cref="Core.Direction.None"/>.
@@ -50,7 +52,7 @@ public struct ComparisonRule :
5052
/// </summary>
5153
/// <returns>The same rule, but with inverted direction.</returns>
5254
public ComparisonRule Invert()
53-
{
55+
{
5456
return new ComparisonRule((Direction)(-((int)Direction)), Culture);
5557
}
5658

@@ -188,5 +190,26 @@ public ComparisonRule(Direction direction, CultureInfo culture)
188190
Direction = direction;
189191
Culture = culture;
190192
}
193+
194+
private ComparisonRule(SerializationInfo info, StreamingContext context)
195+
{
196+
if (info == null) {
197+
throw new ArgumentNullException(nameof(info));
198+
}
199+
Direction = (Direction) info.GetSByte(nameof(Direction));
200+
var cultureId = info.GetInt32(nameof(Culture));
201+
Culture = (cultureId != int.MinValue) ? CultureInfo.GetCultureInfo(cultureId) : null;
202+
}
203+
204+
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
205+
{
206+
if(info == null) {
207+
throw new ArgumentNullException(nameof(info));
208+
}
209+
info.AddValue(nameof(Direction), (sbyte) Direction);
210+
211+
var cultureId = (Culture != null) ? Culture.LCID : int.MinValue;
212+
info.AddValue(nameof(Culture), cultureId);
213+
}
191214
}
192215
}

0 commit comments

Comments
 (0)