如何设置的属性值里没有二传手 [英] How to set value of property where there is no setter

查看:252
本文介绍了如何设置的属性值里没有二传手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过的各种问题提出并回答了,我们可以调用使用反射私人二传手像这样的:

I have seen various questions raised and answered where we can invoke a private setter using reflection such as this one:

是否有可能通过反射来获取属性的私人二传手?

不过我有有一个属性我需要设置,但不能因为没有二传手一些代码,我不能添加setter,因为这不是我的代码。有没有办法在这种情况下使用反射莫名其妙定置了价值?

However I have some code which has a property i need to set but cant because there is no setter, I cant add a setter as this isn't my code. Is there a way to somehow set the value using reflection in this scenario?

推荐答案

您必须记住,一个属性只是语法糖一对的方法。一种方法(吸气)返回属性类型和一个方法(setter方法)的值接受属性类型的值。

You have to keep in mind that a property is just syntactic sugar for a pair of methods. One method (the getter) returns a value of the property type and one method (the setter) accepts a value of the property type.

有没有要求吸气剂和setter实际上获取或设置任何东西。他们只是方法,使他们可以做任何。唯一的要求是,所述吸气剂返回一个值。从外面看有没有办法,你真的可以告诉,如果有一个支持字段。吸气剂可以得到每一个计算,它被称为时间。它可以基于其他属性。

There is no requirement that the getter and setter actually get or set anything. They're just methods, so they're allowed to do anything. The only requirement is that the getter return a value. From the outside there's no way you can really tell if there is a backing field. The getter could be getting computed every time it's called. It may be based on other properties.

所以,没有,没有真正的一般任何方式设定不具有一个设置器的属性。

So, no, there isn't really any way in general to "set" a property that doesn't have a setter.

这篇关于如何设置的属性值里没有二传手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆