Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6163449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:49:37+00:00 2026-05-23T21:49:37+00:00

Example: namespace MyProgram.Testing { public class Test1 { public void TestMethod() { String actualType

  • 0

Example:

namespace MyProgram.Testing
{

    public class Test1
    {
        public void TestMethod()
        {
            String actualType = this.GetType().FullName.ToString();
            return;
        }

        public static String GetInheritedClassName()
        {
            return System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName;
        }

    }

    public class Test2 : Test1
    {
    }

    public class Test3
    {
       String test2ClassName = Test2.GetInheritedClassName();
    }
}

Anyway, I want it to return “MyProgram.Testing.Test2” but instead Test2.GetInheritedClassName() returns “MyProgram.Testing.Test1”. What do I have to put into that static class to get it to return that (if possible)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:49:37+00:00Added an answer on May 23, 2026 at 9:49 pm

    The code that’s printing out the type is the base-class method. Except for rare Reflection scenarios such as you provide above, execution wouldn’t be affected by whether the method is called using the derived type or the base type, so the system makes no distinction.

    You could, however, get around this by defining a generic base type:

        class ClassNameTesterBase<T>where T:ClassNameTester<T>
        {
            public static String getName() { return (typeof(T)).Name; }
        }
    

    and then defining the other types of interest:

        class ClassNameTester1<T&gt : ClassNameTesterBase<T> ...
        class ClassNameTester2<T&gt : ClassNameTester1<T> ...
    

    One may then if desired define leaf classes:

        class ClassNameTester1 : ClassNameTester1<ClassNameTester1> { }
        class ClassNameTester2 : ClassNameTester2<ClassNameTester2> { }
    

    One slight caveat here is that ClassNameTester2 derives its innards from from ClassNameTester1<T> but is not substitutable for anything having to do with the ClassNameTester1<ClassNameTester1>; if it’s being used as a static class, though, that shouldn’t be a problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I've got the following program: namespace ReflectionTest { public class Example { private
Say I do this (a contrived example): #include <iostream> #include <fstream> using namespace std;
Taking this article on classes and structs as an example: http://msdn.microsoft.com/en-us/library/ms173109.aspx namespace ProgrammingGuide {
I have something like this: public void Delete(T entity) { Context.DeleteObject(entity); Context.SaveChanges(); } I
Is it acceptable to add types to the std namespace. For example, I want
The example here doesn't make sense, but this is basically how I wrote my
I have this program in c++: #include <iostream> using namespace std; int main() {
My code is simply as this: UPDATED : #include <iostream> #include <fstream> using namespace
Are there some principles of organizing classes into namespaces? For example is it OK
Consider the following example. It consists of two header files, declaring two different namespaces:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.