[英]How to get the name of the class without instantiating object or having a static method?
I hate to see the name of the class used as a string parameter like "FileDownloader" in the code, and I would like to use something like this FileDownloader.Name(), where FileDownloader is name of the class. 我讨厌在代码中看到用作字符串参数的类的名称,如“FileDownloader”,我想使用类似FileDownloader.Name()的东西,其中FileDownloader是类的名称。
Only problem is that I can't find out how to do that without instantiating object or creating a static method... 唯一的问题是我无法在不实例化对象或创建静态方法的情况下找到如何做到这一点......
Is there a way to get a class name in .net without having the object instance and without creating a static method that returns the name of the class? 有没有办法在.net中获取类名而不使用对象实例并且不创建返回类名称的静态方法?
当然:
var name = typeof(FileDownloader).Name;
使用typeof运算符:
typeof ( FileDownloader).Name
Try typeof(YourClass).name. 尝试typeof(YourClass).name。 This should expose the name of your class 这应该公开您的类的名称
In VB.Net: 在VB.Net中:
Dim sf As StackFrame = New StackFrame()
Dim mb As MethodBase = sf.GetMethod()
Dim className = mb.DeclaringType.Name
There you go. 你去吧 Totally dynamic. 完全动态。 Just be careful about refactoring... If you move it to another class, the class name will change... :) 注意重构......如果你把它移到另一个类,类名会改变...... :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.