K
K
Kirill Serov2017-08-18 00:00:47
Programming
Kirill Serov, 2017-08-18 00:00:47

The necessary references are not included in the assembly. How to fix?

Good afternoon!
There is a class:

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 assemblies 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:
The code
string code = @"using Excel = NetOffice.ExcelApi; "
                   + @"Excel.Application ExcelApp = new Excel.Application(); ";

As you can see, in the passed code I use using. Which should refer to acc. dll (NetOffice.ExcelApi).
This link is in the project:
Properties
5b07e51a41244cd89cb60fb80a11348e.jpg

To the point:
when I get AppDomain.CurrentDomain.GetAssemblies(), then there is no NetOffice assembly there, 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 the line to the Run method code:
NetOffice.ExcelApi.Application SomeVar = new NetOffice.ExcelApi.Application();

After, I decided that I would force the dll to be loaded into the domain. But I found out that when building the project, this dll does not get into the assembly files, although they have the "Copy local" property on them.
In this regard, a few questions:
1) Is it possible to force the collector to copy explicitly unused links (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 Totally
new to C#. 100% somewhere I still misunderstand some things. I ask you to understand and forgive :)
I would be grateful if you could advise good sources for improving skills in C#.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mshak, 2017-10-05
@serovkir

You can dynamically load the required assembly.
To do this, you need to subscribe to the
AppDomain.CurrentDomain.AssemblyResolve event
and your code will have to do the following:
If the assembly is loaded, return the already loaded one,
If the assembly has not yet been loaded, then return Assembly.LoadFrom("path to the assembly file");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question