Click here to Skip to main content
15,902,447 members
Home / Discussions / C#
   

C#

 
GeneralRe: Package Application Pin
Gavin_Mannion26-Feb-02 21:56
Gavin_Mannion26-Feb-02 21:56 
GeneralInherit 2 classes in c# Pin
thongkk24-Feb-02 14:39
thongkk24-Feb-02 14:39 
GeneralRe: Inherit 2 classes in c# Pin
Tom Archer24-Feb-02 15:31
Tom Archer24-Feb-02 15:31 
QuestionHow to import C# dll dynamically Pin
///Ansch24-Feb-02 14:11
///Ansch24-Feb-02 14:11 
AnswerRe: How to import C# dll dynamically Pin
Peter Stephens25-Feb-02 8:04
Peter Stephens25-Feb-02 8:04 
GeneralRe: How to import C# dll dynamically Pin
James T. Johnson25-Feb-02 8:29
James T. Johnson25-Feb-02 8:29 
GeneralRe: How to import C# dll dynamically Pin
Peter Stephens25-Feb-02 10:16
Peter Stephens25-Feb-02 10:16 
AnswerRe: How to import C# dll dynamically Pin
James T. Johnson25-Feb-02 8:27
James T. Johnson25-Feb-02 8:27 
First you use Assembly myAssembly = Assembly.LoadFrom(myDllPath); passing in the location of your dll.

Next you use the returned Assembly and search it for the Type you want to use (Type is the overlying type for all classes, enums, structs, delegates, and events). Type myType = myAssembly.GetType("myNamespace.myType");.

Now with your type you can create an instance of it. object myInstance = Activator.CreateInstance(myType);

Now comes the fun part, invoking your method!

string retVal = (string) myType.InvokeMember("MyMethodName", BindingFlags.Public | BindingFlags.InvokeMethod, null, myInstance, new object { /* any parameters here, leave empty if there are none */ } );

There you have it! You've not successfully run a method and retreived the return value.

To sum it up again,

Assembly myAssembly = Assembly.LoadFrom(/* Assembly filename/path */ myDllFilename);
Type myType = myAssembly.GetType(/* The type to load, with namespace */ myTypeName);
object myInstance = Activator.CreateInstance(/* The Type for the type to create */ myType);
string retVal = (string) myType.InvokeMember(
  /* Method name */ "myMethodName", 
  /* BindingFlags, tells what to look for */ BindingFlags.Public | BindingFlags.InvokeMethod, 
  /* The binder to use, null to use the defaul */ null, 
  /* The instance of the type to execute the method on */ myInstance, 
  /* The parameters to the method */ new object { /* Leave empty if no parameters */ }
);
HTH,

James

Sonork ID: 100.11138 - Hasaki
"My words but a whisper -- your deafness a SHOUT.
I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972

AnswerRe: How to import C# dll dynamically Pin
///Ansch25-Feb-02 13:23
///Ansch25-Feb-02 13:23 
GeneralTypes of exceptions Pin
23-Feb-02 19:40
suss23-Feb-02 19:40 
GeneralRe: Types of exceptions Pin
James T. Johnson23-Feb-02 19:49
James T. Johnson23-Feb-02 19:49 
GeneralWhy cant i declare this! Pin
23-Feb-02 18:20
suss23-Feb-02 18:20 
GeneralRe: Why cant i declare this! Pin
James T. Johnson23-Feb-02 20:03
James T. Johnson23-Feb-02 20:03 
GeneralRe: Why cant i declare this! Pin
Phil Wright28-Feb-02 22:58
Phil Wright28-Feb-02 22:58 
GeneralEasy question: bin & obj directories Pin
TigerNinja_23-Feb-02 16:10
TigerNinja_23-Feb-02 16:10 
GeneralRe: Easy question: bin & obj directories Pin
James T. Johnson23-Feb-02 19:46
James T. Johnson23-Feb-02 19:46 
GeneralResource file (.resource/.resx) help/howto Pin
Andrew Connell23-Feb-02 4:55
Andrew Connell23-Feb-02 4:55 
GeneralRe: Resource file (.resource/.resx) help/howto Pin
Andrew Connell23-Feb-02 13:37
Andrew Connell23-Feb-02 13:37 
GeneralRe: Resource file (.resource/.resx) help/howto Pin
Peter Stephens24-Feb-02 11:18
Peter Stephens24-Feb-02 11:18 
GeneralCompile error, help! Pin
22-Feb-02 16:55
suss22-Feb-02 16:55 
GeneralRe: Compile error, help! Pin
James T. Johnson22-Feb-02 17:11
James T. Johnson22-Feb-02 17:11 
GeneralRe: Compile error, help! Pin
Anders Molin24-Feb-02 11:28
professionalAnders Molin24-Feb-02 11:28 
GeneralRe: Compile error, help! Pin
Peter Stephens24-Feb-02 11:43
Peter Stephens24-Feb-02 11:43 
GeneralChanging value of all TextBoxes Pin
Gavin_Mannion21-Feb-02 22:59
Gavin_Mannion21-Feb-02 22:59 
GeneralRe: Changing value of all TextBoxes Pin
SimonS21-Feb-02 23:07
SimonS21-Feb-02 23:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.