如何将asp.net用户控件的当前类名转换为c#上的字符串?
所以我尝试过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 ASP.NET Web 应用程序项目,则通常会使用代码隐藏模型。
ASP.NET 将从您的 aspx/ascx 文件动态生成并编译一个类,该文件使用您在代码隐藏文件中定义的类作为基类。
this.GetType().Name
和this.GetType().FullName
将返回 ASP.NET 生成的自动生成类的名称。此自动生成的类将继承您在代码隐藏文件中定义的 UserControl/WebPage 类(<%@Control ...>
中的Inherits
关键字) ascx 文件的标记/aspx 文件的<%@Page ...>
标记)。如果您想要代码隐藏类的名称,请使用:
this.GetType().BaseType.Name
或this.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
andthis.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
orthis.GetType().BaseType.FullName
像这样使用
Type.Name
:或像这样(如果您通过
Object.GetType
获取类型):Use
Type.Name
like this:or like this (if you are getting the type via
Object.GetType
):使用
GetType()
和Name
或
Use
GetType()
andName
or
或者您可以使用反射来获取变量的基类型
如果您需要的话我可以提供深入的描述为什么会这样。
如果您愿意,可以尝试此代码,
这将为您提供所需的名称,但这不是 UserControl 的类型,而是文件名。
Or you can use reflection to get base type of your variable
If you need I can provide deep description why it is so.
If you want you can try this code
This will give you the name you want, but this is not the type of the UserControl but it's file name.