Skip to content

Commit f102cdb

Browse files
authored
Add attributes for serialization hints (#1)
1 parent a3bc60f commit f102cdb

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
namespace System.Runtime.Serialization
8+
{
9+
/// <summary>
10+
/// Indicates that this field doesn't allow reflection.
11+
/// </summary>
12+
[AttributeUsage(AttributeTargets.Field, Inherited = false)]
13+
sealed public class FieldNoReflectionAttribute : Attribute
14+
{
15+
}
16+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
namespace System.Runtime.Serialization
8+
{
9+
/// <summary>
10+
/// Provides hints to the binary serializer on how to improve serialization and decrease the size of the serialialized representation.
11+
/// </summary>
12+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, Inherited = true)]
13+
public class SerializationHintsAttribute : Attribute
14+
{
15+
//
16+
// Keep in sync with Microsoft.SPOT.Debugger.SerializationHintsAttribute!!!!
17+
//
18+
19+
#pragma warning disable S1104 // intended use in .NET nanoFramework for native usage
20+
21+
/// <summary>
22+
/// Serialization options for the current object.
23+
/// </summary>
24+
public SerializationOptions Options;
25+
26+
/// <summary>
27+
/// Specifies the size of an array.
28+
/// </summary>
29+
/// <remarks>
30+
/// If an array's size is fixed and known, it can be stated using the <see cref="ArraySize"/> option. A value of -1 can be used if the class being serialized only has one array of simple data types and no other fields.
31+
/// </remarks>
32+
//// -1 == extend to the end of the stream.
33+
public int ArraySize;
34+
35+
/// <summary>
36+
/// Specifies the number of bits in which the current object is bit-packed.
37+
/// </summary>
38+
/// <remarks>
39+
/// This can be applied only to ordinal types, <see cref="DateTime"/> and <see cref="TimeSpan"/>.
40+
/// This indicates how many bits should be kept for an ordinal type. If <see cref="BitPacked"/> is 0, a data type's default size is applied (for example, 2 bytes for an <see cref="short"/> and 1 byte for the <see cref="byte"/> type).
41+
/// Decorating a <see cref="bool"/> field with <see cref="BitPacked"/> attribute set to 1, only one bit - which is enough to represent true or false - is kept for the <see cref="bool"/> value.
42+
/// </remarks>
43+
//// bits count
44+
public int BitPacked;
45+
46+
/// <summary>
47+
/// Specifies the range bias adjustment for a particular serialized value.
48+
/// </summary>
49+
/// <remarks>
50+
/// This can be applied to ordinal types, <see cref="DateTime"/> and <see cref="TimeSpan"/>.
51+
/// It can't be applied to <see cref="bool"/> type objects.
52+
/// With the exception of <see cref="bool"/>, all ordinal types can use <see cref="RangeBias"/>. It allows to store a value using fewer bits. Before saving the ordinal value, the range bias value is subtracted from it. For instance, a 16 bit data type has to used to store values that range between 1000 and 1500. The required bits can be decreased from 16 to 6 if the range is known in advance. When <see cref="RangeBias"/> is set to 1000, the number 1000 is subtracted from the original value before it's serialized.
53+
/// </remarks>
54+
public long RangeBias;
55+
56+
/// <summary>
57+
/// Specifies the range bias adjustment for a particular serialized value.
58+
/// </summary>
59+
/// <remarks>
60+
/// This can be applied to ordinal types, <see cref="DateTime"/> and <see cref="TimeSpan"/>.
61+
/// It can't be applied to <see cref="bool"/> type objects.
62+
/// When serializing a value, <see cref="Scale"/> helps save storage bits similar to <see cref="RangeBias"/>. The value to be serialized is divided by <see cref="Scale"/>. Defining a <see cref="Scale"/> of two, the range of values can be cut in half if a field only contains odd or even values. It's also possible to combine <see cref="RangeBias"/> with <see cref="Scale"/>. However, the order of operations must be taken into account here. The value will be first subtracted by <see cref="RangeBias"/> and then divided by <see cref="Scale"/>.
63+
/// </remarks>
64+
//// this will be ticks for DateTime objects
65+
public ulong Scale;
66+
67+
#pragma warning restore S1104 // Fields should not have public accessibility
68+
69+
}
70+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
4+
// See LICENSE file in the project root for full license information.
5+
//
6+
7+
namespace System.Runtime.Serialization
8+
{
9+
/// <summary>
10+
/// Options for serialization and deserialization.
11+
/// </summary>
12+
[Flags()]
13+
public enum SerializationOptions
14+
{
15+
/// <summary>
16+
/// A value indicating that the pointer to the serialized object is never <see langword="null"/>.
17+
/// </summary>
18+
PointerNeverNull = 0x00000010,
19+
20+
/// <summary>
21+
/// A value indicating that the elements in the serialized object are never <see langword="null"/>.
22+
/// </summary>
23+
ElementsNeverNull = 0x00000020,
24+
25+
/// <summary>
26+
/// A value indicating that the serialized object can only be an instance of the specified class, and not an instance of a derived class.
27+
/// </summary>
28+
FixedType = 0x00000100,
29+
}
30+
}

nanoFramework.System.Runtime.Serialization/System.Runtime.Serialization.nfproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@
4040
<NFMDP_PE_ExcludeClassByName Include="ThisAssembly">
4141
<InProject>false</InProject>
4242
</NFMDP_PE_ExcludeClassByName>
43+
<NFMDP_PE_ExcludeClassByName Include="System.Runtime.Serialization.FieldNoReflectionAttribute">
44+
<InProject>false</InProject>
45+
</NFMDP_PE_ExcludeClassByName>
4346
</ItemGroup>
4447
<ItemGroup>
48+
<Compile Include="FieldNoReflectionAttribute.cs" />
49+
<Compile Include="SerializationHintsAttribute.cs" />
50+
<Compile Include="SerializationOptions.cs" />
4551
<Compile Include="SerializationException.cs" />
4652
<Compile Include="BinaryFormatter.cs" />
4753
<Compile Include="Properties\AssemblyInfo.cs" />

0 commit comments

Comments
 (0)