Use dll dynamically in code.

A

Arne Vajhøj

How can I use dll dynamically in my code?

I assume the DLL is a .NET assembly.

The simplest variant must be:

Isomeinterface o =
(Isomeinterface)Assembly.Load(dllname).CreateInstance(classname);

Arne
 
A

Arne Vajhøj

Assuming that by "dynamically" you mean that you don't want to link your
own assembly to the DLL assembly you want to use (i.e. use a DLL not
known at compile time), then the basic approach involves reflection.

There is a new plug-in framework with .NET 4.0 that is supposed to make
this easier. If you can restrict your goal to something where that is
usable, then that would probably be a better approach.

Do you mean the new MAF (Managed Addin Framework) in 3.5?

That is a very powerful framework, but most likely completely
overkill, because relative complex.
Otherwise, you're
going to have to load the assembly explicitly, inspect it for relevant
types, and execute the code within all via reflection.

Utilizing that the assembly contains a class implementing
a known interface to avoid reflection for calling the loaded
code is very common.

Arne
 
M

Mr. X.

I meant :
I.e I want to load "System.Data.SqlClient dll" on runtime, and use some of
it's classes (such as SqlConnectionStringBuilder)

I will try the samples you gave.

Thanks :)
 
M

Mr. X.

What I did for the sample you gave,
for MySql, I got the message :
=========================
Could not load file or assembly 'MySql.Data.MySqlClient' or one of its
dependencies. The system cannot find the file specified.
 
A

Arne Vajhøj

I meant :
I.e I want to load "System.Data.SqlClient dll" on runtime, and use some
of it's classes (such as SqlConnectionStringBuilder)

Db provider factories exists to solve this problem using
builtin .NET code.

If you have to support .NET 1.x, then you can do it manually like
in this example:

using System;
using System.Reflection;
using System.Data;

namespace E
{
public class MainClass
{
public static void Test(string dllfnm, string conclznam, string
constr)
{
IDbConnection con =
(IDbConnection)Assembly.LoadFrom(dllfnm).CreateInstance(conclznam);
con.ConnectionString = constr;
con.Open();
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM T1";
IDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLine(f1 + " " + f2);
}
con.Close();
}
public static void Main(string[] args)
{
Test(@"C:\InetPub\wwwroot\bin\MySql.Data.dll",
"MySql.Data.MySqlClient.MySqlConnection",
"Database=Test;Data Source=localhost;User Id=;Password=");
}
}
}

Arne
 
A

Arne Vajhøj

What I did for the sample you gave,
for MySql, I got the message :
=========================
Could not load file or assembly 'MySql.Data.MySqlClient' or one of its
dependencies. The system cannot find the file specified.

Code?

What version of MySQL provider?

Where is your code and where is the MySQL provider?

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top