使用反射获取 RuntimeMethodInfo 对象的 ReturnParameter 的 Name 属性 (C#)
假设我在 C# 中有以下类:
public class B : A
{
public Int32 B_ID;
public String B_Value;
public Int32 getID()
{
return B_ID;
}
public void setID(Int32 value)
{
B_ID = value;
}
}
基于反射,我可以获得 getID() (和/或)setID() 方法使用的字段的名称吗? (如果是[B_ID]) 我正在编写一个持久性框架,识别表的键名会很有用,上面的两种方法都包含该表的键名。
看来 RuntimeMethodInfo 的 ReturnParameter 属性有一个名为 Name 的属性应该可以帮助我解决此问题,但它为 null。
为了获取 RuntimeMethodInfo 对象,我使用此 BindingFlags 枚举获取 B 类实例的成员:
- BindingFlags.Public | BindingFlags.Instance | BindingFlags.Instance | BindingFlags.Instance BindingFlags.DeclaredOnly
如何获取此字段名称?此行为应该与属性相同。
提前致谢
Suppose i have the following class in C#:
public class B : A
{
public Int32 B_ID;
public String B_Value;
public Int32 getID()
{
return B_ID;
}
public void setID(Int32 value)
{
B_ID = value;
}
}
Based on Reflection, can I get the name of the field used by getID() (and/or) setID() method? (in case, [B_ID])
I'm coding a persistence framework and it would be useful to identify the key name of a table, which is enclosed by both methods above.
It seems that ReturnParameter property of RuntimeMethodInfo has a property called Name that should help me with this, but it's comming null.
To get that RuntimeMethodInfo object, i'm getting Members of an instance of B class using this BindingFlags enums:
- BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly
How can I get this field name? This behavior should be the same with properties.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕这是不可能的,因为字段名称是实现代码的一部分,而反射不知道如何检索它。
持久性框架通常使用一种映射来提供此类信息。例如,您可以使用 xml 文件,或者可以在字段上使用属性将它们引入为表的键或列,如下所示:
I am afraid that's impossible because the field name is the part of the implemented code and reflection has no clue how to retrieve it.
Persistent frameworks usually use a kind of mapping to provide such information.For example you can use a xml file or you can use attirbutes over your fields to introduce them as key or columns of your table something like this :