469,656 Members | 1,826 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,656 developers. It's quick & easy.

Setting value to a read-only property

Hi,

Is it possible to set value to a read-only property via reflection?

Thx

--
Bartol
--
Remove "TB" from address to reply
Nov 21 '05 #1
5 2221

"Tomislav Bartolin" <to*****************@zg.htnet.hr> wrote in message
news:cn**********@brown.net4u.hr...
Is it possible to set value to a read-only property via reflection?


I apologize for a poor explanation of my question... what I really wanted to
ask is if there was
a way to tell reflection that a read-only property maps to some private
field within that class, so that
I could set the "property value" with reflection, but not allow the user of
my class to do the same from code.

Thx

--
Bartol
--
Remove "TB" from address to reply

Nov 21 '05 #2
"Tomislav Bartolin" <to*****************@zg.htnet.hr> schrieb:
Is it possible to set value to a read-only property via reflection?


*Why?*

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #3
ask is if there was
a way to tell reflection that a read-only property maps to some private
field within that class,
No

so that
I could set the "property value" with reflection, but not allow the user of
my class to do the same from code.


Why do you want to do it with reflection? Couldn't you just provide an
internal method to set the value?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #4
I'm not sure what you want exactly but I believe you can set a property to
readonly in the class definition then set the value from outside the class
using the following code I copied from an earlier posting by someone:

Public Class Testclass
Private Field As Integer = 1
Private Property TestProp() As Integer
Get
Return Field
End Get
Set(ByVal Value As Integer)
Field = Value
End Set
End Property
End Class

Private Sub TestMethod()
Dim O As New Testclass
O.GetType.GetProperty("TestProp", Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.NonPublic).SetValue(O, 3, Nothing)
End Sub
"Tomislav Bartolin" wrote:
Hi,

Is it possible to set value to a read-only property via reflection?

Thx

--
Bartol
--
Remove "TB" from address to reply

Nov 21 '05 #5

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
ask is if there was
a way to tell reflection that a read-only property maps to some private
field within that class,


No

so that
I could set the "property value" with reflection, but not allow the user ofmy class to do the same from code.


Why do you want to do it with reflection? Couldn't you just provide an
internal method to set the value?


Hi Mattias,

Thanks for your reply.

I can't just use an internal method because I do not know what properties
will the class contain.
The user developer creates the class and my O/R tool fills its properties
with data from database fields based on a XML mapping file (via reflection).
Along with the mappings to real database fields, the mappings can include
"virtual" fields which therefore cannot be updated, so it seems logical not
to allow the developer to modify its associated property.
I have solved this design demand by requiring that a private field with the
same name as the read-only property exists, so that it could be filled with
data.

Regards

--
Bartol
--
Remove "TB" from address to reply

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by bagsmode | last post: by
2 posts views Thread by J Krugman | last post: by
12 posts views Thread by daniel kaplan | last post: by
6 posts views Thread by Niyazi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.