4

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...

Nicolás
  • 7,423
  • 33
  • 35
Jronny
  • 2,164
  • 4
  • 30
  • 41

4 Answers4

15

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

Joe
  • 122,218
  • 32
  • 205
  • 338
4

Use Type.Name like this:

using System;

class Test
{
    static void Main()
    {
        Console.WriteLine(typeof(Test).Name);
    }
}

or like this (if you are getting the type via Object.GetType):

using System;

class Foo { }

class Test
{
    static void Main()
    {
        Foo foo = new Foo();
        Console.WriteLine(foo.GetType().Name);
    }
}
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • well, the problem really with GetType() as I have mentioned, is that it includes the namespace and changes the everything to lowercase. – Jronny Dec 10 '09 at 14:40
  • Just curious, are you working with ASP.NET website and a custom control? `Type.Name` doesn't change the case of type's name but ASP.NET does type name mangling for custom pages and user controls that may be causing what you are seeing. – Andrew Hare Dec 10 '09 at 14:43
  • Unfortunately then the type name you are seeing the actual type name. ASP.NET mangles the names of custom controls and pages to the format you are seeing - that really is the name of your type :) – Andrew Hare Dec 10 '09 at 14:48
  • Why is it designed that way? Are there no other way where we could get the exact class name as declared? =) – Jronny Dec 10 '09 at 14:54
1

Use GetType() and Name

Console.Writeline(child.GetType().Name);

or

Console.Writeline(child.GetType().FullName);
Murph
  • 9,985
  • 2
  • 26
  • 41
C. Ross
  • 31,137
  • 42
  • 147
  • 238
  • neither could be used since I have already mentioned in my question that using GetType() includes the namespace. – Jronny Dec 10 '09 at 14:42
  • The Type.Name property will not contain the namespace. Type.FullName will contain the class name. – C. Ross Dec 10 '09 at 15:07
  • My bad, really... I have not mentioned earlier that I was using ASP.Net UserControls – Jronny Dec 10 '09 at 15:10
1

Or you can use reflection to get base type of your variable

control.GetType().BaseType.Name

If you need I can provide deep description why it is so.

If you want you can try this code

System.IO.Path.GetFileNameWithoutExtension(((UserControl)control).AppRelativeVirtualPath)

This will give you the name you want, but this is not the type of the UserControl but it's file name.

Mike
  • 519
  • 4
  • 9