Good day!

There is a class:

using Microsoft.CodeAnalysis.CSharp.Scripting; using Microsoft.CodeAnalysis.Scripting; using System; using System.Reflection; namespace namespace1 { public class AClass { public void Run(string Code) { // Π€ΠΎΡ€ΠΌΠΈΡ€ΡƒΠ΅ΠΌ зависимости Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); ScriptOptions options = ScriptOptions.Default.AddReferences(assemblies); // ЗапускаСм скрипт var result = CSharpScript.RunAsync(Code, options); } } } 

It's simple: I get the builds of the current domain -> I copy them to the script settings -> I execute the script. The code I'm trying to pass to the Run procedure:

 string code = @"using Excel = NetOffice.ExcelApi; " + @"Excel.Application ExcelApp = new Excel.Application(); "; 

As you noticed, in the transmitted code I use using. Which should refer to acc. dll (NetOffice.ExcelApi). In the project, this link is present: Properties

enter image description here

To the point: when I get AppDomain.CurrentDomain.GetAssemblies (), then there is no NetOffice assembly, respectively. As I understand it, this is due to the fact that the compiler is smart and does not load those assemblies that are not explicitly used in the code. Everything is logical (this was confirmed when I added a line to the code of the Run method:

 NetOffice.ExcelApi.Application SomeVar = new NetOffice.ExcelApi.Application(); 

After, I decided that I would forcibly load the dll into the domain. But I found out that when building a project, this dll does not fall into the assembly files, although the "Copy locally" property stands on them.

In this regard, several

1) Is it possible to force the collector to copy unused unused references (dll) to the release folder?

2) Is it possible to get a list of all Assemblies (maybe just a list of names) in the code, including those that are not used explicitly? (non-ASP.NET application)

PS In C # the perfect newbie. 100% somewhere while I misunderstand some things. I ask to understand and forgive :) I would be grateful if you would advise good sources for pumping a skill in C #.

    1 answer 1

    1. If you use an assembly in code (not through Assembly.Load , but through an added link), then it should be copied into the output directory.

    2. There is not quite a reliable path.

       Assembly.GetEntryAssembly().GetReferencedAssemblies() 

      It is unreliable because the compiler has the full right to build, which is not used in the project, and not to include in the list referenced assemblies.

      The correct way is to specify the list of necessary assemblies explicitly , and not to try to get it somewhere in a roundabout way.


    Yes, besides, the path you have chosen (to dynamically generate a script and execute it through the scripting API) seems a bit strange. I suspect that the same result can be achieved and easier by directly calling the desired API.

    • 1) I am building a project in visual studio. Those. with this method, in any case, all the references I specified in the references (via nuget) should go to the output directory, right? - Serovkir
    • @Serovkir: Do you have dependencies of the project? And then maybe you added, and then deleted. - VladD
    • @Serovkir: Does your screenshot in question display the current state of affairs? - VladD
    • 1) I am building a project in visual studio. Those. with this method, 2) this option is just not suitable, since the compiler does not add unused assemblies there. /// By architecture: I need to do something, like a dynamic compiler. I pass the code there, it is executed, while using those libraries that I specified ref. I understand your doubts, but there are reasons for this. - Serovkir
    • Reflects, at the moment it is. - Serovkir