Skip to content

Commit 4bb5cb0

Browse files
authored
Use declaring syntax as a baseline for generated syntax for containing types. (#108)
1 parent e787027 commit 4bb5cb0

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

DllImportGenerator/DllImportGenerator/DllImportStub.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)