Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does BuildManager.AddReferencedAssembly exactly do?

Tags:

c#

asp.net-mvc

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

like image 373
Ali Adlavaran Avatar asked Feb 01 '26 18:02

Ali Adlavaran


1 Answers

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);
    }
}
like image 80
Yuval Itzchakov Avatar answered Feb 04 '26 09:02

Yuval Itzchakov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!