Click here to Skip to main content
Click here to Skip to main content

Dynamically appeal of methods from a class

, 25 Apr 2006
Rate this:
Please Sign up or sign in to vote.
An article showing how to call dynamical (from url) the methods from a class

Introduction

Sample Image of Default Page

The picture shows the executed page after click on -Default Page- link, or on initial load.

This article wants to point on dynamic aspect of requesting the functions during the execution of your web site. Think on the folowing situation: you have to invoke some methods from a class, in this example we call it: DynChooseFunc, but we want this to do from the url and not from code like: NameFunc(), but NameFunc - represents a parameter in the url string, for example: http://www.yoursite.net/default.aspx?action=NameFunc. Now that you got the idea what this code does, let's examine how can we accomplish this.

Repesentation

Sample Image of Specific Page

The picture shows the executed page after click on -Specific Page- link.

Here I must mention that the Default.aspx contains a TreeView (From Navigation Panel) ans a Label (From General Panel). The label has the goal of showing us what is currently loaded in our browser.

Using the code

Here I present you the class DynChooseFunc with two functions: PageNameDefault() and PageNameSpecific(). They both return string value (for simplicity, of course). We consider the source code of the file DynChooseFunc.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AppDiadiora
{
    /// <span class="code-SummaryComment"><summary>
</span>    /// Class with methods and propreties 
    /// that are loaded selective from default.aspx
    /// <span class="code-SummaryComment"></summary>
</span>    public class DynChooseFunc
    {
        public DynChooseFunc()
        {
        }

        public string PageNameDefault()
        {
            return "Default page";
        }

        public string PageNameSpecific()
        {
            return "Index page";
        }

    }
}

And next is the code behind for default.aspx

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AppDiadiora;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DynChooseFunc dcf = new DynChooseFunc();

        
        if (Request.Params["action"] != null)
        {
            string strMethodName = Request.Params["action"];
            //execute the DynChooseFunc.<<strMethodName>> method
            lbTitle.Text = dcf.GetType().GetMethod(strMethodName).Invoke(dcf, null).ToString();
        }
        else
        {
            //execute the DynChooseFunc.PageNameDefault method
            lbTitle.Text = dcf.GetType().GetMethod("PageNameDefault").Invoke(dcf, null).ToString();
        }


    }
}

Comments

Creation of an object of class DynChooseFunc() in order to gain access to Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

DynChooseFunc dcf = new DynChooseFunc();

Obtains the action value from the URL:

Request.Params["action"]

Invokes the method strMethodName from DynChooseFunc() class. Here I must mention that strMethodName is dynammicaly interclaced in code:

dcf.GetType().GetMethod(strMethodName).Invoke(dcf, null)

Assign the returned function's result to the label named lbTitle in order to show the most simply possible the results. Of course you may make it more sophysticated to work for example with xml files, datasets and so on.

lbTitle.Text = dcf.GetType().GetMethod(strMethodName).Invoke(dcf, null).ToString();

Conclusions

If you look more closely and understand how this works, than in my opinion it's another implementation of the proxy pattern.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Diadiora Alexandru
Software Developer (Senior)
Moldova (Republic Of) Moldova (Republic Of)
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 25 Apr 2006
Article Copyright 2006 by Diadiora Alexandru
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid