@@ -119,25 +119,14 @@ public static DllImportStub Create(
119119 INamedTypeSymbol currType = method . ContainingType ;
120120 while ( ! ( currType is null ) )
121121 {
122- var visibility = currType . DeclaredAccessibility switch
123- {
124- Accessibility . Public => SyntaxKind . PublicKeyword ,
125- Accessibility . Private => SyntaxKind . PrivateKeyword ,
126- Accessibility . Protected => SyntaxKind . ProtectedKeyword ,
127- Accessibility . Internal => SyntaxKind . InternalKeyword ,
128- _ => throw new NotSupportedException ( ) , // [TODO] Proper error message
129- } ;
130-
131- TypeDeclarationSyntax typeDecl = currType . TypeKind switch
132- {
133- TypeKind . Class => ClassDeclaration ( currType . Name ) ,
134- TypeKind . Struct => StructDeclaration ( currType . Name ) ,
135- _ => throw new NotSupportedException ( ) , // [TODO] Proper error message
136- } ;
137-
138- typeDecl = typeDecl . AddModifiers (
139- Token ( visibility ) ,
140- Token ( SyntaxKind . PartialKeyword ) ) ;
122+ // Use the declaring syntax as a basis for this type declaration.
123+ // Since we're generating source for the method, we know that the current type
124+ // has to be declared in source.
125+ TypeDeclarationSyntax typeDecl = ( TypeDeclarationSyntax ) currType . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) ;
126+ // Remove current members and attributes so we don't double declare them.
127+ typeDecl = typeDecl . WithMembers ( List < MemberDeclarationSyntax > ( ) )
128+ . WithAttributeLists ( List < AttributeListSyntax > ( ) ) ;
129+
141130 containingTypes . Add ( typeDecl ) ;
142131
143132 currType = currType . ContainingType ;
0 commit comments