如何将asp.net用户控件的当前类名转换为c#上的字符串?

发布于 2024-08-15 04:27:02 字数 436 浏览 3 评论 0原文

所以我尝试过 GetType() 但由于某种原因,它确实包含命名空间......
C# 没有指定类名称的属性吗?
例如:

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

public class Child : Parent {
}

我尝试使用名称为硬编码的字符串属性创建 Child,但前提是我们能够找到更好的解决方法...也许是反射或表达式...

提前致谢 = )

编辑:顺便说一句,我正在研究用户控件......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜声 2024-08-22 04:27:02

如果您使用的是 ASP.NET Web 应用程序项目,则通常会使用代码隐藏模型。

ASP.NET 将从您的 aspx/ascx 文件动态生成并编译一个类,该文件使用您在代码隐藏文件中定义的类作为基类。

this.GetType().Namethis.GetType().FullName 将返回 ASP.NET 生成的自动生成类的名称。此自动生成的类将继承您在代码隐藏文件中定义的 UserControl/WebPage 类(<%@Control ...> 中的 Inherits 关键字) ascx 文件的标记/aspx 文件的 <%@Page ...> 标记)。

如果您想要代码隐藏类的名称,请使用:

this.GetType().BaseType.Namethis.GetType().BaseType.FullName

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

ゝ偶尔ゞ 2024-08-22 04:27:02

像这样使用 Type.Name

using System;

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

或像这样(如果您通过 Object.GetType 获取类型):

using System;

class Foo { }

class Test
{
    static void Main()
    {
        Foo foo = new Foo();
        Console.WriteLine(foo.GetType().Name);
    }
}

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);
    }
}
风透绣罗衣 2024-08-22 04:27:02

使用 GetType()Name

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

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

Use GetType() and Name

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

or

Console.Writeline(child.GetType().FullName);
北城半夏 2024-08-22 04:27:02

或者您可以使用反射来获取变量的基类型

control.GetType().BaseType.Name

如果您需要的话我可以提供深入的描述为什么会这样。

如果您愿意,可以尝试此代码,

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

这将为您提供所需的名称,但这不是 UserControl 的类型,而是文件名。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文