如何判断反思性如果一个属性有一个公共的二传手

编程入门 行业动态 更新时间:2024-10-14 04:26:35
本文介绍了如何判断反思性如果一个属性有一个公共的二传手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

林节省了一个对象模型到XML,但是当我在加载它回来,我在尝试使用PropertyInfo.SetValue(),因为酒店没有一个二传手只是一个getter时例外。

我想要么不救出来,只有有干将,或找出负载其是否有效,我尝试设置值与否的属性。

任何人都知道如何做到这一点。

干杯

解决方案

您可以使用 PropertyInfo.GetSetMethod - 这将返回空如果有一个属性是只读或二传手是非公

如果(property.GetSetMethod()!= NULL) { //是的,你可以写信给它。 }

如果你可以用一个非公开的setter应付,你可以使用:

如果(property.GetSetMethod(真)!= NULL) { //是啊,有一个二传手 - 但它可能是私人}

Im saving an object model out to XML but when i load it back in I get exceptions when trying to use PropertyInfo.SetValue() because the property doesn't have a setter just a getter.

I want to either not save out the properties that only have getters or figure out on load whether its valid for me to try and set a value or not.

Anybody know how to do this

Cheers

解决方案

You can use PropertyInfo.GetSetMethod - this will return null if either the property is read-only or the setter is non-public.

if (property.GetSetMethod() != null) { // Yup, you can write to it. }

If you can cope with a non-public setter, you can use:

if (property.GetSetMethod(true) != null) { // Yup, there's a setter - but it may be private }

更多推荐

如何判断反思性如果一个属性有一个公共的二传手

本文发布于:2023-06-08 21:32:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/589426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有一个   如何判断   属性   二传

发布评论

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

>www.elefans.com

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