MattyJim
03-10-2007, 04:11 PM
hello there!
i've created a web application using asp.net and c#, part of which uses the Reflection API in order to dynamically invoke methods.
basically, the situation is:
1. i have an .aspx that contains a class with 4 methods:
'Form_1()', 'Form_2()', 'Form_3()' and 'Form_4()'
2. Each of these methods uses Response.Write to output HTML.
3. I want to call each of these methods dynamically.......
/*something like this, for example*/
"Form_" + intFormNum
.......which is where Reflection comes in.
4. I pass strings like the one in the example above, containing the calling class name and method name into an object in a seperate class in order to invoke each method:
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;
}
.......so that this can be called with something like:
/*assuming that my Run method is part of a class named 'assembly_methods', and it's being called from a class named 'Setup' in a namespace called 'NET_application'*/
assembly_methods AM = new assembly_methods();
AM.Run("NET_application.Setup", "Form_1");
5. this works fine up to a point.
i can invoke each method, but i always get an error explaning that i can't use 'Response' in this context (presumably because i'm calling it from a different class).
what i'd like to know is:
a) whether there's a 'cleaner' way to achieve what i'm trying to do (all this business of invoking methods from other classes seems like a bit of a hack)
b) what's the best way to get around the Response context problem? is it possible to pass in a class reference or something like that?
i've created a web application using asp.net and c#, part of which uses the Reflection API in order to dynamically invoke methods.
basically, the situation is:
1. i have an .aspx that contains a class with 4 methods:
'Form_1()', 'Form_2()', 'Form_3()' and 'Form_4()'
2. Each of these methods uses Response.Write to output HTML.
3. I want to call each of these methods dynamically.......
/*something like this, for example*/
"Form_" + intFormNum
.......which is where Reflection comes in.
4. I pass strings like the one in the example above, containing the calling class name and method name into an object in a seperate class in order to invoke each method:
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;
}
.......so that this can be called with something like:
/*assuming that my Run method is part of a class named 'assembly_methods', and it's being called from a class named 'Setup' in a namespace called 'NET_application'*/
assembly_methods AM = new assembly_methods();
AM.Run("NET_application.Setup", "Form_1");
5. this works fine up to a point.
i can invoke each method, but i always get an error explaning that i can't use 'Response' in this context (presumably because i'm calling it from a different class).
what i'd like to know is:
a) whether there's a 'cleaner' way to achieve what i'm trying to do (all this business of invoking methods from other classes seems like a bit of a hack)
b) what's the best way to get around the Response context problem? is it possible to pass in a class reference or something like that?