1717
1818namespace 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 )
0 commit comments