Click here to Skip to main content
15,881,204 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to Use Array As Value Type Pin
Wes Aday22-Sep-12 10:42
professionalWes Aday22-Sep-12 10:42 
GeneralRe: How to Use Array As Value Type PinPopular
Dave Kreskowiak22-Sep-12 13:15
mveDave Kreskowiak22-Sep-12 13:15 
QuestionCreate Instance or invoke members of a class from a loaded assembly Pin
Danzy8322-Sep-12 1:57
Danzy8322-Sep-12 1:57 
AnswerRe: Create Instance or invoke members of a class from a loaded assembly Pin
Eddy Vluggen22-Sep-12 2:54
professionalEddy Vluggen22-Sep-12 2:54 
GeneralRe: Create Instance or invoke members of a class from a loaded assembly Pin
Danzy8322-Sep-12 4:18
Danzy8322-Sep-12 4:18 
GeneralRe: Create Instance or invoke members of a class from a loaded assembly Pin
Eddy Vluggen22-Sep-12 5:01
professionalEddy Vluggen22-Sep-12 5:01 
AnswerRe: Create Instance or invoke members of a class from a loaded assembly Pin
jschell22-Sep-12 8:44
jschell22-Sep-12 8:44 
AnswerRe: Create Instance or invoke members of a class from a loaded assembly Pin
BobJanova24-Sep-12 0:52
BobJanova24-Sep-12 0:52 
You can use the reflection API to call methods, properties and fields of any class, including those that you've loaded from an assembly at runtime. This is what Eddy's pointed you at.

However, reflection is slow, and also pretty cumbersome to write code for. If you know what methods you want to call, then that means you can define an interface, and if you're writing the assembly you're trying to load from (or people are writing them to an API you specify), you can make sure the class you try to load implements it. Put that interface in a library that both the main application and the external library can refer to (this can be actually in the application assembly in some cases), and make sure that the classes you want to interact with in the external assembly implement it. You can then cast the result of Activator.CreateInstance to the interface and call methods on the instance directly.

There are several examples of plugin architectures (because that's what your close to) available on CP. Here's a simple example modified from a previous version of my LobbyClient (the current one loads plugins into a separate AppDomain which you probably aren't looking for):

public static IGameType LoadContent(String filename){
 Assembly a = Assembly.LoadFrom(filename);
 if(a == null) throw new GameLoadException("Assembly "+filename+" not found or not valid");
 foreach(Module mod in a.GetLoadedModules()){
  foreach(Type ty in mod.GetTypes()){
   foreach(Type intf in ty.GetInterfaces()){
    if(intf == typeof(IGameType)){
     return (IGameType)System.Activator.CreateInstance(ty);
    }
   }
  }
 }
}


IGameType is specified somewhere that plugin classes can see it when you're writing plugins.
QuestionC# Application With MS Access Pin
SUBODH.SB21-Sep-12 23:39
SUBODH.SB21-Sep-12 23:39 
AnswerRe: C# Application With MS Access Pin
Eddy Vluggen22-Sep-12 2:56
professionalEddy Vluggen22-Sep-12 2:56 
AnswerRe: C# Application With MS Access Pin
Smart Arab23-Sep-12 10:39
Smart Arab23-Sep-12 10:39 
QuestionC# linq problem Pin
classy_dog21-Sep-12 11:38
classy_dog21-Sep-12 11:38 
AnswerRe: C# linq problem Pin
Matt T Heffron21-Sep-12 13:18
professionalMatt T Heffron21-Sep-12 13:18 
Questionputting VS projects/solutions in a TFS team project Pin
chuckdawit21-Sep-12 7:58
chuckdawit21-Sep-12 7:58 
AnswerRe: putting VS projects/solutions in a TFS team project Pin
Emmanuel Medina21-Sep-12 11:30
professionalEmmanuel Medina21-Sep-12 11:30 
GeneralRe: putting VS projects/solutions in a TFS team project Pin
chuckdawit21-Sep-12 11:52
chuckdawit21-Sep-12 11:52 
QuestionMonoDroid Pin
mAzeem2221-Sep-12 5:55
mAzeem2221-Sep-12 5:55 
AnswerRe: MonoDroid Pin
Emmanuel Medina21-Sep-12 11:34
professionalEmmanuel Medina21-Sep-12 11:34 
GeneralRe: MonoDroid Pin
mAzeem2221-Sep-12 19:57
mAzeem2221-Sep-12 19:57 
GeneralRe: MonoDroid Pin
Richard MacCutchan21-Sep-12 21:15
mveRichard MacCutchan21-Sep-12 21:15 
QuestionC# using linq Pin
classy_dog21-Sep-12 5:53
classy_dog21-Sep-12 5:53 
AnswerRe: C# using linq Pin
Eddy Vluggen21-Sep-12 6:14
professionalEddy Vluggen21-Sep-12 6:14 
GeneralRe: C# using linq Pin
Paul Conrad22-Sep-12 11:19
professionalPaul Conrad22-Sep-12 11:19 
QuestionSend commands to DOS using C# Pin
kirangowle21-Sep-12 5:25
kirangowle21-Sep-12 5:25 
AnswerRe: Send commands to DOS using C# Pin
Thomas Daniels21-Sep-12 5:30
mentorThomas Daniels21-Sep-12 5:30 

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.