Skip to content

Minor change to allow Native AOT compilation#113

Merged
pavkam merged 1 commit into
pavkam:mainfrom
jordan-hemming:native-aot
Sep 26, 2025
Merged

Minor change to allow Native AOT compilation#113
pavkam merged 1 commit into
pavkam:mainfrom
jordan-hemming:native-aot

Conversation

@jordan-hemming

@jordan-hemming jordan-hemming commented Sep 26, 2025

Copy link
Copy Markdown
Contributor

I'd really like to be able to take advantage of the Native AOT compilation feature in .NET 8 and up when using Sharpie. Apparently the one blocker is a single use of Assembly.GetCallingAssembly() in NativeLibraryWrapper.

GetCallingAssembly() is kinda weird. It returns the assembly of the method that calls the method that then calls it. I don't know what dark magic it uses to achieve this, but it isn't supported when using Native AOT. However, the way it's being used in this project (inside a private method which is only called from within the same assembly) it always* returns the same result, which is the Sharpie assembly itself. So this PR replaces that call with GetExecutingAssembly() which returns the same result but doesn't have the same restrictions when used with Native AOT compilation.

Native AOT seems to work fine after this change.

*Apparently there are edge cases where GetCallingAssembly() will return a different result depending on how the compiler does inlining, but this doesn't seem to happen and I doubt it's the expected behavior in this case.

…mbly() to allow Native AOT in .NET 8 and above.

@pavkam pavkam left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. You're right, the choice of the method to get the assembly is not important. I just used the first that came to mind. 🫡

@pavkam
pavkam merged commit b6a4ffb into pavkam:main Sep 26, 2025
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants