使用反射从字符串中获取属性值

编程入门 行业动态 更新时间:2024-10-25 04:19:25
本文介绍了使用反射从字符串中获取属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试实施使用反射的数据转换1 示例在我的代码中.

I am trying implement the Data transformation using Reflection1 example in my code.

GetSourceValue 函数有一个比较各种类型的开关,但我想删除这些类型和属性,并让 GetSourceValue 仅使用单个字符串获取属性值作为参数.我想在字符串中传递一个类和属性并解析该属性的值.

The GetSourceValue function has a switch comparing various types, but I want to remove these types and properties and have GetSourceValue get the value of the property using only a single string as the parameter. I want to pass a class and property in the string and resolve the value of the property.

这可能吗?

1 原始博客文章的网络存档版本

推荐答案

public static object GetPropValue(object src, string propName) { return src.GetType().GetProperty(propName).GetValue(src, null); }

当然,您会想要添加验证和诸如此类的东西,但这就是它的要点.

Of course, you will want to add validation and whatnot, but that is the gist of it.

更多推荐

使用反射从字符串中获取属性值

本文发布于:2023-11-12 11:45:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581413.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   反射   属性

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!