Using assemlby without depending on it

  • Thread starter Thread starter Bill
  • Start date Start date
B

Bill

I am trying to use a 3rd party assembly that may or may not be available on
a given machine. Is there a way I can use the classes within that assembly
without it throwing an assembly load exception when the assembly file is not
available? I want to safely bypass the feature if the assembly is not
there.

I was going to try using the Assembly a = Assembly.Load() but there are a
lot or properties and method that I need to use. It would take forever if I
manually called GetMethd(), GetProperty(), and Invoke for every value I
needed.
 
right out of help:

You can check if the file is there, if so, load assembly, then ether scan
for types using reflection or try to create an instance of a predefined type
and catch an error if there is one.

Realistically speaking you should create an interface and build it into your
application and then match types of objects from assembly to an interface

cheers

using System;
using System.Reflection;

class Test {

public static void Main() {
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.AssemblyLoad += new
AssemblyLoadEventHandler(MyAssemblyLoadEventHandler);

PrintLoadedAssemblies(currentDomain);
// Lists mscorlib and this assembly

// You must supply a valid fully qualified assembly name here.
currentDomain.CreateInstance("System.Windows.Forms, Version, Culture,
PublicKeyToken", "System.Windows.Forms.TextBox");
// Loads System, System.Drawing, System.Windows.Forms

PrintLoadedAssemblies(currentDomain);
// Lists all five assemblies
}

static void PrintLoadedAssemblies(AppDomain domain) {
Console.WriteLine("LOADED ASSEMBLIES:");
foreach (Assembly a in domain.GetAssemblies()) {
Console.WriteLine(a.FullName);
}
Console.WriteLine();
}

static void MyAssemblyLoadEventHandler(object sender,
AssemblyLoadEventArgs args) {
Console.WriteLine("ASSEMBLY LOADED: " + args.LoadedAssembly.FullName);
Console.WriteLine();
}
}
 
I was going to try using the Assembly a = Assembly.Load() but there
are a lot or properties and method that I need to use. It would take
forever if I manually called GetMethd(), GetProperty(), and Invoke for
every value I needed.

I don't think there's a way to simply call the functions directly, but
there are a couple of ways you could solve this... you should be able to
write a couple of functions to make it easier. For example, write one
that starts like this:

private object ExecAssemblyMethod(
string methodName,
params object[] methodParams)
{
...
// Find the function, Invoke it with methodParams
// and return the result of Invoke
}

You could also write a wrapper class that matches the functions you want
to call from the lib so that you can use the Intellisense in the editor.
You would need to wrap every function that you're going to use, but once
it's done you wouldn't have to mess with it. For example:

private class LibWrapper
{
private bool bLibInitialized = false;
private void Init3rdPartyLib() { ... Assembly.Load(...); ... }
private object ExecAssemblyMethod(...) { ... } // from above

private int ThirdPartyFunc(int a, string b)
{
if (!bLibInitialized) throw new Exception(...);
return (int)ExecAssemblyMethod("ThirdPartyFunc", a, b);
}
}

Having said this, what are you going to do if the lib isn't available on
a particular system? The whole idea doesn't really make sense. But if
you use a wrapper lib you could return default values or throw exceptions
or something like that.

-mdb
 
You would probably have to deal with Assembly load ether way when you
consider security. You could abstract it to a function, however, i suspect it
would not change the overall requirement to load the assembly (i.e. load
security certificates for it as well).

mdb said:
I was going to try using the Assembly a = Assembly.Load() but there
are a lot or properties and method that I need to use. It would take
forever if I manually called GetMethd(), GetProperty(), and Invoke for
every value I needed.

I don't think there's a way to simply call the functions directly, but
there are a couple of ways you could solve this... you should be able to
write a couple of functions to make it easier. For example, write one
that starts like this:

private object ExecAssemblyMethod(
string methodName,
params object[] methodParams)
{
...
// Find the function, Invoke it with methodParams
// and return the result of Invoke
}

You could also write a wrapper class that matches the functions you want
to call from the lib so that you can use the Intellisense in the editor.
You would need to wrap every function that you're going to use, but once
it's done you wouldn't have to mess with it. For example:

private class LibWrapper
{
private bool bLibInitialized = false;
private void Init3rdPartyLib() { ... Assembly.Load(...); ... }
private object ExecAssemblyMethod(...) { ... } // from above

private int ThirdPartyFunc(int a, string b)
{
if (!bLibInitialized) throw new Exception(...);
return (int)ExecAssemblyMethod("ThirdPartyFunc", a, b);
}
}

Having said this, what are you going to do if the lib isn't available on
a particular system? The whole idea doesn't really make sense. But if
you use a wrapper lib you could return default values or throw exceptions
or something like that.

-mdb
 
what are you going to do if the lib isn't available on a particular system?
If the assembly is not there the feature will be disabled. Its only a small
part of the avaialable features.

I guess i have no choice but to write the wrapper class. The only thing is
there are like 10 collection classes that are going to be a pain to convert.



mdb said:
I was going to try using the Assembly a = Assembly.Load() but there
are a lot or properties and method that I need to use. It would take
forever if I manually called GetMethd(), GetProperty(), and Invoke for
every value I needed.

I don't think there's a way to simply call the functions directly, but
there are a couple of ways you could solve this... you should be able to
write a couple of functions to make it easier. For example, write one
that starts like this:

private object ExecAssemblyMethod(
string methodName,
params object[] methodParams)
{
...
// Find the function, Invoke it with methodParams
// and return the result of Invoke
}

You could also write a wrapper class that matches the functions you want
to call from the lib so that you can use the Intellisense in the editor.
You would need to wrap every function that you're going to use, but once
it's done you wouldn't have to mess with it. For example:

private class LibWrapper
{
private bool bLibInitialized = false;
private void Init3rdPartyLib() { ... Assembly.Load(...); ... }
private object ExecAssemblyMethod(...) { ... } // from above

private int ThirdPartyFunc(int a, string b)
{
if (!bLibInitialized) throw new Exception(...);
return (int)ExecAssemblyMethod("ThirdPartyFunc", a, b);
}
}

Having said this, what are you going to do if the lib isn't available on
a particular system? The whole idea doesn't really make sense. But if
you use a wrapper lib you could return default values or throw exceptions
or something like that.

-mdb
 
Back
Top