473,378 Members | 1,469 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,378 software developers and data experts.

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 2023
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*****@somewhere.com> wrote in message
news:8u********************************@4ax.com...
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=Activator.CreateInstance(assemblyName,
typeName).Unwrap();

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.typeName
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=dynamicObj.GetType();
MethodInfo dynamicMethod=dynamicType.GetMethod(methodName);

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=dynamicMethod.Invoke(dynamicObject, 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=Activator.CreateInstance(assemblyName,
typeName).Unwrap();

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.typeName
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=dynamicObj.GetType();
MethodInfo dynamicMethod=dynamicType.GetMethod(methodName);

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=dynamicMethod.Invoke(dynamicObject, 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" ?>
<configuration>
<configSections>
<section name="ClientSection"
type="System.Configuration.DictionarySectionHandle r" />
</configSections>
<!-- Custom Section -->
<ClientSection>
<add key="BusinessAssembly" value="<your_assembly>" />
<add key="BusinessType" value="<your_type>" />
<add key="BusinessMethod" value="<your_method>" />
</ClientSection>
<!-- / Custom Section -->
</configuration>
public Object DoAction()
{
try
{
Hashtable configData = (Hashtable)
ConfigurationSettings.GetConfig("ClientSection");
if(configData==null) throw new ArgumentNullException("Unable to read
configuration section");
// Get Assembly
String strAssembly =
Convert.ToString(configData["BusinessAssembly"]);
Assembly assembly = Assembly.Load(strAssembly);
if(assembly==null) throw new Exception("Unable to load assembly:
"+assemblyName);
// Get Type
String strType = Convert.ToString(configData["BusinessType"]);
Type workingType = assembly.GetType(strType);
if(workingType==null) throw new Exception("Unable to load type:
"+workingType);
// Get Method
String strMethod = Convert.ToString(configData["BusinessMethod"]);
MethodInfo methodInfo = workingType.GetMethod(strMethod);
if(methodInfo==null) throw new Exception(strAssembly+" (type:
"+workingType+") doesnot contain "+strMethod);
// Type Instance
Object obj = assembly.CreateInstance(strType, true);
if(obj==null) throw new Exception("Instantiation failed for
"+strType);
// Invoke Method
Object objReturnValue = methodInfo.Invoke(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
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...
13
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. ...
16
by: ad | last post by:
Does C#2.0 support optional parameters like VB.NET: Function MyFunction(Optional ByVal isCenter As Boolean = False)
3
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...
12
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...
4
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? ...
1
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...
14
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...
2
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...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.