473,491 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2364

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9157
by: bagsmode | last post by:
Hi, I'm trying to set a session cookie and then redirect, however I get the error: Status: 302 Moved Location: /index.cgi I thought I recall getting an error like this when I first tried...
2
2211
by: J Krugman | last post by:
I have a form with a couple of submit buttons, plus a "pseudolink" that is also supposed to submit the form; the submitted form data feeds to a CGI script. The two submit buttons have the name...
12
1937
by: daniel kaplan | last post by:
Hi All, Been learning Javascript (via google) to create forms with pre-set values. As seen below. My problem I have found is this: I can't seem to figure out (or find so far via google) how to...
0
1329
by: huzz | last post by:
Am trying to read user defined setting from web.config as shown in this tutorial http://aspnet.4guysfromrolla.com/articles/053102-1.aspx When i try to read the value i get error "Method name...
6
2714
by: Niyazi | last post by:
Hi, We have and IBM AS400 and I belive the reional setting is Turkish. The IBM Client-Access for Windows that install in our PC (WIN XP SP2) set to Turkish characters. Now my PC has English...
5
2143
by: Mark | last post by:
Hi All, This maybe a really simple question but I need some help. I have been having problems with security and thanks to the help received from a reply to an earlier post, I have found a...
1
1441
by: Harshal P | last post by:
Hello, I am settings hidden field value from the DayRender event hadler of the asp.net calendar control, when i try to read the value of hidden field from javascript, the value is empty string. If...
3
7888
by: Jeff | last post by:
ASP.NET 2.0 I'm wondering how to add a custom setting into web.config. From this website I'm doing a http request to another website, and I would like to have the webaddress of this other...
8
16083
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list...
7
1719
by: PetterL | last post by:
I have a setting called My.settings.firstrun set to True, set in the setting manager. When i read this in the first form form_Load in a IF sentence it always come out as false. I have tried to...
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7154
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
280
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.