Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the current class name of asp.net usercontrols to string on c#?

So I have tried GetType() but for some reason, It does include the namespace...
Does C# not have a property for a class specifying its name?
For example:

public class Parent : System.Web.UI.UserControl {
    public someFunction(){
        Child child = new Child();
        Console.WriteLine(child.ThePropertyThatContainsTheName);
    }
}

public class Child : Parent {
}

I have tried to create the Child with a string property that has the name hard-coded, but only if we could find a better workaround to this... maybe reflection or expressions...

Thanks in advance =)

Edit: I am working on user controls by the way...

like image 399
Jronny Avatar asked Nov 30 '22 19:11

Jronny


1 Answers

If you are using an ASP.NET Web Application project, you will normally be using the code-behind model.

ASP.NET will dynamically generate and compile a class from your aspx/ascx file, which uses the class you defined in your code-behind file as a base class.

this.GetType().Name and this.GetType().FullName will return the name of the auto-generated class generated by ASP.NET. This auto-generated class will subclass the UserControl/WebPage class you have defined in your code-behind file (Inherits keyword in the <%@Control ...> tag of your ascx file / <%@Page ...> tag of your aspx file).

If you want the name of your code-behind class, use:

this.GetType().BaseType.Name or this.GetType().BaseType.FullName

like image 155
Joe Avatar answered Dec 15 '22 02:12

Joe