关于在 C# (.NET 2.0) 中运行时读取类属性的问题?
假设我有一个具有 3 个属性的类。直到运行时我才知道要读取哪个属性。有没有办法在没有 switch/if-else 语句的情况下做到这一点?
如果将 DataTable 视为一个类,则可以动态处理列,这可以工作,但我无法将我的类更改为 DataTable (长话短说)。
对于 DataTable,代码将是:
string columnName = "Age";
int age = dataRow[columnName];
这在类中怎么可能?
string propertyName = "Baka";
int age = item.getProperty(propertyName) // ???
Let's say I have a class that has 3 properties. I don't know which property I want to read until runtime. Is there a way to do this without a switch/if-else statement?
If a DataTable is treated as a class, then the columns can be treated dynamically which would work, but I'm not able to change my class into a DataTable (long story).
For a DataTable, the code would be:
string columnName = "Age";
int age = dataRow[columnName];
How is this possible in a class?
string propertyName = "Baka";
int age = item.getProperty(propertyName) // ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
进行这种运行时评估的最简单方法是使用 Reflection。
根据您使用的 .NET 版本,您可以向类添加方法或使用扩展方法。
The easiest way to do this kind of runtime evaluation is with Reflection.
Depending on the version of .NET you're using, you could add methods to your class or use extension methods.
编辑:我刚刚阅读了您关于不使用开关的评论,但无论如何我都会将其保留在这里,以防万一......跳到我的第二个答案。
也许是这样的:
我在遗传学方面不太强,所以也许有人可以帮助我回归演员阵容。
或者,您可以向 DataRow 类型的类添加一个字段,并使用索引器来访问它:
用法:
EDIT: I just read your comment about not using a switch, but I'll keep this here anyway, just in case... skip to my second answer.
Maybe something like this:
I'm not too strong in Genetics, so maybe someone can help me with the return cast.
Alternatively, you could add a field to your class of type DataRow and use an indexer to access it:
Usage:
之前回答过类似的问题。这是一个代码示例,并链接到另一个问题。
隐藏公共函数
您最感兴趣的是GetValue System.Reflection.PropertyInfo 类的方法。
C# 代码示例
VB 代码示例
Answered a similar question earlier. Here's a code sample, and link to the other question.
Hiding Public Functions
You're going to be most interested in the GetValue method of the System.Reflection.PropertyInfo class.
C# Code Sample
VB Code Sample
如果您知道对象的类型,则可以使用反射获取所有属性,然后获取您请求的属性。
每个 PropertyInfo 都有一个名称,您可以将其与您的字符串相匹配。
If you know the type of object you can get all the properties, and then your requested property, using reflection.
Each PropertyInfo has a Name which you can match up to your String.
item.GetProperties() 将返回一个包含所有属性的 PropertyInfo[](我认为仅是公共的)
PropertyInfo 对象有一个属性名称,它是属性的名称,因此您可以通过搜索迭代搜索 PropertyInfo 对象的数组姓名。
item.GetProperties() would return a PropertyInfo[] containing all properties (public only I think)
A PropertyInfo Object has a property Name which is the name of the property, so you can iterate over the array searching for a PropertyInfo object with your search name.
使用索引器的示例:
用法:
Example using indexers:
Usage: