monkey0506 / native-generic-delegates

Generic C# delegates for P/Invoke.
Other
8 stars 0 forks source link

`MarshalInfo` should use `IFieldInitializerOperation` instead of manual parsing #16

Closed monkey0506 closed 9 months ago

monkey0506 commented 1 year ago

Describe the issue

The methods in the MarshalInfo class in the incremental-generator branch are responsible for parsing user-supplied MarshalAsAttributes. It is supported for this attribute to be supplied by a reference to a readonly field, but previously this was not properly implemented. The correct Roslyn APIs necessary were not understood. See GetOperation returning null for VariableDeclaratorSyntax, VariableDeclarationSyntax, and FieldDeclarationSyntax - Stack Overflow for more information.

Proposed solution

The above referenced Stack Overflow question has an answer posted which includes details about the solution to this issue. The IFieldInitializerOperation can be obtained by discovering the EqualsValueClauseSyntax child node of the FieldDeclarationSyntax for inline field initializers. The manual parsing of the MarshalAsAttribute(s) provided by the referenced readonly field should be abandoned in favor of the much more powerful IOperation APIs.

Additional considerations

This issue depends on #15 as it refers to the same areas of the code.

Also, the Stack Overflow answer provides details about parsing values assigned to the readonly field inside of a constructor. This is currently not supported at all, and will be added as a new issue which will depend on this one.

monkey0506 commented 9 months ago

Fixed by a6d0462 (interceptors branch).