我可以在静态方法或属性中获取类名 [英] Can I get the class name in a static method or property

查看:74
本文介绍了我可以在静态方法或属性中获取类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我有一个对象,我知道我可以获得该类的名称但是我可以在以下场景中获得类名:


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 data
from 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 could
be calculated from the class name.

So, I will be able to write

MyStoredData.GetData(ChildClass.ClassName) and it will return all the records in the xml of type ChildClass and MyStoredData
encapsulates the reading of data and GetData is a static method
It is not important whether I get the class name from a method or a property.

Any ideas welcome. Hope it makes sense.

Steve

推荐答案

我能建议的最简洁的答案是泛型 - 有GetData< ; T>(),所以

你调用GetData< ChildClass>(),然后在GetData< T>()里面你可以使用

typeof(T).Name,或者从typeof(T)访问属性元数据,例如

XmlTypeAttribute。

Marc
The cleanest answer I can suggest is generics - have GetData<T>(), so
you call GetData<ChildClass>(), then inside GetData<T>() you can use
typeof(T).Name, or access attribute metadata from typeof(T) such as
XmlTypeAttribute.

Marc


史蒂夫< s _ ****** @ yahoo.comwrote:
steve <s_******@yahoo.comwrote:

>

如果我有一个对象,我知道我可以得到类的名称

但是我可以在以下场景中获得类名:


public class ParentClass

{

公共静态字符串ClassName

{

get

{

return" ParentClass" ;

}


}

}


公共类ChildClass:ParentClass

{< br $>

}


因此ChildClass.ClassName返回ChildClass

和ParentClass.ClassName返回ParentClass ;
>
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"



不,你不能这样做。实际上,如果你看一下生成的代码

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


它看起来似乎是一个有点奇怪,但我真的想用它来返回一个

令牌,这个令牌可以用来从

一个xml文件读取数据的类名,它包含一个列表这些课程。


我希望能够根据知道班级

名称进行阅读,而不必在每个班级中存储静态数据这可能是从班级名称计算的



所以,我将能够写出


MyStoredData .GetData(ChildClass.ClassName),它将返回类型为ChildClass的xml中的所有

记录,并且MyStoredData封装

读取数据,GetData是静态方法


从方法或

属性获取类名是不重要的。


任何想法欢迎。希望它有意义。
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 data from
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 could be
calculated from the class name.

So, I will be able to write

MyStoredData.GetData(ChildClass.ClassName) and it will return all the
records in the xml of type ChildClass and MyStoredData encapsulates
the reading of data and GetData is a static method

It is not important whether I get the class name from a method or a
property.

Any ideas welcome. Hope it makes sense.



您可以改为使用泛型吗?


MyStoredData.GetData< ChildClass>(...)





-

Jon Skeet - < sk *** @ pobox.com>

网站: http://www.pobox.com/~skeet

博客: http://www.msmvps。 com / jon.skeet

C#深度: http:// csharpindepth .com

Could you change to use generics instead?

MyStoredData.GetData<ChildClass>(...)

?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com


2008年9月2日星期二13:33:25 -0700,steve< s _ ****** @ yahoo.comwrote:
On Tue, 02 Sep 2008 13:33:25 -0700, steve <s_******@yahoo.comwrote:

[...]

因此ChildClass.ClassName返回ChildClass和$ / b $ B $ ParentClass.ClassName返回" ParentClass"
[...]
So that ChildClass.ClassName returns "ChildClass" and
ParentClass.ClassName returns "ParentClass"



因为你不能在没有

知道班级名称的情况下调用静态成员(方法或属性),这对你有什么好处?怎么用

写这个:

MyStoredData.GetData(ChildClass.ClassName);


比这更好:


MyStoredData.GetData(" ChildClass");


甚至:


MyStoredData .GetData(typeof(ChildClass).GetName());





更一般地说,虽然这不是你的具体问题,.NET已经

内置了序列化支持,包括能够将XML文件序列化为
。这听起来有点像你可能重新发明轮子

这里。


Pete

Since you cannot call the static member (method or property) without
knowing the class name already, how would this benefit you? How is
writing 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 already
has serialization support built in, including being able to serialize to
an XML file. It sounds a little like you might be reinventing the wheel
here.

Pete


这篇关于我可以在静态方法或属性中获取类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆