本文介绍了我可以在静态方法或属性中获取类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我有一个对象,我知道我可以获得该类的名称但是我可以在以下场景中获得类名: public class ParentClass { public static string ClassName { get { 返回" ParentClass" } } } 公共类ChildClass:ParentClass { } 因此ChildClass.ClassName返回" ChildClass"和ParentClass.ClassName返回ParentClass 看起来有点奇怪但我真的想用它来返回一个基于可用于读取数据的类名的令牌来自xml文件的并且包含这些类的列表。 我希望能够只需知道类名和不必在每个类中存储静态数据,这可以从类名计算出来。 所以,我将能够写出 MyStoredData.GetData(ChildClass.ClassName),它将返回类型为ChildClass和MyStoredData的xml中的所有记录 封装了数据读取,GetData是一个静态方法 我是否从方法或财产中获取类名并不重要。 欢迎任何想法。希望它有意义。 史蒂夫If I have an object I know I can get the name of the class but can I get the class name in the following scenario :public class ParentClass{public static string ClassName{get{return "ParentClass"}}}public class ChildClass : ParentClass{}So that ChildClass.ClassName returns "ChildClass" and ParentClass.ClassName returns "ParentClass"It may seem a bit strange but I really want to use it to return a token based on the class name that can be used for reading datafrom an xml file and that contains a list of these classes.I would like to be able to base the read on just knowing the class name and not have to store static data in each class which couldbe calculated from the class name.So, I will be able to writeMyStoredData.GetData(ChildClass.ClassName) and it will return all the records in the xml of type ChildClass and MyStoredDataencapsulates the reading of data and GetData is a static methodIt is not important whether I get the class name from a method or a property.Any ideas welcome. Hope it makes sense.Steve推荐答案 不,你不能这样做。实际上,如果你看一下生成的代码 " ChildClass.ClassName"你会看到它实际上是*打电话给 " ParentClass.ClassName"。No, you can''t do this. In fact, if you look at the generated code for"ChildClass.ClassName" you''ll see it''s *actually* a call to"ParentClass.ClassName". 您可以改为使用泛型吗? MyStoredData.GetData< ChildClass>(...) ? - Jon Skeet - < sk *** @ pobox.com> 网站: http://www.pobox.com/~skeet 博客: http://www.msmvps。 com / jon.skeet C#深度: http:// csharpindepth .comCould you change to use generics instead?MyStoredData.GetData<ChildClass>(...)?--Jon Skeet - <sk***@pobox.com>Web site: http://www.pobox.com/~skeetBlog: http://www.msmvps.com/jon.skeetC# in Depth: http://csharpindepth.com 因为你不能在没有 知道班级名称的情况下调用静态成员(方法或属性),这对你有什么好处?怎么用 写这个: MyStoredData.GetData(ChildClass.ClassName); 比这更好: MyStoredData.GetData(" ChildClass"); 甚至: MyStoredData .GetData(typeof(ChildClass).GetName()); ? 更一般地说,虽然这不是你的具体问题,.NET已经 内置了序列化支持,包括能够将XML文件序列化为。这听起来有点像你可能重新发明轮子 这里。 PeteSince you cannot call the static member (method or property) withoutknowing the class name already, how would this benefit you? How iswriting this:MyStoredData.GetData(ChildClass.ClassName);better than this:MyStoredData.GetData("ChildClass");or even:MyStoredData.GetData(typeof(ChildClass).GetName()) ;?More generally, while it''s not your question specifically, .NET alreadyhas serialization support built in, including being able to serialize toan XML file. It sounds a little like you might be reinventing the wheelhere.Pete 这篇关于我可以在静态方法或属性中获取类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-15 05:37