given assembly name, classname, static methodname, how to invoke?

U

uncle

config file that contains lines like...

assemblyname|classname|methodname

I want to invoke that static method. I found the following examples
which make sense...

Type theClass = assembly.GetType("Namespace.Classname", false, true);
bool result = (bool)theClass.InvokeMember("Initialize",
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static,
null, null, new object[]{ });

But how to I get the assembly instance given an assembly name (like
wow.dll) and not the full assembly path!?

-- Thanks, Andrew
 
M

Morten Wennevik [C# MVP]

uncle said:
config file that contains lines like...

assemblyname|classname|methodname

I want to invoke that static method. I found the following examples
which make sense...

Type theClass = assembly.GetType("Namespace.Classname", false, true);
bool result = (bool)theClass.InvokeMember("Initialize",
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static,
null, null, new object[]{ });

But how to I get the assembly instance given an assembly name (like
wow.dll) and not the full assembly path!?

-- Thanks, Andrew

Hi Andrew,

You can use Assembly.LoadFrom("wow.dll") if you only have its filename. If
you have its full name use Assembly.Load(AssemblyName). There is also
Assembly.LoadWithPartialName("wow"), but both LoadFrom and especially
LoadFromWithPartialName may load a wrong assembly if there are version
differences, or simply a completely wrong assemply with the same name.
 
P

puzzlecracker

uncle said:
config file that contains lines like...

I want to invoke that static method.  I found the following examples
which make sense...
Type theClass = assembly.GetType("Namespace.Classname", false, true);
bool result = (bool)theClass.InvokeMember("Initialize",
BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static,
null, null, new object[]{ });
But how to I get the assembly instance given an assembly name (like
wow.dll) and not the full assembly path!?
-- Thanks, Andrew

Hi Andrew,

You can use Assembly.LoadFrom("wow.dll") if you only have its filename.  If
you have its full name use Assembly.Load(AssemblyName).  There is also
Assembly.LoadWithPartialName("wow"), but both LoadFrom and especially
LoadFromWithPartialName may load a wrong assembly if there are version
differences, or simply a completely wrong assemply with the same name.

A few follow-up questions:

1. LoadForm and Load -- what is the difference between Assembly name
and dll filename? Does it
2.I presume that it will load the latest version, how to tell it to
load a different version and where/how to specify assembly version in
assembly dll?


Thanks
 
U

uncle

If I understand the documentation correctly, then you have to use  
Assembly.Load() if you want to ensure version information as part of the  
loading process.  LoadFile() and LoadFrom() will load any assembly witha  
matching name (with each of those methods have their own particular  
behavior).  You'll have to check the version yourself if you use those  
methods.

MSDN has a very detailed description of the behaviors of these various  
methods, probably much better than anything you're going to get in the  
newsgroup.  You should start there, and post specific questions if you  
find yourself confused about what the docs say.

Pete

So in my situation, the assembly is already loaded. This is not a
plugin scenario with dynamic loading and what not. I just need to
call a static method on a class given the class name and method name.
 
P

puzzlecracker

Ah. Well, you could use the AppDomain.GetAssemblies and then search for
the relevant assembly by name (just do a string comparison if you like,
probably using something like String.Contains()). But that has the same
basic issue of the other approaches, which is that given just a partial
name you might wind up getting an assembly other than the one you want.

As long as you're okay with that, maybe that's the way to go. But you
should definitely take careful consideration of how your code could be
used and whether you can actually guarantee that you will always get the
desired assembly in this way. If you can't make that guarantee, then
you're dealing with what looks to me to be a fairly significant security
hole.

Pete

If I understand correctly, CSharp doesn't use path to find DLLs/
assemblies, as it's done in c++, but CRL looks at the current
directory or paths specified in a configuration file. I use former,
and could be utterly wrong about the latter (experts correct me here).
So, how will AppDomain.GetAssemblies find all the assemblies
currently loaded (reachable) but the running code?

Examples will be appreciated!

Thanks
 

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