473,659 Members | 3,277 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I detect the presense of an optional assembly and call a method within it?

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:

1- Read a configuration file where the client provides the assembly name (DLL) and the
method name.

2- Detect the existence of the assembly file.

3- Invoke the client method.

I'm not sure how to handle item #3. I cannot add a reference to the client DLL in my
application since I do not know in advance what the assembly name will be nor the method
name. Can someone provide me a clue on how I should handle it?

Thanks.
Nov 17 '05 #1
7 2037
The client can configure his assembly and provide information about it in a
client.config file but this happens with .Net Remoting. If this is not what
you are looking for, please tell me what is your client.

Regards,

Jv

"Gaetan" wrote:
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:

1- Read a configuration file where the client provides the assembly name (DLL) and the
method name.

2- Detect the existence of the assembly file.

3- Invoke the client method.

I'm not sure how to handle item #3. I cannot add a reference to the client DLL in my
application since I do not know in advance what the assembly name will be nor the method
name. Can someone provide me a clue on how I should handle it?

Thanks.

Nov 17 '05 #2
You need more than just the assembly name, you also need the class name that
will be invoked against.

Basically you will need to use Reflection. The idea being that reflection
allows you to dynamically create instances at runtime and invoke method.
Take a look at reflection and see if this helps.

"Gaetan" <so*****@somewh ere.com> wrote in message
news:8u******** *************** *********@4ax.c om...
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:

1- Read a configuration file where the client provides the assembly name
(DLL) and the
method name.

2- Detect the existence of the assembly file.

3- Invoke the client method.

I'm not sure how to handle item #3. I cannot add a reference to the client
DLL in my
application since I do not know in advance what the assembly name will be
nor the method
name. Can someone provide me a clue on how I should handle it?

Thanks.

Nov 17 '05 #3
Here is how you can go about it.
The config file should have the following details for each method that
needs to be invoked - The assembly, the type and the method name.

First you need to create an instance of the object on which the method
call will be invoked. Use the static CreateInstance method of the
Activator class that accepts the assembly name and the type name as
arguments

object dynamicObj=Acti vator.CreateIns tance(assemblyN ame,
typeName).Unwra p();

If the assembly doesn't exist or an incorrect typName is specified,
then the above statement will throw a FileNotFound Exception or a
TypeLoad Exception respectively, which you can catch and process
accordingly. The typeName should be specified as namespaceName.t ypeName
where namespaceName is the namespace under which typeName is defined.
Once, you have a "live" object, you can use methods of the Type Class
and the MethodInfo class to invoke the specified method.

Type dynamicType=dyn amicObj.GetType ();
MethodInfo dynamicMethod=d ynamicType.GetM ethod(methodNam e);

I'm assuming that the method to be invoked is not overloaded. If that
is not the case, then you need to use an appropriate overload of the
Type.GetMethod method, specifying the input parameter types.

object returnValue=dyn amicMethod.Invo ke(dynamicObjec t, null);

Again, assuming that the method to be invoked does not accept any
parameters. You can find more detailed explanations and examples in the
msdn documentation for Type and MethoInfo classes.

Hope this helps.
Regards,
Sarin.

Nov 17 '05 #4
This useful information seems to be what I was looking for. Now reading more about
Reflection and the methods you indicated.

Thank you.

On 23 Oct 2005 23:02:57 -0700, "Spidey" <sa************ *@gmail.com> wrote:
Here is how you can go about it.
The config file should have the following details for each method that
needs to be invoked - The assembly, the type and the method name.

First you need to create an instance of the object on which the method
call will be invoked. Use the static CreateInstance method of the
Activator class that accepts the assembly name and the type name as
arguments

object dynamicObj=Acti vator.CreateIns tance(assemblyN ame,
typeName).Unwr ap();

If the assembly doesn't exist or an incorrect typName is specified,
then the above statement will throw a FileNotFound Exception or a
TypeLoad Exception respectively, which you can catch and process
accordingly. The typeName should be specified as namespaceName.t ypeName
where namespaceName is the namespace under which typeName is defined.
Once, you have a "live" object, you can use methods of the Type Class
and the MethodInfo class to invoke the specified method.

Type dynamicType=dyn amicObj.GetType ();
MethodInfo dynamicMethod=d ynamicType.GetM ethod(methodNam e);

I'm assuming that the method to be invoked is not overloaded. If that
is not the case, then you need to use an appropriate overload of the
Type.GetMeth od method, specifying the input parameter types.

object returnValue=dyn amicMethod.Invo ke(dynamicObjec t, null);

Again, assuming that the method to be invoked does not accept any
parameters. You can find more detailed explanations and examples in the
msdn documentation for Type and MethoInfo classes.

Hope this helps.
Regards,
Sarin.

Nov 17 '05 #5
MUS
Hello Gaetan!

Well this might not be the perfect Plugin approach (i.e. have a static
reference to client assembly) but just an idea, may be you feel worth
implementing. Its based on the use of config file and reflection.

<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>
<configSections >
<section name="ClientSec tion"
type="System.Co nfiguration.Dic tionarySectionH andler" />
</configSections>
<!-- Custom Section -->
<ClientSectio n>
<add key="BusinessAs sembly" value="<your_as sembly>" />
<add key="BusinessTy pe" value="<your_ty pe>" />
<add key="BusinessMe thod" value="<your_me thod>" />
</ClientSection>
<!-- / Custom Section -->
</configuration>
public Object DoAction()
{
try
{
Hashtable configData = (Hashtable)
ConfigurationSe ttings.GetConfi g("ClientSectio n");
if(configData== null) throw new ArgumentNullExc eption("Unable to read
configuration section");
// Get Assembly
String strAssembly =
Convert.ToStrin g(configData["BusinessAssemb ly"]);
Assembly assembly = Assembly.Load(s trAssembly);
if(assembly==nu ll) throw new Exception("Unab le to load assembly:
"+assemblyName) ;
// Get Type
String strType = Convert.ToStrin g(configData["BusinessTy pe"]);
Type workingType = assembly.GetTyp e(strType);
if(workingType= =null) throw new Exception("Unab le to load type:
"+workingTy pe);
// Get Method
String strMethod = Convert.ToStrin g(configData["BusinessMethod "]);
MethodInfo methodInfo = workingType.Get Method(strMetho d);
if(methodInfo== null) throw new Exception(strAs sembly+" (type:
"+workingType+" ) doesnot contain "+strMethod );
// Type Instance
Object obj = assembly.Create Instance(strTyp e, true);
if(obj==null) throw new Exception("Inst antiation failed for
"+strType);
// Invoke Method
Object objReturnValue = methodInfo.Invo ke(obj, null);
return objReturnValue;
}
catch(Exception ex) { throw ex; }
}
Its just a thought, I hope this might be of some help. I cant predict
the correctness of the code since i wrote it in "wordpad" [i have a
problem with my VS.NET] :-)

Let me know in case of any inconsistancy.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd

Nov 17 '05 #6
MUS
Hello Again!

Just to make a typo correction in the first paragraph.

"Well this might not be the perfect Plugin approach (i.e. DONT have a
static
reference to client assembly) ..."

Plugin Approach is based on the idea that you DONT have a static
reference to an assembly and the fuctionality of the assembly is
exploited at runtime.

Regards,

Moiz Uddin Shaikh
Software Engineer
Kalsoft (Pvt) Ltd

Nov 17 '05 #7

Thanks Moiz for the sample code. I already have XML support in my appl. However your code
to load and invoke an external method will surely be reused shortly.

Gaetan.
Nov 17 '05 #8

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

Similar topics

2
1619
by: Theodore | last post by:
Hi, from within an executable I am calling class A located in a dll file which in turn calls class B located in another dll file. I want class B to be able to resolve the last entry assembly. By calling the ExecutingAssembly i am getting Class B assembly, if i call the CallingAssembly i get back the System.Windows.Forms Assembly and finally if a call the GetEntryAssembly i get the assembly of the executable. So how can i get the Class A...
13
2511
by: William Ryan | last post by:
I just picked up a copy of John Robbins' debugging book and started to look at disassembled code. Anyway, I hate optional Parameters in VB, but I was checking them out to see what IL is created. I've done this before with Modules, and saw that <gasp> they behave just like sealed classes with only static members. Anyway, it looks like Optional Parameters are nothing but a way to tell the compiler to write some overloads for you. So, in...
16
4057
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
3
4395
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here really understand how this could be an issue. The assemblies that the system is complaining about are ones that we build here and we're not changing version numbers on anything. The errors come and go with no apparent rhyme or reason. We do not...
12
2288
by: Nick Hounsome | last post by:
Can anyone tell me what the rational is for not supporting optional arguments. It is obviously a trivial thing to implement and, since C++ has them, I would not expect them to be omitted without a good reason.
4
5938
by: Joe HM | last post by:
Hello - I realize that there is no more IsMissing function in VB.NET but how can I have a boolean argument that is optional and in the code I need to determine whether it was passed it or not? I use the Single.NaN to set the default values for some of my optional single arguments so that I can determine whether an argument was passed it ... but that does not work for booleans.
1
1029
by: Phill W. | last post by:
Within an Assembly added to the Global Assembly Cache, System.Reflection.Assembly.GetExecuting.Location returns the path to the shadow(?) copy buried under C:\Windows\Assembly\Gac\... Is there an way of finding out where the DLL was /before/ it was added to the GAC? (and prefereably /without/ resorting to using the GetPrivateProfileString API call on the .ini file that resides within
14
3262
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
2
2015
by: Steve | last post by:
Kind of a strange question... I have a VB.NET 2.0 solution containing a main project (my EXE) and a number of other projects (class DLLs) that are "plug-ins" to the main app. These plugins get installed depending on each user's requirements. I'd like to implement a function in one plugin that only executes if another plugin is present. Typically, for one plugin to access a second plugin, I need to set a reference at design-time to the...
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.