Matt

Matt

  • NA
  • 4
  • 0

Am I going about this the right way?

Mar 11 2007 10:44 AM
hello there!


i'm new to c#, and as i'm looking to avoid developing any bad programming habits, i'd like to run my program design past all you experts just to make sure i'm not heading in the wrong direction.

can anybody see anything that might be wrong with the following idea?


i want to be able to call functions dynamically in order to avoid using large hard-coded switch statements; basically, the c# equivalent of the following vbscript code:


<code>
'this is vbscript
Execute("Call Form_" & intFormNum & "()")
</code>


at present, i achieve this by passing a dynamically built string out (from a class in an .aspx) into an external class that uses the 'Reflection' object:

<code>
/* this method calls methods that exist in other classes, based upon string representations of
the method names */

public object Run(string strClassName, string strMethodName)

{

Assembly asm = Assembly.GetExecutingAssembly();

Type type = asm.GetType(strClassName);

object obj = asm.CreateInstance(strClassName);

 

object ret = type.InvokeMember(strMethodName,

BindingFlags.Default | BindingFlags.InvokeMethod,

null, obj, null);

return ret;

}

</code>


the code works just fine, and i can invoke the methods ok, but the whole thing feels weird and a bit unnecessary.

in a nutshell, i'm using an external class to call methods that exist in the class that called the external class.
is this a normal thing to do?

if anybody could let me know whether there's an easier way of achieving my goal, i'd be very appreciative.



Thanks!