PC Review


Reply
Thread Tools Rate Thread

C# Newbie Question: Dynamically Calling a DLL

 
 
=?Utf-8?B?bW9mbGFoZXJ0eQ==?=
Guest
Posts: n/a
 
      9th Mar 2005
I have created a simple DLL that contains this code:

using System;
namespace Test2
{
public class Test
{
int intAmount = 100;
public int GetValue()
{
return intAmount;
}
}
}

When I want to dynamically reference this DLL and return its value, how do I
accomplish this?

I was doing this:

string sDLLName = "C:\\Test\\Test2.dll";
Assembly objAssembly = System.Reflection.Assembly.LoadFrom(sDLLName);
MethodInfo Method = objAssembly.GetTypes()[0].GetMethod("GetValue");

This code seems to function fine to this point, but any attempts to try to
"invoke" or retrieve the value from the DLL fail miserably. (I am leaving all
of that code out; it is all green anyway It may be something simple, but I
am missing the "zen" of this completely. Any help would be appreciated. Once
I have the method, what is my next step?

Thanks!
Mo
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      9th Mar 2005
>This code seems to function fine to this point, but any attempts to try to
>"invoke" or retrieve the value from the DLL fail miserably.


Failing how, exactly?

Since it's an instance method you need an instance of Test to call it
on. I don't see you creating that anywhere.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
Joshua Flanagan
Guest
Posts: n/a
 
      10th Mar 2005
I'm not sure what you've already tried, but something along these lines
should work:

string sAssemblyName="Test2";
// string sDLLName = @"c:\Test\" + sAssemblyName + ".dll";
// You should always use Assemly.Load (specifying the assembly name)
// if you are trying to use the functionality provided in the assembly.
// You generally should only use Assembly.LoadFrom if you are making a
// a utility that analyzes the contents of the assembly.
Assembly assembly = Assembly.Load(sAssemblyName);
object testObject = assembly.CreateInstance("Test2.Test");
MethodInfo method = testObject.GetType().GetMethod("GetValue");
object result = method.Invoke(testObject, new object[0]);


As a nitpicky aside - try to break the habit of using using the "obj"
pseudo-hungarian prefix on your variables. If you need to keep using
"str" for your strings, fine, but "obj" makes absolutely no sense.
EVERY variable in .NET is an object. Do you really want to prepend
"obj" to every variable?

Hope this helps.

Joshua Flanagan
http://flimflan.com/blog

moflaherty wrote:
> I have created a simple DLL that contains this code:
>
> using System;
> namespace Test2
> {
> public class Test
> {
> int intAmount = 100;
> public int GetValue()
> {
> return intAmount;
> }
> }
> }
>
> When I want to dynamically reference this DLL and return its value, how do I
> accomplish this?
>
> I was doing this:
>
> string sDLLName = "C:\\Test\\Test2.dll";
> Assembly objAssembly = System.Reflection.Assembly.LoadFrom(sDLLName);
> MethodInfo Method = objAssembly.GetTypes()[0].GetMethod("GetValue");
>
> This code seems to function fine to this point, but any attempts to try to
> "invoke" or retrieve the value from the DLL fail miserably. (I am leaving all
> of that code out; it is all green anyway It may be something simple, but I
> am missing the "zen" of this completely. Any help would be appreciated. Once
> I have the method, what is my next step?
>
> Thanks!
> Mo

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie question: dynamically adding a textbox to a form Albert D. Kallal Microsoft Access Form Coding 6 11th Jan 2007 02:32 PM
Calling API functions dynamically? Usenet User Microsoft VB .NET 46 30th May 2006 04:16 PM
Calling functions dynamically hardieca@hotmail.com Microsoft C# .NET 3 23rd Mar 2006 03:40 PM
Add hyperlinks dynamically - newbie question JezB Microsoft ASP .NET 6 25th May 2004 09:12 PM
creating controls dynamically, newbie question (web project) Marius Horak Microsoft C# .NET 4 13th Aug 2003 11:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:44 AM.