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

C#

 
GeneralRe: Silent EXE application Pin
leppie27-Mar-03 8:47
leppie27-Mar-03 8:47 
GeneralRe: Silent EXE application Pin
Member 13901627-Mar-03 18:17
Member 13901627-Mar-03 18:17 
GeneralRe: Silent EXE application Pin
leppie28-Mar-03 7:13
leppie28-Mar-03 7:13 
GeneralParsing XML frm C# webservice to VB6 - HELP!! Pin
stuck!27-Mar-03 4:05
sussstuck!27-Mar-03 4:05 
GeneralDynamic list of classes Pin
econner27-Mar-03 3:28
econner27-Mar-03 3:28 
GeneralRe: Dynamic list of classes Pin
Nick Pirocanac27-Mar-03 9:52
Nick Pirocanac27-Mar-03 9:52 
GeneralRe: Dynamic list of classes Pin
econner27-Mar-03 18:15
econner27-Mar-03 18:15 
GeneralRe: Dynamic list of classes Pin
Nick Pirocanac27-Mar-03 18:33
Nick Pirocanac27-Mar-03 18:33 
Take another look at ValidRunnableClass. It's an abstract class which exposes one method, Run(). If you change your example above a hair, to look like:

public class MyTestFuction : ValidRunnableClass
{

// Change the Execute call to match the Run() method in ValidRunnableClass.
public override void Run(string [] args)
{
// do something here
}
}

Now, you have a class that has a common interface. You can add more classes at will, without having to add any switch/cases at all. Next, to cause the execution check my example again:

Type [] aTypes = System.Reflection.Assembly.GetExecutingAssembly().GetTypes();

foreach(Type oCurrType in aTypes)
{
if(oCurrType.ToString() == args[0] && oCurrType.IsSubclassOf(Type.GetType("RunArbitraryClasss.ValidRunnableClass")))
{
// Get the constructor for the class to invoke the run method on.
ConstructorInfo ci = oCurrType.GetConstructor(Type.EmptyTypes);

// Create an instance of the class that is derived from
// ValidRunnableClass. This will give us our prototype for invoking the
// Run method on the subclass.
ValidRunnableClass vrc = (ValidRunnableClass) ci.Invoke(null);

// Drop the first arg from the args list.
string [] passArgs = new string [args.Length - 1];
int i = 0;
for(i = 1; i < args.Length; i++)
{
passArgs[i - 1] = args[i];
}

// Execute the target class.
return(vrc.Run(passArgs));
}
}

throw new Exception("A class was specified that is not subclassed from ValidRunnableClass!");}


First, you create an instance of the ValidRunnableClass interface from a System.Type object. You do this with the System.Type.GetConstructor() function. Then, you use the ConstructorInfo.Invoke() method to create a ValidRunnableClass object. Then, you call the Run() method on the ValidRunnableClass which does your work. Because you extended MyTestFunction from ValidRunnableClass, it will call the proper method in MyTestFunction automatically!

Ok, I think you should be able to make it go from here.

Good luck!

Nick.
GeneralRe: How do you make buttons with icons? Pin
Nick Seng26-Mar-03 16:59
Nick Seng26-Mar-03 16:59 
GeneralRe: How do you make buttons with icons? Pin
Don_s26-Mar-03 21:50
Don_s26-Mar-03 21:50 
GeneralSend Hex file to CommPort Pin
Member Salamon26-Mar-03 12:41
Member Salamon26-Mar-03 12:41 
GeneralMessageBox's Pin
jtmtv1826-Mar-03 10:07
jtmtv1826-Mar-03 10:07 
GeneralRe: MessageBox's Pin
Le centriste26-Mar-03 11:05
Le centriste26-Mar-03 11:05 
GeneralRe: MessageBox's Pin
jtmtv1826-Mar-03 12:34
jtmtv1826-Mar-03 12:34 
GeneralRe: MessageBox's Pin
Nick Seng26-Mar-03 16:57
Nick Seng26-Mar-03 16:57 
GeneralBitmaps and Graphics Pin
Jon Newman26-Mar-03 8:06
Jon Newman26-Mar-03 8:06 
GeneralRe: Bitmaps and Graphics Pin
Nathan Blomquist26-Mar-03 8:16
Nathan Blomquist26-Mar-03 8:16 
GeneralRe: Bitmaps and Graphics Pin
Jon Newman26-Mar-03 8:23
Jon Newman26-Mar-03 8:23 
GeneralRe: Bitmaps and Graphics Pin
Jon Newman26-Mar-03 8:27
Jon Newman26-Mar-03 8:27 
GeneralBuild Properties problem Pin
Le centriste26-Mar-03 5:44
Le centriste26-Mar-03 5:44 
GeneralRe: Build Properties problem Pin
Thomas Freudenberg8-Apr-03 14:42
Thomas Freudenberg8-Apr-03 14:42 
QuestionPID of current process? Pin
se99ts26-Mar-03 5:13
se99ts26-Mar-03 5:13 
AnswerRe: PID of current process? Pin
Stephane Rodriguez.26-Mar-03 5:22
Stephane Rodriguez.26-Mar-03 5:22 
GeneralRe: PID of current process? Pin
se99ts26-Mar-03 5:25
se99ts26-Mar-03 5:25 
GeneralRe: PID of current process? Pin
Stephane Rodriguez.26-Mar-03 6:10
Stephane Rodriguez.26-Mar-03 6:10 

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.