Ali Adlavaran
Ali Adlavaran

Reputation: 3735

What does BuildManager.AddReferencedAssembly exactly do?

the MSDN says it "Adds an assembly to the application's set of referenced assemblies.". but how? where effects happens exactly?is there any ienumerable/list of referenced assembly to access and add manually to them?

actually if we call this method after app start it throws

"This method can only be called during the application's pre-start initialization phase. Use PreApplicationStartMethodAttribute to declare a method that will be invoked in that phase."

ok. now i don't want to use it. i want to know what does do exactly and then i simulate in my custom function. thanks

Upvotes: 3

Views: 1033

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149598

It's a good thing we have the source code:

public static void AddReferencedAssembly(Assembly assembly) 
{
    if (assembly == null) 
    {
        throw new ArgumentNullException("assembly");
    }

    ThrowIfPreAppStartNotRunning();

    s_dynamicallyAddedReferencedAssembly.Add(assembly);
}

s_dynamicallyAddedReferencedAssembly.Add is a List<Assembly> of dynamically added assemblies which will be taken into account during compilation.

There is another internal method called GetReferencedAssembiles which gets all the assemblies for the project, which iterates and adds all the dynamically added assemblies to the list of referenced assemblies:

internal static ICollection GetReferencedAssemblies(CompilationSection compConfig,
                                                    int removeIndex) 
{
    // shorted for brevity

    foreach (Assembly assembly in s_dynamicallyAddedReferencedAssembly)
    {
        referencedAssemblies.Add(assembly);
    }
}

Upvotes: 3

Related Questions