0

Solution Explorer
Solution Explorer

I can get current method name, but currently I can't get really current class name.
From reference: C# getting its own class name, accepted answer is this.GetType().Name.

Test.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string currentMethodName = MethodBase.GetCurrentMethod().Name; // "Page_Load"
            string currentClassName1 = this.GetType().Name; // "test_test_aspx" // How to get "Test" only?
            string currentClassName2 = this.GetType().FullName; // "ASP.test_test_aspx" // How to get "Test" only?
        }
    }
}
akkapolk
  • 554
  • 8
  • 33
  • @user1672994, it's working, but it's not common code. For example: I must use `nameof(AnotherClass)` in AnotherClass. – akkapolk Nov 22 '19 at 05:08

1 Answers1

1

Try with this:

  string className = MethodBase.GetCurrentMethod().DeclaringType.Name;
Angel Cantu
  • 141
  • 6