Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# compile functions based upon version of dll's used.

I got lots of smaller projects, who each depend on various external dll's. Vendors bug-fix those dll's resulting in a range of my programs to be updated as wel,or not, as fixes not always work for other programs. And while i do know of some ways to tackle this. ea keep dll's in a lib folder or use Nuget. But still then maintaining of generic wrapper code can be a hassle.

So I wonder are there some way in visual studio so we could write wrappers like this:

[Rongten.dll = 1.5+]   // 1.5 and above versions
Public string[] ExtendedInfo()
{ return this.api.ExtendedInfo }

[Rongten.dll = 1.2]  //only a specific version
Public string[] ExtendedInfo()
{String[] t = new string[1];
 t = "not available";
  return t;
}
 [Rongten.dll = 1.1-]  //below a specific version
Public string  ExtendedInfo()
{ 
 t = "not available";
  return t;
}

So some square bracket method [ to allow a dll version] or something like that, maybe by an addon.

like image 295
Peter Avatar asked Apr 06 '18 08:04

Peter


2 Answers

No, basically. You would have to query the library version discovered at runtime (either via an API, or by inspecting the file metadata), and do something like a switch inside the method. Usually you would only need to do this once; you could do this via a static field and "lazy" load, or you could put the version query into static initializer.

like image 168
Marc Gravell Avatar answered Oct 10 '22 05:10

Marc Gravell


It is not possible to do this magically, but there is an easy way out using preprocessor directives. The only thing you have to do is configure the right version in your project settings:

#if RONGTENVERSION_1_5_PLUS
public string[] ExtendedInfo()
{
    return this.api.ExtendedInfo;
}
#elif RONGTENVERSION_1_2
public string[] ExtendedInfo()
{
    return new string[] { "not available" };
}
#elif RONGTENVERSION_1_1_MIN
public string ExtendedInfo()
{ 
    return "not available";
}
#endif

The file containing this can be linked in your project, so it can benefit from the compiler directives.

like image 20
Patrick Hofman Avatar answered Oct 10 '22 04:10

Patrick Hofman