Retrieving derived class name inside base class

Discussion in 'ASP .Net' started by Goran Djuranovic, Sep 10, 2007.

  1. Hi all,
    Is there a way to retrieve a derived class name inside a subroutine or a
    function of the base class? I am trying to get some data from the database,
    based on which derived class is calling the subroutine. I know the base
    class name can be retrieved by using reflection namespace:
    System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.

    Thanks
    Goran
     
    Goran Djuranovic, Sep 10, 2007
    #1
    1. Advertising

  2. It would be very much against the whole idea of inheritance. Instead you
    should make a virtual GetData method in the base class and override it in
    the derived classes.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "Goran Djuranovic" <> wrote in message
    news:%...
    > Hi all,
    > Is there a way to retrieve a derived class name inside a subroutine or a
    > function of the base class? I am trying to get some data from the
    > database, based on which derived class is calling the subroutine. I know
    > the base class name can be retrieved by using reflection namespace:
    > System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >
    > Thanks
    > Goran
    >
     
    Eliyahu Goldin, Sep 10, 2007
    #2
    1. Advertising

  3. Eliyahu, thanks, but I would not like to have to type anything on the derived classes (not even override methods), if possible.

    Maybe I should explain the problem in detail. What I am trying to do is re-use a menu control and its methods on 10 different web pages with as less coding as possible. So, here is how I planned to do it:
    - have every .aspx page contain a menu control (nothing declared in the code-behind, because it would conflict with the base class)
    - have a base class contain all the methods for menu control (plus base class inherits from System.Web.UI.Page)
    - have every page inherit from a base class
    - every page has a subset of menu items, which is filterred based on what the page (class name) is, and this is where the problem is, because I cannot retrieve the name of the derived class from within the base class.

    Wish I could use multiple inheritance (without having to use the interfaces).

    Thanks
    Goran

    "Eliyahu Goldin" <> wrote in message news:...
    > It would be very much against the whole idea of inheritance. Instead you
    > should make a virtual GetData method in the base class and override it in
    > the derived classes.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "Goran Djuranovic" <> wrote in message
    > news:%...
    >> Hi all,
    >> Is there a way to retrieve a derived class name inside a subroutine or a
    >> function of the base class? I am trying to get some data from the
    >> database, based on which derived class is calling the subroutine. I know
    >> the base class name can be retrieved by using reflection namespace:
    >> System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >>
    >> Thanks
    >> Goran
    >>

    >
    >
     
    Goran Djuranovic, Sep 10, 2007
    #3
  4. You can always call GetType() method, can't you?

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "Goran Djuranovic" <> wrote in message
    news:ejI$...
    Eliyahu, thanks, but I would not like to have to type anything on the
    derived classes (not even override methods), if possible.

    Maybe I should explain the problem in detail. What I am trying to do is
    re-use a menu control and its methods on 10 different web pages with as less
    coding as possible. So, here is how I planned to do it:
    - have every .aspx page contain a menu control (nothing declared in the
    code-behind, because it would conflict with the base class)
    - have a base class contain all the methods for menu control (plus base
    class inherits from System.Web.UI.Page)
    - have every page inherit from a base class
    - every page has a subset of menu items, which is filterred based on what
    the page (class name) is, and this is where the problem is, because I cannot
    retrieve the name of the derived class from within the base class.

    Wish I could use multiple inheritance (without having to use the
    interfaces).

    Thanks
    Goran

    "Eliyahu Goldin" <> wrote in
    message news:...
    > It would be very much against the whole idea of inheritance. Instead you
    > should make a virtual GetData method in the base class and override it in
    > the derived classes.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "Goran Djuranovic" <> wrote in message
    > news:%...
    >> Hi all,
    >> Is there a way to retrieve a derived class name inside a subroutine or a
    >> function of the base class? I am trying to get some data from the
    >> database, based on which derived class is calling the subroutine. I know
    >> the base class name can be retrieved by using reflection namespace:
    >> System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >>
    >> Thanks
    >> Goran
    >>

    >
    >
     
    Eliyahu Goldin, Sep 10, 2007
    #4
  5. Thanks for Eliyahu's input.

    Hi Goran,

    As Eliyahu has suggested, to getting the base type from OO and inheritance
    perspective is not quite realistic and recommended. However, based on my
    understanding on your scenario, what you want to do is actually get some
    information in the base class so as to determine which concrete child page
    is the current running one, correct?

    I've performed some research and the following ways should be some possible
    means:

    ** Since the base class is Page(which also derived from Control class), you
    can use the "this.Page" property to get the current running page class and
    reflect its type.


    ** Or I think it is better to use some control or markup on the page to
    detect the current page since you can use "this.Page.Controls" to get the
    running page's Control collection. For example, this.Page.Title can give
    you the page title.

    Here is a test base page class I used for test and it works well:

    >>>>>>>sample base page >>>>>>

    public class BasePageClass : Page
    {

    public string PageTypeName
    {
    get
    {
    return this.Page.GetType().FullName;
    }
    }

    public string PageTitle
    {
    get
    {
    return this.Page.Title;
    }
    }
    }
    <<<<<<<<<<<<<<<<<<<<<<

    Hope this helps.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead



    ==================================================

    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    ications.



    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/subscriptions/support/default.aspx.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.




    --------------------
    >From: "Eliyahu Goldin" <>
    >References: <#>

    <>
    <ejI$>
    >Subject: Re: Retrieving derived class name inside base class
    >Date: Tue, 11 Sep 2007 00:36:35 +0200


    >
    >You can always call GetType() method, can't you?
    >
    >--
    >Eliyahu Goldin,
    >Software Developer & Consultant
    >Microsoft MVP [ASP.NET]
    >http://msmvps.com/blogs/egoldin
    >
    >
    >"Goran Djuranovic" <> wrote in message
    >news:ejI$...
    >Eliyahu, thanks, but I would not like to have to type anything on the
    >derived classes (not even override methods), if possible.
    >
    >Maybe I should explain the problem in detail. What I am trying to do is
    >re-use a menu control and its methods on 10 different web pages with as

    less
    >coding as possible. So, here is how I planned to do it:
    >- have every .aspx page contain a menu control (nothing declared in the
    >code-behind, because it would conflict with the base class)
    >- have a base class contain all the methods for menu control (plus base
    >class inherits from System.Web.UI.Page)
    >- have every page inherit from a base class
    >- every page has a subset of menu items, which is filterred based on what
    >the page (class name) is, and this is where the problem is, because I

    cannot
    >retrieve the name of the derived class from within the base class.
    >
    >Wish I could use multiple inheritance (without having to use the
    >interfaces).
    >
    >Thanks
    >Goran
    >
    >"Eliyahu Goldin" <> wrote in
    >message news:...
    >> It would be very much against the whole idea of inheritance. Instead you
    >> should make a virtual GetData method in the base class and override it in
    >> the derived classes.
    >>
    >> --
    >> Eliyahu Goldin,
    >> Software Developer
    >> Microsoft MVP [ASP.NET]
    >> http://msmvps.com/blogs/egoldin
    >> http://usableasp.net
    >>
    >>
    >> "Goran Djuranovic" <> wrote in message
    >> news:%...
    >>> Hi all,
    >>> Is there a way to retrieve a derived class name inside a subroutine or a
    >>> function of the base class? I am trying to get some data from the
    >>> database, based on which derived class is calling the subroutine. I know
    >>> the base class name can be retrieved by using reflection namespace:
    >>> System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >>>
    >>> Thanks
    >>> Goran
    >>>

    >>
    >>

    >
    >
    >
     
    Steven Cheng[MSFT], Sep 11, 2007
    #5
  6. Hi Steven,
    Your code returns the name of the .aspx page which is OK, but I need the
    name of the .vb class behind it. So, for example, the ASPX page name in this
    case is "Login.aspx" and the code-behind class name is "Login". What I need
    returned is "Login", and not "Login.aspx".

    Also, "Login" class inherits from the base class.

    Thanks for your help guys.
    Goran

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Thanks for Eliyahu's input.
    >
    > Hi Goran,
    >
    > As Eliyahu has suggested, to getting the base type from OO and inheritance
    > perspective is not quite realistic and recommended. However, based on my
    > understanding on your scenario, what you want to do is actually get some
    > information in the base class so as to determine which concrete child page
    > is the current running one, correct?
    >
    > I've performed some research and the following ways should be some
    > possible
    > means:
    >
    > ** Since the base class is Page(which also derived from Control class),
    > you
    > can use the "this.Page" property to get the current running page class and
    > reflect its type.
    >
    >
    > ** Or I think it is better to use some control or markup on the page to
    > detect the current page since you can use "this.Page.Controls" to get the
    > running page's Control collection. For example, this.Page.Title can give
    > you the page title.
    >
    > Here is a test base page class I used for test and it works well:
    >
    >>>>>>>>sample base page >>>>>>

    > public class BasePageClass : Page
    > {
    >
    > public string PageTypeName
    > {
    > get
    > {
    > return this.Page.GetType().FullName;
    > }
    > }
    >
    > public string PageTitle
    > {
    > get
    > {
    > return this.Page.Title;
    > }
    > }
    > }
    > <<<<<<<<<<<<<<<<<<<<<<
    >
    > Hope this helps.
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    >
    >
    > ==================================================
    >
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    > ications.
    >
    >
    >
    > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 1 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions or complex
    > project analysis and dump analysis issues. Issues of this nature are best
    > handled working with a dedicated Microsoft Support Engineer by contacting
    > Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    >
    > --------------------
    >>From: "Eliyahu Goldin" <>
    >>References: <#>

    > <>
    > <ejI$>
    >>Subject: Re: Retrieving derived class name inside base class
    >>Date: Tue, 11 Sep 2007 00:36:35 +0200

    >
    >>
    >>You can always call GetType() method, can't you?
    >>
    >>--
    >>Eliyahu Goldin,
    >>Software Developer & Consultant
    >>Microsoft MVP [ASP.NET]
    >>http://msmvps.com/blogs/egoldin
    >>
    >>
    >>"Goran Djuranovic" <> wrote in message
    >>news:ejI$...
    >>Eliyahu, thanks, but I would not like to have to type anything on the
    >>derived classes (not even override methods), if possible.
    >>
    >>Maybe I should explain the problem in detail. What I am trying to do is
    >>re-use a menu control and its methods on 10 different web pages with as

    > less
    >>coding as possible. So, here is how I planned to do it:
    >>- have every .aspx page contain a menu control (nothing declared in the
    >>code-behind, because it would conflict with the base class)
    >>- have a base class contain all the methods for menu control (plus base
    >>class inherits from System.Web.UI.Page)
    >>- have every page inherit from a base class
    >>- every page has a subset of menu items, which is filterred based on what
    >>the page (class name) is, and this is where the problem is, because I

    > cannot
    >>retrieve the name of the derived class from within the base class.
    >>
    >>Wish I could use multiple inheritance (without having to use the
    >>interfaces).
    >>
    >>Thanks
    >>Goran
    >>
    >>"Eliyahu Goldin" <> wrote in
    >>message news:...
    >>> It would be very much against the whole idea of inheritance. Instead you
    >>> should make a virtual GetData method in the base class and override it
    >>> in
    >>> the derived classes.
    >>>
    >>> --
    >>> Eliyahu Goldin,
    >>> Software Developer
    >>> Microsoft MVP [ASP.NET]
    >>> http://msmvps.com/blogs/egoldin
    >>> http://usableasp.net
    >>>
    >>>
    >>> "Goran Djuranovic" <> wrote in message
    >>> news:%...
    >>>> Hi all,
    >>>> Is there a way to retrieve a derived class name inside a subroutine or
    >>>> a
    >>>> function of the base class? I am trying to get some data from the
    >>>> database, based on which derived class is calling the subroutine. I
    >>>> know
    >>>> the base class name can be retrieved by using reflection namespace:
    >>>> System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >>>>
    >>>> Thanks
    >>>> Goran
    >>>>
    >>>
    >>>

    >>
    >>
    >>

    >
     
    Goran Djuranovic, Sep 11, 2007
    #6
  7. Thanks for your reply Goran,

    Yes, my code example only return the page's title, however, it acually
    demonstrate that you can get the underlying running page's class through

    this.Page.GetType()

    You can print out this value. This class should be a dynamic generated
    type(by ASP.NET runtime). You can use its "BaseType" to get the further
    parent type. Thus, you can get all the classes in the page's inheritance
    chain. How do you think?

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.


    --------------------
    >Subject: Re: Retrieving derived class name inside base class
    >Date: Tue, 11 Sep 2007 09:58:14 -0400


    >Hi Steven,
    >Your code returns the name of the .aspx page which is OK, but I need the
    >name of the .vb class behind it. So, for example, the ASPX page name in

    this
    >case is "Login.aspx" and the code-behind class name is "Login". What I

    need
    >returned is "Login", and not "Login.aspx".
    >
    >Also, "Login" class inherits from the base class.
    >
    >Thanks for your help guys.
    >Goran
    >
    >"Steven Cheng[MSFT]" <> wrote in message
    >news:...
    >> Thanks for Eliyahu's input.
    >>
    >> Hi Goran,
    >>
    >> As Eliyahu has suggested, to getting the base type from OO and

    inheritance
    >> perspective is not quite realistic and recommended. However, based on my
    >> understanding on your scenario, what you want to do is actually get some
    >> information in the base class so as to determine which concrete child

    page
    >> is the current running one, correct?
    >>
    >> I've performed some research and the following ways should be some
    >> possible
    >> means:
    >>
    >> ** Since the base class is Page(which also derived from Control class),
    >> you
    >> can use the "this.Page" property to get the current running page class

    and
    >> reflect its type.
    >>
    >>
    >> ** Or I think it is better to use some control or markup on the page to
    >> detect the current page since you can use "this.Page.Controls" to get the
    >> running page's Control collection. For example, this.Page.Title can give
    >> you the page title.
    >>
    >> Here is a test base page class I used for test and it works well:
    >>
    >>>>>>>>>sample base page >>>>>>

    >> public class BasePageClass : Page
    >> {
    >>
    >> public string PageTypeName
    >> {
    >> get
    >> {
    >> return this.Page.GetType().FullName;
    >> }
    >> }
    >>
    >> public string PageTitle
    >> {
    >> get
    >> {
    >> return this.Page.Title;
    >> }
    >> }
    >> }
    >> <<<<<<<<<<<<<<<<<<<<<<
    >>
    >> Hope this helps.
    >>
    >> Sincerely,
    >>
    >> Steven Cheng
    >>
    >> Microsoft MSDN Online Support Lead
    >>
    >>
    >>
    >> ==================================================
    >>
    >> Get notification to my posts through email? Please refer to
    >>

    http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    >> ications.
    >>
    >>
    >>
    >> Note: The MSDN Managed Newsgroup support offering is for non-urgent

    issues
    >> where an initial response from the community or a Microsoft Support
    >> Engineer within 1 business day is acceptable. Please note that each

    follow
    >> up response may take approximately 2 business days as the support
    >> professional working with you may need further investigation to reach the
    >> most efficient resolution. The offering is not appropriate for situations
    >> that require urgent, real-time or phone-based interactions or complex
    >> project analysis and dump analysis issues. Issues of this nature are best
    >> handled working with a dedicated Microsoft Support Engineer by contacting
    >> Microsoft Customer Support Services (CSS) at
    >> http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >>
    >> ==================================================
    >>
    >>
    >> This posting is provided "AS IS" with no warranties, and confers no
    >> rights.
    >>
    >>
    >>
    >>
    >> --------------------
    >>>From: "Eliyahu Goldin" <>
    >>>References: <#>

    >> <>
    >> <ejI$>
    >>>Subject: Re: Retrieving derived class name inside base class
    >>>Date: Tue, 11 Sep 2007 00:36:35 +0200

    >>
    >>>
    >>>You can always call GetType() method, can't you?
    >>>
    >>>--
    >>>Eliyahu Goldin,
    >>>Software Developer & Consultant
    >>>Microsoft MVP [ASP.NET]
    >>>http://msmvps.com/blogs/egoldin
    >>>
    >>>
    >>>"Goran Djuranovic" <> wrote in message
    >>>news:ejI$...
    >>>Eliyahu, thanks, but I would not like to have to type anything on the
    >>>derived classes (not even override methods), if possible.
    >>>
    >>>Maybe I should explain the problem in detail. What I am trying to do is
    >>>re-use a menu control and its methods on 10 different web pages with as

    >> less
    >>>coding as possible. So, here is how I planned to do it:
    >>>- have every .aspx page contain a menu control (nothing declared in the
    >>>code-behind, because it would conflict with the base class)
    >>>- have a base class contain all the methods for menu control (plus base
    >>>class inherits from System.Web.UI.Page)
    >>>- have every page inherit from a base class
    >>>- every page has a subset of menu items, which is filterred based on what
    >>>the page (class name) is, and this is where the problem is, because I

    >> cannot
    >>>retrieve the name of the derived class from within the base class.
    >>>
    >>>Wish I could use multiple inheritance (without having to use the
    >>>interfaces).
    >>>
    >>>Thanks
    >>>Goran
    >>>
    >>>"Eliyahu Goldin" <> wrote in
    >>>message news:...
    >>>> It would be very much against the whole idea of inheritance. Instead

    you
    >>>> should make a virtual GetData method in the base class and override it
    >>>> in
    >>>> the derived classes.
    >>>>
    >>>> --
    >>>> Eliyahu Goldin,
    >>>> Software Developer
    >>>> Microsoft MVP [ASP.NET]
    >>>> http://msmvps.com/blogs/egoldin
    >>>> http://usableasp.net
    >>>>
    >>>>
    >>>> "Goran Djuranovic" <> wrote in

    message
    >>>> news:%...
    >>>>> Hi all,
    >>>>> Is there a way to retrieve a derived class name inside a subroutine

    or
    >>>>> a
    >>>>> function of the base class? I am trying to get some data from the
    >>>>> database, based on which derived class is calling the subroutine. I
    >>>>> know
    >>>>> the base class name can be retrieved by using reflection namespace:
    >>>>> System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >>>>>
    >>>>> Thanks
    >>>>> Goran
    >>>>>
    >>>>
    >>>>
    >>>
    >>>
    >>>

    >>

    >
    >
    >
     
    Steven Cheng[MSFT], Sep 12, 2007
    #7
  8. Hi Goran,

    Any further question or concern on this? If so, welcome to continue discuss
    here.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------

    >From: (Steven Cheng[MSFT])
    >Organization: Microsoft
    >Date: Wed, 12 Sep 2007 03:14:48 GMT
    >Subject: Re: Retrieving derived class name inside base class


    >Path: TK2MSFTNGHUB02.phx.gbl
    >Xref: TK2MSFTNGHUB02.phx.gbl microsoft.public.dotnet.framework.aspnet:42045
    >NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
    >
    >Thanks for your reply Goran,
    >
    >Yes, my code example only return the page's title, however, it acually
    >demonstrate that you can get the underlying running page's class through
    >
    > this.Page.GetType()
    >
    >You can print out this value. This class should be a dynamic generated
    >type(by ASP.NET runtime). You can use its "BaseType" to get the further
    >parent type. Thus, you can get all the classes in the page's inheritance
    >chain. How do you think?
    >
    >Sincerely,
    >
    >Steven Cheng
    >
    >Microsoft MSDN Online Support Lead
    >
    >This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >--------------------
    >>Subject: Re: Retrieving derived class name inside base class
    >>Date: Tue, 11 Sep 2007 09:58:14 -0400

    >
    >>Hi Steven,
    >>Your code returns the name of the .aspx page which is OK, but I need the
    >>name of the .vb class behind it. So, for example, the ASPX page name in

    >this
    >>case is "Login.aspx" and the code-behind class name is "Login". What I

    >need
    >>returned is "Login", and not "Login.aspx".
    >>
    >>Also, "Login" class inherits from the base class.
    >>
    >>Thanks for your help guys.
    >>Goran
    >>
    >>"Steven Cheng[MSFT]" <> wrote in message
    >>news:...
    >>> Thanks for Eliyahu's input.
    >>>
    >>> Hi Goran,
    >>>
    >>> As Eliyahu has suggested, to getting the base type from OO and

    >inheritance
    >>> perspective is not quite realistic and recommended. However, based on

    my
    >>> understanding on your scenario, what you want to do is actually get some
    >>> information in the base class so as to determine which concrete child

    >page
    >>> is the current running one, correct?
    >>>
    >>> I've performed some research and the following ways should be some
    >>> possible
    >>> means:
    >>>
    >>> ** Since the base class is Page(which also derived from Control class),
    >>> you
    >>> can use the "this.Page" property to get the current running page class

    >and
    >>> reflect its type.
    >>>
    >>>
    >>> ** Or I think it is better to use some control or markup on the page to
    >>> detect the current page since you can use "this.Page.Controls" to get

    the
    >>> running page's Control collection. For example, this.Page.Title can give
    >>> you the page title.
    >>>
    >>> Here is a test base page class I used for test and it works well:
    >>>
    >>>>>>>>>>sample base page >>>>>>
    >>> public class BasePageClass : Page
    >>> {
    >>>
    >>> public string PageTypeName
    >>> {
    >>> get
    >>> {
    >>> return this.Page.GetType().FullName;
    >>> }
    >>> }
    >>>
    >>> public string PageTitle
    >>> {
    >>> get
    >>> {
    >>> return this.Page.Title;
    >>> }
    >>> }
    >>> }
    >>> <<<<<<<<<<<<<<<<<<<<<<
    >>>
    >>> Hope this helps.
    >>>
    >>> Sincerely,
    >>>
    >>> Steven Cheng
    >>>
    >>> Microsoft MSDN Online Support Lead
    >>>
    >>>
    >>>
    >>> ==================================================
    >>>
    >>> Get notification to my posts through email? Please refer to
    >>>

    >http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#noti

    f
    >>> ications.
    >>>
    >>>
    >>>
    >>> Note: The MSDN Managed Newsgroup support offering is for non-urgent

    >issues
    >>> where an initial response from the community or a Microsoft Support
    >>> Engineer within 1 business day is acceptable. Please note that each

    >follow
    >>> up response may take approximately 2 business days as the support
    >>> professional working with you may need further investigation to reach

    the
    >>> most efficient resolution. The offering is not appropriate for

    situations
    >>> that require urgent, real-time or phone-based interactions or complex
    >>> project analysis and dump analysis issues. Issues of this nature are

    best
    >>> handled working with a dedicated Microsoft Support Engineer by

    contacting
    >>> Microsoft Customer Support Services (CSS) at
    >>> http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >>>
    >>> ==================================================
    >>>
    >>>
    >>> This posting is provided "AS IS" with no warranties, and confers no
    >>> rights.
    >>>
    >>>
    >>>
    >>>
    >>> --------------------
    >>>>From: "Eliyahu Goldin" <>
    >>>>References: <#>
    >>> <>
    >>> <ejI$>
    >>>>Subject: Re: Retrieving derived class name inside base class
    >>>>Date: Tue, 11 Sep 2007 00:36:35 +0200
    >>>
    >>>>
    >>>>You can always call GetType() method, can't you?
    >>>>
    >>>>--
    >>>>Eliyahu Goldin,
    >>>>Software Developer & Consultant
    >>>>Microsoft MVP [ASP.NET]
    >>>>http://msmvps.com/blogs/egoldin
    >>>>
    >>>>
    >>>>"Goran Djuranovic" <> wrote in message
    >>>>news:ejI$...
    >>>>Eliyahu, thanks, but I would not like to have to type anything on the
    >>>>derived classes (not even override methods), if possible.
    >>>>
    >>>>Maybe I should explain the problem in detail. What I am trying to do is
    >>>>re-use a menu control and its methods on 10 different web pages with as
    >>> less
    >>>>coding as possible. So, here is how I planned to do it:
    >>>>- have every .aspx page contain a menu control (nothing declared in the
    >>>>code-behind, because it would conflict with the base class)
    >>>>- have a base class contain all the methods for menu control (plus base
    >>>>class inherits from System.Web.UI.Page)
    >>>>- have every page inherit from a base class
    >>>>- every page has a subset of menu items, which is filterred based on

    what
    >>>>the page (class name) is, and this is where the problem is, because I
    >>> cannot
    >>>>retrieve the name of the derived class from within the base class.
    >>>>
    >>>>Wish I could use multiple inheritance (without having to use the
    >>>>interfaces).
    >>>>
    >>>>Thanks
    >>>>Goran
    >>>>
    >>>>"Eliyahu Goldin" <> wrote in
    >>>>message news:...
    >>>>> It would be very much against the whole idea of inheritance. Instead

    >you
    >>>>> should make a virtual GetData method in the base class and override

    it
    >>>>> in
    >>>>> the derived classes.
    >>>>>
    >>>>> --
    >>>>> Eliyahu Goldin,
    >>>>> Software Developer
    >>>>> Microsoft MVP [ASP.NET]
    >>>>> http://msmvps.com/blogs/egoldin
    >>>>> http://usableasp.net
    >>>>>
    >>>>>
    >>>>> "Goran Djuranovic" <> wrote in

    >message
    >>>>> news:%...
    >>>>>> Hi all,
    >>>>>> Is there a way to retrieve a derived class name inside a subroutine

    >or
    >>>>>> a
    >>>>>> function of the base class? I am trying to get some data from the
    >>>>>> database, based on which derived class is calling the subroutine. I
    >>>>>> know
    >>>>>> the base class name can be retrieved by using reflection namespace:
    >>>>>> System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >>>>>>
    >>>>>> Thanks
    >>>>>> Goran
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>>
    >>>

    >>
    >>
    >>

    >
    >
     
    Steven Cheng[MSFT], Sep 14, 2007
    #8
  9. Hi Steven,
    For some reason Me.Page.GetType() returns "ASP.Login_aspx", but using
    BaseType as Me.GetType.BaseType.FullName returns what I want. Again, thanks
    for your help and consider this thread closed.

    Goran

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Thanks for your reply Goran,
    >
    > Yes, my code example only return the page's title, however, it acually
    > demonstrate that you can get the underlying running page's class through
    >
    > this.Page.GetType()
    >
    > You can print out this value. This class should be a dynamic generated
    > type(by ASP.NET runtime). You can use its "BaseType" to get the further
    > parent type. Thus, you can get all the classes in the page's inheritance
    > chain. How do you think?
    >
    > Sincerely,
    >
    > Steven Cheng
    >
    > Microsoft MSDN Online Support Lead
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    > --------------------
    >>Subject: Re: Retrieving derived class name inside base class
    >>Date: Tue, 11 Sep 2007 09:58:14 -0400

    >
    >>Hi Steven,
    >>Your code returns the name of the .aspx page which is OK, but I need the
    >>name of the .vb class behind it. So, for example, the ASPX page name in

    > this
    >>case is "Login.aspx" and the code-behind class name is "Login". What I

    > need
    >>returned is "Login", and not "Login.aspx".
    >>
    >>Also, "Login" class inherits from the base class.
    >>
    >>Thanks for your help guys.
    >>Goran
    >>
    >>"Steven Cheng[MSFT]" <> wrote in message
    >>news:...
    >>> Thanks for Eliyahu's input.
    >>>
    >>> Hi Goran,
    >>>
    >>> As Eliyahu has suggested, to getting the base type from OO and

    > inheritance
    >>> perspective is not quite realistic and recommended. However, based on
    >>> my
    >>> understanding on your scenario, what you want to do is actually get some
    >>> information in the base class so as to determine which concrete child

    > page
    >>> is the current running one, correct?
    >>>
    >>> I've performed some research and the following ways should be some
    >>> possible
    >>> means:
    >>>
    >>> ** Since the base class is Page(which also derived from Control class),
    >>> you
    >>> can use the "this.Page" property to get the current running page class

    > and
    >>> reflect its type.
    >>>
    >>>
    >>> ** Or I think it is better to use some control or markup on the page to
    >>> detect the current page since you can use "this.Page.Controls" to get
    >>> the
    >>> running page's Control collection. For example, this.Page.Title can give
    >>> you the page title.
    >>>
    >>> Here is a test base page class I used for test and it works well:
    >>>
    >>>>>>>>>>sample base page >>>>>>
    >>> public class BasePageClass : Page
    >>> {
    >>>
    >>> public string PageTypeName
    >>> {
    >>> get
    >>> {
    >>> return this.Page.GetType().FullName;
    >>> }
    >>> }
    >>>
    >>> public string PageTitle
    >>> {
    >>> get
    >>> {
    >>> return this.Page.Title;
    >>> }
    >>> }
    >>> }
    >>> <<<<<<<<<<<<<<<<<<<<<<
    >>>
    >>> Hope this helps.
    >>>
    >>> Sincerely,
    >>>
    >>> Steven Cheng
    >>>
    >>> Microsoft MSDN Online Support Lead
    >>>
    >>>
    >>>
    >>> ==================================================
    >>>
    >>> Get notification to my posts through email? Please refer to
    >>>

    > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
    >>> ications.
    >>>
    >>>
    >>>
    >>> Note: The MSDN Managed Newsgroup support offering is for non-urgent

    > issues
    >>> where an initial response from the community or a Microsoft Support
    >>> Engineer within 1 business day is acceptable. Please note that each

    > follow
    >>> up response may take approximately 2 business days as the support
    >>> professional working with you may need further investigation to reach
    >>> the
    >>> most efficient resolution. The offering is not appropriate for
    >>> situations
    >>> that require urgent, real-time or phone-based interactions or complex
    >>> project analysis and dump analysis issues. Issues of this nature are
    >>> best
    >>> handled working with a dedicated Microsoft Support Engineer by
    >>> contacting
    >>> Microsoft Customer Support Services (CSS) at
    >>> http://msdn.microsoft.com/subscriptions/support/default.aspx.
    >>>
    >>> ==================================================
    >>>
    >>>
    >>> This posting is provided "AS IS" with no warranties, and confers no
    >>> rights.
    >>>
    >>>
    >>>
    >>>
    >>> --------------------
    >>>>From: "Eliyahu Goldin" <>
    >>>>References: <#>
    >>> <>
    >>> <ejI$>
    >>>>Subject: Re: Retrieving derived class name inside base class
    >>>>Date: Tue, 11 Sep 2007 00:36:35 +0200
    >>>
    >>>>
    >>>>You can always call GetType() method, can't you?
    >>>>
    >>>>--
    >>>>Eliyahu Goldin,
    >>>>Software Developer & Consultant
    >>>>Microsoft MVP [ASP.NET]
    >>>>http://msmvps.com/blogs/egoldin
    >>>>
    >>>>
    >>>>"Goran Djuranovic" <> wrote in message
    >>>>news:ejI$...
    >>>>Eliyahu, thanks, but I would not like to have to type anything on the
    >>>>derived classes (not even override methods), if possible.
    >>>>
    >>>>Maybe I should explain the problem in detail. What I am trying to do is
    >>>>re-use a menu control and its methods on 10 different web pages with as
    >>> less
    >>>>coding as possible. So, here is how I planned to do it:
    >>>>- have every .aspx page contain a menu control (nothing declared in the
    >>>>code-behind, because it would conflict with the base class)
    >>>>- have a base class contain all the methods for menu control (plus base
    >>>>class inherits from System.Web.UI.Page)
    >>>>- have every page inherit from a base class
    >>>>- every page has a subset of menu items, which is filterred based on
    >>>>what
    >>>>the page (class name) is, and this is where the problem is, because I
    >>> cannot
    >>>>retrieve the name of the derived class from within the base class.
    >>>>
    >>>>Wish I could use multiple inheritance (without having to use the
    >>>>interfaces).
    >>>>
    >>>>Thanks
    >>>>Goran
    >>>>
    >>>>"Eliyahu Goldin" <> wrote in
    >>>>message news:...
    >>>>> It would be very much against the whole idea of inheritance. Instead

    > you
    >>>>> should make a virtual GetData method in the base class and override it
    >>>>> in
    >>>>> the derived classes.
    >>>>>
    >>>>> --
    >>>>> Eliyahu Goldin,
    >>>>> Software Developer
    >>>>> Microsoft MVP [ASP.NET]
    >>>>> http://msmvps.com/blogs/egoldin
    >>>>> http://usableasp.net
    >>>>>
    >>>>>
    >>>>> "Goran Djuranovic" <> wrote in

    > message
    >>>>> news:%...
    >>>>>> Hi all,
    >>>>>> Is there a way to retrieve a derived class name inside a subroutine

    > or
    >>>>>> a
    >>>>>> function of the base class? I am trying to get some data from the
    >>>>>> database, based on which derived class is calling the subroutine. I
    >>>>>> know
    >>>>>> the base class name can be retrieved by using reflection namespace:
    >>>>>> System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name.
    >>>>>>
    >>>>>> Thanks
    >>>>>> Goran
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>>
    >>>

    >>
    >>
    >>

    >
     
    Goran Djuranovic, Sep 17, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    571
    qazmlp
    Apr 10, 2005
  2. Replies:
    4
    Views:
    414
    Alf P. Steinbach
    May 23, 2007
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. Replies:
    2
    Views:
    716
Loading...

Share This Page