Dynamically referencing dll C#

Asked

Viewed 202 times

0

During the development of an application that will connect to a database of different versions, a question arose whether it is possible to load the dll based on the version selected by the user.

Example: We will have two versions of Oracle 11g e 12c where each version has its dll, the problem is that I am not being able to reference both in the project without generating build error.

Severity    Code    Description Project File    Line    Suppression State
Error   CS0433  The type 'OracleCommand' exists in both 'Oracle.DataAccess, Version=2.112.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342' and 'Oracle.DataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342'   DataSetMaker    C:\Users\Administrator\Desktop\VSB\Desenvolvimento\DataSetMaker\Controller\Connection.cs    60  Active

Is there any way to reference them dynamically based on radio button where the user would select which version to use?

1 answer

1


a question has arisen whether it is possible to load the dll based on the version user-selected

Yes it is possible, however if you load a dll by reflection, everything you do, will have to be done by reflection.

Example:

var assembly = Assembly.LoadFrom(@"C:\Temp\Oracle.ManagedDataAccess.dll");
var oracleConnectionType =  assembly.GetType("Oracle.ManagedDataAccess.Client.OracleConnection");
var connectionInstance = assembly.CreateInstance(oracleConnectionType.FullName);

and so it goes... this ends up making your code very complex and difficult to maintain.

Is there any way to reference them dynamically based on radio button where the user would select which version to use?

Well, it is possible to load both dll at the same time and create specific methods for each of them.

The first step is to reference them in your project, but beware, they cannot have the same name.

The second step is to define an alias for each of them:

Deinifir alias para sua dll

In one of them define the alias for OnzeG and to the other DozeC.

The third step is to reference these aliases in your code, above the usings:

extern alias DozenoveI;
extern alias OnzeG;

After that, create your methods referencing all types with their specific aliases, putting them in front of the method as follows

alias::Type

Example:

public DataTable ExecutarCom11g(string connectionString, string query)
{
    using (OnzeG::Oracle.ManagedDataAccess.Client.OracleConnection conn = new OnzeG::Oracle.ManagedDataAccess.Client.OracleConnection(connectionString))
    {
        conn.Open();

        OnzeG::Oracle.ManagedDataAccess.Client.OracleCommand oracleCommand = conn.CreateCommand();
        oracleCommand.CommandText = query;

        DataTable dataTable = new DataTable();
        OnzeG::Oracle.ManagedDataAccess.Client.OracleDataReader reader = oracleCommand.ExecuteReader();
        dataTable.Load(reader);

        return dataTable;
    }
}

public DataTable ExecutarCom12c(string connectionString, string query)
{
    using (DozeC::Oracle.ManagedDataAccess.Client.OracleConnection conn = new DozeC::Oracle.ManagedDataAccess.Client.OracleConnection(connectionString))
    {
        conn.Open();

        DozeC::Oracle.ManagedDataAccess.Client.OracleCommand oracleCommand = conn.CreateCommand();
        oracleCommand.CommandText = query;

        DataTable dataTable = new DataTable();
        DozeC::Oracle.ManagedDataAccess.Client.OracleDataReader reader = oracleCommand.ExecuteReader();
        dataTable.Load(reader);

        return dataTable;
    }
}

After that it is only encapsulate the methods in a class and call them when it is convenient:

string connectionString = "Data Source=banco;User ID=usuario;Password=senha;";
string query = "select 1 from dual";

BancoDeDados conectorBancoDeDados = new BancoDeDados();
var resultado11g = conectorBancoDeDados.ExecutarCom11g(connectionString, query);
var resultado12i = conectorBancoDeDados.ExecutarCom12c(connectionString, query);
  • Fantastic, I did not know it was possible to define alias for references, thank you very much worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.