I have a .NET 6.0 project which uses Royslin to compile a C# like language. That scripting language inherits and uses a few classes that my project has. Currently I use the following code to add a reference to the current assembly:
var options = ScriptOptions.Default
.WithReferences(Assembly.GetExecutingAssembly())
The previous code works fine until I publish my project which is a simple CLI program. When I publish the project, it becomes a single executable so GetExecutingAssembly().Location
will be empty. And due to the requirements, I have to keep it as a single file executable. So now I need a way of converting GetExecutingAssembly()
to byte[]
such that I can use the method MetadataReference.CreateFromImage()
I have tried a few solutions so far:
1- Tried Using BinaryFormatter
and serialize the assembly or even single class assembly and I got an error that the type is not serializable even though it has the serializable attribute
2- Tried using Hash
from System.Security.Policy
, but the Hash class doesn't even exist, and I tried searching for it as COM reference and Nuget package but couldn't find anything
3- I can maybe add the assembly to a resource file however I have multiple projects which means I am going to need to change the resource file when I switch to debug or release mode as well as I will have to add a version of the dll for each project and switch this version according to the current project that is running. If there is a work around this in visual studio build options that would be great.
4- I maybe compile the classes that the scripting language need at run time and generate dll files but I don't see this as efficient
Any suggestions on how to convert the assembly to binary array of even a metadata reference?