473,320 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
Oct 8 '08 #1
7 1814

"uncle" wrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Oct 8 '08 #2
On Oct 8, 2:46*am, Morten Wennevik [C# MVP]
<MortenWenne...@hotmail.comwrote:
"uncle" wrote:
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.

--
Happy Coding!
Morten Wennevik [C# MVP]
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
Oct 8 '08 #3
On Wed, 08 Oct 2008 06:17:56 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
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?
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 with a
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
Oct 8 '08 #4
On Oct 8, 1:28*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 08 Oct 2008 06:17:56 -0700, puzzlecracker <ironsel2...@gmail.com>*
wrote:
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?

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.
Oct 8 '08 #5
On Wed, 08 Oct 2008 15:38:56 -0700, uncle <ak****@gmail.comwrote:
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.
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
Oct 8 '08 #6
On Oct 8, 7:32 pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 08 Oct 2008 15:38:56 -0700, uncle <akt...@gmail.comwrote:
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.

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
Oct 9 '08 #7
On Wed, 08 Oct 2008 20:37:26 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
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.
To be honest, I don't know all the specific rules .NET uses to look for
DLLs. I actually would think that it _would_ use the user's path, but I
admit I have no really compelling reason to think that other than the
simple issue of backward compatible behavior (in this case, mainly for the
purpose of having a more-easily understood API).
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?
Assemblies are loaded into an AppDomain. The AppDomain already knows what
assemblies have been loaded into it, so the GetAssemblies() method is
trivial: it just returns the list of those assemblies.

Pete
Oct 9 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Joshua Beall | last post by:
Hi All, I have been trying to dynamically call a static member function, as follows: $className = 'MyClass'; $methodName = 'MyMethod' $result = $className::$methodName(); However, I get a...
1
by: Luis Pinho | last post by:
Hi There, I've got a server that is waiting for requests, these request correspond to calls to objects that are specified in assemblies stored in the GAC. To do this, I use reflection to call...
1
by: Ajay Pal Singh | last post by:
Hi, I am making an windows service similar to windows task schedular. The service would invoke the methods of some assemblies at some predefined schedules. (windows service read the...
3
by: Manfred Braun | last post by:
Hi All, I am able to invoke some methods from an instance of a loaded assembly, but I am not able to invoke the loaded assemblie's "Main" method. I try it this way: Assembly assembly =...
7
by: Gaetan | last post by:
I would like to extend the capabilities of my application by calling a user method residing in a client provided assembly without having to recompile my application. Things would work like this:...
0
by: Erick Shuai | last post by:
In my project, I need to use custom attribute on all assemblies. This attribute will implement some non-functional code and I want to use it on assembly scope(AttributeTargets is Assembly). I...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
0
by: Thomas | last post by:
Hi all, I recently migrated all projects which are in .net 1.1 to 2.0 and everything seems to work correctly except one assembly reference problem. The project in my team is organized we have Web...
5
by: SunnyDrake | last post by:
HI! I wrting some program part of it is XML config parser which contains some commands(for flexibility of engenie). how do i more simple(if it possible not via System.Reflection or...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.