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

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

我正在尝试实现使用反射进行数据转换 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:43:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   反射   属性

发布评论

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

>www.elefans.com

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