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



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.DictionarySectionHandler" />
</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

.



Relevant Pages

  • Re: C++ debug/release builds and project references
    ... Is your class library in configuration "Release"? ... How do you add reference in the Winform project? ... suspect if you add the ManagedClassLib.dll in the debug folder. ... Creating command line "cl.exe ...
    (microsoft.public.dotnet.languages.vc)
  • Never mind.
    ... different configuration. ... the entry in the GAC did not match the ... The entry in the reference pointed to an incorrect ... > the app's config file before shutting down. ...
    (microsoft.public.dotnet.distributed_apps)
  • Going nuts - Configuration Error
    ... An error occurred during the processing of a configuration file ... And a Reference to a DLL to handle my dataaccess - also created my myself. ... partial, or location-based assembly bind). ... Attempting download of new URL ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Reference per configuration
    ... I can't define reference for a specific ... >> the "Debug" configuration I use Nunit for testing, ...
    (microsoft.public.dotnet.framework)
  • Re: New Motherboard Install
    ... Reinstall FC5 over my existing setup ... Kudzu figure it out. ... You will probably have to run the X configuration tool before you ... Step one will not take long to do, and is worth ...
    (Fedora)