English 中文(简体)
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...

最佳回答

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

问题回答

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);
    }
}

Use GetType() and Name

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

or

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

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签