views:

574

answers:

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

+4  A: 

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
well, the problem really with GetType() as I have mentioned, is that it includes the namespace and changes the everything to lowercase.
Jronny
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
yes, I forgot to mention that I am working with usercontrols...
Jronny
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
Why is it designed that way? Are there no other way where we could get the exact class name as declared? =)
Jronny
+1  A: 

Use GetType() and Name

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

or

Console.Writeline(child.GetType().FullName);
C. Ross
neither could be used since I have already mentioned in my question that using GetType() includes the namespace.
Jronny
The Type.Name property will not contain the namespace. Type.FullName will contain the class name.
C. Ross
My bad, really... I have not mentioned earlier that I was using ASP.Net UserControls
Jronny
+9  A: 

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
I'll try this later back home.
Jronny
this one is perfect!
Jronny
A: 

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