1

How can I programmatically add an assembly (DLL) as reference in a C# project? I need to add a reference at run time.

Consider if my project uses another class library (DLL) then I need to add that DLL as a reference in my project. How can I do that at run time?

4
  • Do you mean "Add a reference to a .csproj file" or "dynamically load a dll at runtime" ? Commented Mar 10, 2009 at 9:22
  • It's not clear exactly what you mean, but if you look at the project file it's a reasonably simple format - it wouldn't be hard to load it up as an XML file, modify it programmatically, and then write it out again. If that's not the kind of thing you're after, could you clarify your question? Commented Mar 10, 2009 at 9:22
  • Can you tell us why you programmatically want to add an assembly in a C# project? Commented Mar 10, 2009 at 10:19
  • my application loads various dll, and processes them. so i need to add the dlls in my references. Commented Mar 10, 2009 at 12:35

2 Answers 2

2

I guess you want to load an type at runtime? You can use Assembly.Load and reflection to do this.

Sign up to request clarification or add additional context in comments.

Comments

1

Probably you want to get the type information from a class in another .dll, and then create an object instance of that class.

var lateBindingType = Type.GetType("Name.Of.The.Class,NameOfDll");
var instance = Activator.CreateInstance(lateBindingType);

In the call to Type.GetType you use the Fully Qualified Type Name.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.