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.
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
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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.