@@ -67,19 +67,54 @@ public static RecipientInfo GetInfo(INamedTypeSymbol typeSymbol, ImmutableArray<
67
67
/// Gets the head <see cref="CompilationUnitSyntax"/> instance.
68
68
/// </summary>
69
69
/// <returns>The head <see cref="CompilationUnitSyntax"/> instance with the type attributes.</returns>
70
- public static CompilationUnitSyntax GetSyntax ( )
70
+ public static CompilationUnitSyntax GetSyntax ( bool isDynamicallyAccessedMembersAttributeAvailable )
71
71
{
72
+ int numberOfAttributes = 5 + ( isDynamicallyAccessedMembersAttributeAvailable ? 1 : 0 ) ;
73
+ ImmutableArray < AttributeListSyntax > . Builder attributes = ImmutableArray . CreateBuilder < AttributeListSyntax > ( numberOfAttributes ) ;
74
+
75
+ // Prepare the base attributes with are always present:
76
+ //
77
+ // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
78
+ // [global::System.Diagnostics.DebuggerNonUserCode]
79
+ // [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
80
+ // [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
81
+ // [global::System.Obsolete("This type is not intended to be used directly by user code")]
82
+ attributes . Add (
83
+ AttributeList ( SingletonSeparatedList (
84
+ Attribute ( IdentifierName ( $ "global::System.CodeDom.Compiler.GeneratedCode") ) . AddArgumentListArguments (
85
+ AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( IMessengerRegisterAllGenerator ) . FullName ) ) ) ,
86
+ AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( IMessengerRegisterAllGenerator ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ) ) ) ) ) ;
87
+ attributes . Add ( AttributeList ( SingletonSeparatedList ( Attribute ( IdentifierName ( "global::System.Diagnostics.DebuggerNonUserCode" ) ) ) ) ) ;
88
+ attributes . Add ( AttributeList ( SingletonSeparatedList ( Attribute ( IdentifierName ( "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" ) ) ) ) ) ;
89
+ attributes . Add (
90
+ AttributeList ( SingletonSeparatedList (
91
+ Attribute ( IdentifierName ( "global::System.ComponentModel.EditorBrowsable" ) ) . AddArgumentListArguments (
92
+ AttributeArgument ( ParseExpression ( "global::System.ComponentModel.EditorBrowsableState.Never" ) ) ) ) ) ) ;
93
+ attributes . Add (
94
+ AttributeList ( SingletonSeparatedList (
95
+ Attribute ( IdentifierName ( "global::System.Obsolete" ) ) . AddArgumentListArguments (
96
+ AttributeArgument ( LiteralExpression (
97
+ SyntaxKind . StringLiteralExpression ,
98
+ Literal ( "This type is not intended to be used directly by user code" ) ) ) ) ) ) ) ;
99
+
100
+ if ( isDynamicallyAccessedMembersAttributeAvailable )
101
+ {
102
+ // Conditionally add the attribute to inform trimming, if the type is available:
103
+ //
104
+ // [global::System.CodeDom.Compiler.DynamicallyAccessedMembersAttribute(global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
105
+ attributes . Add (
106
+ AttributeList ( SingletonSeparatedList (
107
+ Attribute ( IdentifierName ( "global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute" ) ) . AddArgumentListArguments (
108
+ AttributeArgument ( ParseExpression ( "global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods" ) ) ) ) ) ) ;
109
+ }
110
+
72
111
// This code produces a compilation unit as follows:
73
112
//
74
113
// // <auto-generated/>
75
114
// #pragma warning disable
76
115
// namespace CommunityToolkit.Mvvm.Messaging.__Internals
77
116
// {
78
- // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")]
79
- // [global::System.Diagnostics.DebuggerNonUserCode]
80
- // [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
81
- // [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
82
- // [global::System.Obsolete("This type is not intended to be used directly by user code")]
117
+ // <ATTRIBUTES>
83
118
// internal static partial class __IMessengerExtensions
84
119
// {
85
120
// }
@@ -93,21 +128,7 @@ public static CompilationUnitSyntax GetSyntax()
93
128
Token ( SyntaxKind . InternalKeyword ) ,
94
129
Token ( SyntaxKind . StaticKeyword ) ,
95
130
Token ( SyntaxKind . PartialKeyword ) )
96
- . AddAttributeLists (
97
- AttributeList ( SingletonSeparatedList (
98
- Attribute ( IdentifierName ( $ "global::System.CodeDom.Compiler.GeneratedCode") ) . AddArgumentListArguments (
99
- AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( IMessengerRegisterAllGenerator ) . FullName ) ) ) ,
100
- AttributeArgument ( LiteralExpression ( SyntaxKind . StringLiteralExpression , Literal ( typeof ( IMessengerRegisterAllGenerator ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ) ) ) ) ,
101
- AttributeList ( SingletonSeparatedList ( Attribute ( IdentifierName ( "global::System.Diagnostics.DebuggerNonUserCode" ) ) ) ) ,
102
- AttributeList ( SingletonSeparatedList ( Attribute ( IdentifierName ( "global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage" ) ) ) ) ,
103
- AttributeList ( SingletonSeparatedList (
104
- Attribute ( IdentifierName ( "global::System.ComponentModel.EditorBrowsable" ) ) . AddArgumentListArguments (
105
- AttributeArgument ( ParseExpression ( "global::System.ComponentModel.EditorBrowsableState.Never" ) ) ) ) ) ,
106
- AttributeList ( SingletonSeparatedList (
107
- Attribute ( IdentifierName ( "global::System.Obsolete" ) ) . AddArgumentListArguments (
108
- AttributeArgument ( LiteralExpression (
109
- SyntaxKind . StringLiteralExpression ,
110
- Literal ( "This type is not intended to be used directly by user code" ) ) ) ) ) ) ) ) )
131
+ . AddAttributeLists ( attributes . MoveToImmutable ( ) . ToArray ( ) ) ) )
111
132
. NormalizeWhitespace ( ) ;
112
133
}
113
134
0 commit comments