Skip to content

Commit 9670ca0

Browse files
committed
Update to Roslyn 5.3.0
1 parent 13d55c8 commit 9670ca0

20 files changed

Lines changed: 395 additions & 48 deletions

ThreatDragonModels/New Threat Model/New Threat Model.json

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/CSharpScriptSerializer/ArrayCSScriptSerializer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@
88

99
namespace CSharpScriptSerialization
1010
{
11+
/// <summary>
12+
/// Serializes arrays (including multi-dimensional and jagged arrays) as C# array creation expressions.
13+
/// </summary>
1114
public class ArrayCSScriptSerializer : CSScriptSerializer
1215
{
16+
/// <summary>
17+
/// Creates a new instance of <see cref="ArrayCSScriptSerializer"/> for the given array type.
18+
/// </summary>
19+
/// <param name="type">The array <see cref="System.Type"/> to serialize.</param>
1320
public ArrayCSScriptSerializer(Type type)
1421
: base(type)
1522
{

src/CSharpScriptSerializer/CSScriptSerializer.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@
1717

1818
namespace CSharpScriptSerialization
1919
{
20+
/// <summary>
21+
/// Abstract base class for all C# script serializers. Provides static factory methods to
22+
/// <see cref="Serialize">serialize</see> .NET objects to C# script strings and
23+
/// <see cref="DeserializeAsync{T}(string)">deserialize</see> them back by evaluating the script.
24+
/// </summary>
2025
public abstract class CSScriptSerializer : ICSScriptSerializer
2126
{
27+
/// <summary>
28+
/// A list of <see cref="ICSScriptSerializerFactory"/> instances consulted in order when no serializer
29+
/// is found in <see cref="Serializers"/> for a given type. Add factories here to support additional types.
30+
/// </summary>
2231
public static readonly List<ICSScriptSerializerFactory> SerializerFactories =
2332
new List<ICSScriptSerializerFactory>();
2433

34+
/// <summary>
35+
/// A cache of <see cref="ICSScriptSerializer"/> instances keyed by <see cref="System.Type"/>.
36+
/// Pre-populate entries here to override the default serializer for specific types.
37+
/// </summary>
2538
public static readonly ConcurrentDictionary<Type, ICSScriptSerializer> Serializers =
2639
new ConcurrentDictionary<Type, ICSScriptSerializer>();
2740

@@ -30,19 +43,49 @@ public abstract class CSScriptSerializer : ICSScriptSerializer
3043

3144
protected CSScriptSerializer(Type type) => Type = type;
3245

46+
/// <summary>
47+
/// Gets the CLR <see cref="System.Type"/> this serializer handles.
48+
/// </summary>
3349
public Type Type { get; }
3450

51+
/// <inheritdoc/>
3552
public abstract ExpressionSyntax GetCreation(object obj);
3653

54+
/// <summary>
55+
/// Evaluates a C# script string and returns the result as <typeparamref name="T"/>.
56+
/// </summary>
57+
/// <typeparam name="T">The expected type of the evaluated result.</typeparam>
58+
/// <param name="script">The C# script to evaluate.</param>
3759
public static T Deserialize<T>(string script) => DeserializeAsync<T>(script).GetAwaiter().GetResult();
3860

61+
/// <summary>
62+
/// Asynchronously evaluates a C# script string and returns the result as <typeparamref name="T"/>.
63+
/// </summary>
64+
/// <typeparam name="T">The expected type of the evaluated result.</typeparam>
65+
/// <param name="script">The C# script to evaluate.</param>
3966
public static Task<T> DeserializeAsync<T>(string script)
4067
=> DeserializeAsync<T>(script, Enumerable.Empty<Assembly>(), Enumerable.Empty<string>());
4168

69+
/// <summary>
70+
/// Evaluates a C# script string with additional assembly references and namespace imports,
71+
/// and returns the result as <typeparamref name="T"/>.
72+
/// </summary>
73+
/// <typeparam name="T">The expected type of the evaluated result.</typeparam>
74+
/// <param name="script">The C# script to evaluate.</param>
75+
/// <param name="referencedAssemblies">Additional assemblies to reference during evaluation.</param>
76+
/// <param name="imports">Additional namespaces to import during evaluation.</param>
4277
public static T Deserialize<T>(string script, IEnumerable<Assembly> referencedAssemblies,
4378
IEnumerable<string> imports)
4479
=> DeserializeAsync<T>(script, referencedAssemblies, imports).GetAwaiter().GetResult();
4580

81+
/// <summary>
82+
/// Asynchronously evaluates a C# script string with additional assembly references and namespace imports,
83+
/// and returns the result as <typeparamref name="T"/>.
84+
/// </summary>
85+
/// <typeparam name="T">The expected type of the evaluated result.</typeparam>
86+
/// <param name="script">The C# script to evaluate.</param>
87+
/// <param name="referencedAssemblies">Additional assemblies to reference during evaluation.</param>
88+
/// <param name="imports">Additional namespaces to import during evaluation.</param>
4689
public static Task<T> DeserializeAsync<T>(
4790
string script, IEnumerable<Assembly> referencedAssemblies, IEnumerable<string> imports)
4891
=> CSharpScript.EvaluateAsync<T>(script,
@@ -60,6 +103,15 @@ public static Task<T> DeserializeAsync<T>(
60103
typeof(List<>).GetTypeInfo().Namespace)
61104
.AddImports(imports));
62105

106+
/// <summary>
107+
/// Serializes <paramref name="obj"/> to a formatted C# script string.
108+
/// </summary>
109+
/// <param name="obj">The object to serialize.</param>
110+
/// <param name="applyFormattingOptions">
111+
/// An optional delegate to customize the Roslyn formatting options applied to the output.
112+
/// When <see langword="null"/>, a sensible default set of options is used.
113+
/// </param>
114+
/// <returns>A C# script string that, when evaluated, recreates <paramref name="obj"/>.</returns>
63115
public static string Serialize(object obj, Func<OptionSet, OptionSet> applyFormattingOptions = null)
64116
{
65117
using (var workspace = new AdhocWorkspace())
@@ -79,13 +131,22 @@ public static string Serialize(object obj, Func<OptionSet, OptionSet> applyForma
79131
}
80132
}
81133

134+
/// <summary>
135+
/// Returns the full Roslyn <see cref="CompilationUnitSyntax"/> that wraps the creation expression for
136+
/// <paramref name="obj"/> as a top-level global statement.
137+
/// </summary>
138+
/// <param name="obj">The object to serialize.</param>
82139
public static CompilationUnitSyntax GetCompilationUnitExpression(object obj) => CompilationUnit()
83140
.WithMembers(
84141
SingletonList<MemberDeclarationSyntax>(
85142
GlobalStatement(
86143
ExpressionStatement(GetCreationExpression(obj))
87144
.WithSemicolonToken(MissingToken(SyntaxKind.SemicolonToken)))));
88145

146+
/// <summary>
147+
/// Returns the Roslyn <see cref="ExpressionSyntax"/> that represents the creation of <paramref name="obj"/>.
148+
/// </summary>
149+
/// <param name="obj">The object to serialize.</param>
89150
public static ExpressionSyntax GetCreationExpression(object obj) => GetSerializer(obj).GetCreation(obj);
90151

91152
private static ICSScriptSerializer GetSerializer(object obj)

src/CSharpScriptSerializer/CSharpScriptSerializer.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
<PackageId>CSharpScriptSerializer</PackageId>
66
<AssemblyTitle>CSharpScriptSerializer</AssemblyTitle>
77
<Title>CSharpScriptSerializer</Title>
8-
<VersionPrefix>3.0.4</VersionPrefix>
8+
<VersionPrefix>4.0.0</VersionPrefix>
99
<TargetFrameworks>netstandard2.1</TargetFrameworks>
10-
<NetStandardImplicitPackageVersion>2.0.3</NetStandardImplicitPackageVersion>
1110
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
1211
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
1312
<Description>Serialize objects to C# scripts</Description>
1413
<Authors>Andriy Svyryd</Authors>
1514
<PackageTags>Roslyn;CSharp;C#;CSX;Script;Serialization</PackageTags>
1615
<PackageReleaseNotes>
1716
<![CDATA[
17+
Version 4.0.0
18+
* Update to Roslyn V5.3.0
1819
Version 3.0.4
1920
* Simplify empty object initializers
2021
Version 3.0.3
@@ -46,8 +47,8 @@ Version 1.4.0
4647
</PropertyGroup>
4748

4849
<ItemGroup>
49-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.2.0" />
50-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.2.0" />
50+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="5.3.0" />
51+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="5.3.0" />
5152
</ItemGroup>
5253

5354
</Project>

src/CSharpScriptSerializer/CollectionCSScriptSerializer.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,78 @@
77

88
namespace CSharpScriptSerialization
99
{
10+
/// <summary>
11+
/// Serializes a collection of type <typeparamref name="T"/> as a constructor invocation with
12+
/// a collection initializer expression.
13+
/// </summary>
14+
/// <typeparam name="T">The collection type to serialize.</typeparam>
1015
public class CollectionCSScriptSerializer<T> : ConstructorCSScriptSerializer<T>
1116
{
1217
private readonly IReadOnlyCollection<Func<object, object>> _elementDecomposers;
1318
private readonly Func<T, IEnumerable<object>> _getEnumerable;
1419

20+
/// <summary>
21+
/// Creates a new instance of <see cref="CollectionCSScriptSerializer{T}"/> that serializes each element
22+
/// directly, with no constructor arguments and using <see cref="System.Collections.IEnumerable"/> to enumerate.
23+
/// </summary>
1524
public CollectionCSScriptSerializer()
1625
: this(elementDecomposers: null, constructorParameterGetters: null)
1726
{
1827
}
1928

29+
/// <summary>
30+
/// Creates a new instance of <see cref="CollectionCSScriptSerializer{T}"/> with custom element decomposers.
31+
/// </summary>
32+
/// <param name="elementDecomposers">
33+
/// Functions that decompose each element into sub-values for a complex collection initializer entry
34+
/// (e.g., key and value for a dictionary). Pass <see langword="null"/> or empty to serialize each element directly.
35+
/// </param>
2036
public CollectionCSScriptSerializer(
2137
IReadOnlyCollection<Func<object, object>> elementDecomposers)
2238
: this(elementDecomposers, constructorParameterGetters: null)
2339
{
2440
}
2541

42+
/// <summary>
43+
/// Creates a new instance of <see cref="CollectionCSScriptSerializer{T}"/> with strongly-typed constructor
44+
/// parameter getters.
45+
/// </summary>
46+
/// <param name="constructorParameterGetters">
47+
/// Getters for values passed as positional constructor arguments when creating <typeparamref name="T"/>.
48+
/// </param>
2649
public CollectionCSScriptSerializer(
2750
IReadOnlyCollection<Func<T, object>> constructorParameterGetters)
2851
: this(elementDecomposers: null, constructorParameterGetters: constructorParameterGetters)
2952
{
3053
}
3154

55+
/// <summary>
56+
/// Creates a new instance of <see cref="CollectionCSScriptSerializer{T}"/> with element decomposers
57+
/// and a non-generic enumerable getter.
58+
/// </summary>
59+
/// <param name="elementDecomposers">
60+
/// Functions that decompose each element into sub-values for a complex collection initializer entry.
61+
/// </param>
62+
/// <param name="getEnumerable">
63+
/// A function that extracts the sequence of elements from an instance of <typeparamref name="T"/>.
64+
/// </param>
3265
public CollectionCSScriptSerializer(
3366
IReadOnlyCollection<Func<object, object>> elementDecomposers,
3467
Func<object, IEnumerable<object>> getEnumerable)
3568
: this(elementDecomposers, nonGenericParameterGetters: null, nonGenericGetEnumerable: getEnumerable)
3669
{
3770
}
3871

72+
/// <summary>
73+
/// Creates a new instance of <see cref="CollectionCSScriptSerializer{T}"/> with strongly-typed constructor
74+
/// parameter getters and an enumerable getter.
75+
/// </summary>
76+
/// <param name="constructorParameterGetters">
77+
/// Getters for values passed as positional constructor arguments when creating <typeparamref name="T"/>.
78+
/// </param>
79+
/// <param name="getEnumerable">
80+
/// A function that extracts the sequence of elements from an instance of <typeparamref name="T"/>.
81+
/// </param>
3982
public CollectionCSScriptSerializer(
4083
IReadOnlyCollection<Func<T, object>> constructorParameterGetters,
4184
Func<T, IEnumerable<object>> getEnumerable)
@@ -46,13 +89,33 @@ public CollectionCSScriptSerializer(
4689
{
4790
}
4891

92+
/// <summary>
93+
/// Creates a new instance of <see cref="CollectionCSScriptSerializer{T}"/> with element decomposers
94+
/// and non-generic constructor parameter getters.
95+
/// </summary>
96+
/// <param name="elementDecomposers">
97+
/// Functions that decompose each element into sub-values for a complex collection initializer entry.
98+
/// </param>
99+
/// <param name="nonGenericParameterGetters">
100+
/// Non-generic getters for values passed as positional constructor arguments.
101+
/// </param>
49102
public CollectionCSScriptSerializer(
50103
IReadOnlyCollection<Func<object, object>> elementDecomposers,
51104
IReadOnlyCollection<Func<object, object>> nonGenericParameterGetters)
52105
: this(elementDecomposers, nonGenericParameterGetters, nonGenericGetEnumerable: null)
53106
{
54107
}
55108

109+
/// <summary>
110+
/// Creates a new instance of <see cref="CollectionCSScriptSerializer{T}"/> with element decomposers
111+
/// and strongly-typed constructor parameter getters.
112+
/// </summary>
113+
/// <param name="elementDecomposers">
114+
/// Functions that decompose each element into sub-values for a complex collection initializer entry.
115+
/// </param>
116+
/// <param name="constructorParameterGetters">
117+
/// Getters for values passed as positional constructor arguments when creating <typeparamref name="T"/>.
118+
/// </param>
56119
public CollectionCSScriptSerializer(
57120
IReadOnlyCollection<Func<object, object>> elementDecomposers,
58121
IReadOnlyCollection<Func<T, object>> constructorParameterGetters)

src/CSharpScriptSerializer/ConstructorCSScriptSerializer.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,57 @@
66

77
namespace CSharpScriptSerialization
88
{
9+
/// <summary>
10+
/// Serializes objects of type <typeparamref name="T"/> as a constructor invocation expression,
11+
/// passing values derived from the object as positional arguments.
12+
/// </summary>
13+
/// <typeparam name="T">The type of object to serialize.</typeparam>
914
public class ConstructorCSScriptSerializer<T> : CSScriptSerializer
1015
{
1116
private readonly IReadOnlyCollection<Func<T, object>> _constructorParameterGetters;
1217
private ObjectCreationExpressionSyntax _objectCreationExpression;
1318

19+
/// <summary>
20+
/// Creates a new instance of <see cref="ConstructorCSScriptSerializer{T}"/> that emits a
21+
/// no-argument constructor call.
22+
/// </summary>
1423
public ConstructorCSScriptSerializer()
1524
: this(constructorParameterGetters: null)
1625
{
1726
}
1827

28+
/// <summary>
29+
/// Creates a new instance of <see cref="ConstructorCSScriptSerializer{T}"/> using non-generic
30+
/// parameter getters.
31+
/// </summary>
32+
/// <param name="nonGenericParameterGetters">
33+
/// Functions that accept the object as <see cref="object"/> and return the value for each
34+
/// positional constructor argument.
35+
/// </param>
1936
public ConstructorCSScriptSerializer(IReadOnlyCollection<Func<object, object>> nonGenericParameterGetters)
2037
: this(nonGenericParameterGetters
2138
.Select<Func<object, object>, Func<T, object>>(g => o => g(o)).ToArray())
2239
{
2340
}
2441

42+
/// <summary>
43+
/// Creates a new instance of <see cref="ConstructorCSScriptSerializer{T}"/> using strongly-typed
44+
/// parameter getters.
45+
/// </summary>
46+
/// <param name="constructorParameterGetters">
47+
/// Getters for values passed as positional constructor arguments when creating <typeparamref name="T"/>.
48+
/// Pass <see langword="null"/> or an empty collection to emit a no-argument constructor call.
49+
/// </param>
2550
public ConstructorCSScriptSerializer(IReadOnlyCollection<Func<T, object>> constructorParameterGetters)
2651
: base(typeof(T)) => _constructorParameterGetters = constructorParameterGetters;
2752

53+
/// <summary>
54+
/// Gets a value indicating whether to emit an empty argument list <c>()</c> when no constructor
55+
/// parameters are provided. Defaults to <see langword="true"/>.
56+
/// </summary>
2857
protected virtual bool GenerateEmptyArgumentList => true;
2958

59+
/// <inheritdoc/>
3060
public override ExpressionSyntax GetCreation(object obj) => GetObjectCreationExpression((T)obj);
3161

3262
protected virtual ObjectCreationExpressionSyntax GetObjectCreationExpression(T obj)

src/CSharpScriptSerializer/EnumCSScriptSerializer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66

77
namespace CSharpScriptSerialization
88
{
9+
/// <summary>
10+
/// Serializes enum values as member-access expressions (e.g., <c>MyEnum.Value</c>).
11+
/// Composite flag values are represented as bitwise-OR combinations of their constituent members.
12+
/// </summary>
913
public class EnumCSScriptSerializer : CSScriptSerializer
1014
{
15+
/// <summary>
16+
/// Creates a new instance of <see cref="EnumCSScriptSerializer"/> for the given enum type.
17+
/// </summary>
18+
/// <param name="type">The enum <see cref="System.Type"/> to serialize.</param>
1119
public EnumCSScriptSerializer(Type type)
1220
: base(type)
1321
{

src/CSharpScriptSerializer/FalseCSScriptSerializer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
namespace CSharpScriptSerialization
55
{
6+
/// <summary>
7+
/// Serializes <see langword="false"/> as the <c>false</c> literal expression.
8+
/// </summary>
69
public class FalseCSScriptSerializer : CSScriptSerializer
710
{
11+
/// <summary>
12+
/// The singleton instance of <see cref="FalseCSScriptSerializer"/>.
13+
/// </summary>
814
public static readonly FalseCSScriptSerializer Instance = new FalseCSScriptSerializer();
915

1016
private FalseCSScriptSerializer()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
namespace CSharpScriptSerialization
22
{
3+
/// <summary>
4+
/// Allows a type to supply its own <see cref="ICSScriptSerializer"/> instead of relying on
5+
/// the default serializer discovery logic in <see cref="CSScriptSerializer"/>.
6+
/// </summary>
37
public interface ICSScriptSerializable
48
{
9+
/// <summary>
10+
/// Returns the <see cref="ICSScriptSerializer"/> to use when serializing this object.
11+
/// </summary>
512
ICSScriptSerializer GetSerializer();
613
}
714
}

0 commit comments

Comments
 (0)