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

C#

 
GeneralSilent EXE application Pin
Dr. Freud27-Mar-03 4:16
Dr. Freud27-Mar-03 4:16 
GeneralRe: Silent EXE application Pin
Rocky Moore27-Mar-03 4:55
Rocky Moore27-Mar-03 4:55 
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 
Hi!

You can try this code snippit, I think it's what you want to do. I made this type of thing before for a Java proggie, and it was even easier in C#! Good Luck!

A few runs look like:

------------------------------
RunArbitraryClasss.exe RunArbitraryClasss.NonRunnableClass arg1 arg2

Unhandled Exception: System.Exception: A class was specified that is not subclassed from ValidRunnableClass!
at RunArbitraryClasss.RunArbitraryClasss.Run(String[] args) in c:\source\automation\runarbitraryclasss\runarbitraryclasss.cs:line 53
at RunArbitraryClasss.RunArbitraryClasss.Main(String[] args) in c:\source\automation\runarbitraryclasss\runarbitraryclasss.cs:line 20

------------------------------
RunArbitraryClasss.exe RunArbitraryClasss.RunnableClass arg1 arg2
I'm a runnable class: arg1 arg2




Here's the source!
--------------------------------------------------------------

using System;
using System.Reflection;

namespace RunArbitraryClasss
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class RunArbitraryClasss
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static int Main(string[] args)
		{
			// Get the program running.
			RunArbitraryClasss oRunArbitraryClasss = new RunArbitraryClasss();

			return(oRunArbitraryClasss.Run(args));
		}

		public int Run(string[] args)
		{
			// Get a list of valid types.
			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!");
		}
	}

	abstract class ValidRunnableClass
	{
		abstract public int Run(string [] args);
	}

	class RunnableClass : ValidRunnableClass
	{
		public override int Run(string [] args)
		{
			Console.Write("I'm a runnable class:");
			
			foreach(string tempStr in args)
				Console.Write(" {0}", tempStr);

			return(0);
		}
	}

	class NonRunnableClass
	{
		public int Run(string [] args)
		{
			Console.Write("I'm a NON-runnable class:");
			
			foreach(string tempStr in args)
				Console.Write(" {0}", tempStr);

			return(0);
		}
	}
} 


Nick.
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 
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 

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.