Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2005
    Location
    greater manchester, uk
    Posts
    65
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Dynamically Invoking Methods with Reflection

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

    Code:
    /*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:


    Code:
    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:


    Code:
    /*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?

  • #2
    New Coder
    Join Date
    Mar 2005
    Location
    greater manchester, uk
    Posts
    65
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Context

    well, i found a way around the context error.

    if i reference the 'Context' object before my 'Response.Write' inside the method that outputs the HTML code.......

    Code:
    Context.Response.Write("blah.......");

    .......i can invoke the method from outside the class without causing any error.

    i'm still only guessing as to why this works though, and if anybody could shed some light on the subject i'd be very grateful.


    Thanks!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •