私はVB.NETでクラス継承を広範囲に使用するアプリケーションを構築しており、できるだけ多くのコードをデータベースアクションを含むベースクラスに入れています。私が抱えている問題は、親クラスから派生クラスのプロパティを設定する方法です。値を取得して、読み取り専用ではないプロパティの値を問題なく設定することはできますが、コードを通じて読み取り専用プロパティを設定したいが、ユーザーがこれらを変更することはできません(Modified date/Created date )。問題を示す簡単なコンソールアプリケーションをまとめました。子クラスreadonlyプロパティを親クラスから変更する
親クラス:
Public Class clsParent
Private _ParentInteger As Integer = 0
Public Property ParentInteger As Integer
Get
Return _ParentInteger
End Get
Set(value As Integer)
_ParentInteger = value
End Set
End Property
Public Sub PrintProperties()
Dim t As Type = Me.GetType
Console.WriteLine("Object type: '{0}'", t.ToString)
Dim propInfos As PropertyInfo() = t.GetProperties()
Console.WriteLine("The number of properties: {0}", propInfos.Length)
Console.WriteLine("------------------------------------------------------------")
For Each propInfo In propInfos
Dim readable As Boolean = propInfo.CanRead
Dim writable As Boolean = propInfo.CanWrite
Console.WriteLine(" Property name: {0}", propInfo.Name)
Console.WriteLine(" Property type: {0}", propInfo.PropertyType)
Console.WriteLine(" Read-Write: {0}", readable And writable)
Console.WriteLine(" Value: {0}", propInfo.GetValue(Me))
Console.WriteLine("------------------------------------------------------------")
Next
End Sub
Public Sub TryWriteProperties()
Dim t As Type = Me.GetType
Dim propInfos As PropertyInfo() = t.GetProperties()
For Each propInfo In propInfos
Console.WriteLine("------------------------------------------------------------")
Console.WriteLine(" {0}", propInfo.Name)
Try
Console.WriteLine(" Old Value: {0}", propInfo.GetValue(Me))
propInfo.SetValue(Me, CInt(propInfo.GetValue(Me)) + 100)
Console.WriteLine(" New Value: {0}", propInfo.GetValue(Me))
Catch ex As Exception
Console.WriteLine(" Failed to set new value: {0}", ex.Message)
End Try
Next
End Sub
End Class
と子クラス。
Public Class clsChild
Inherits clsParent
Dim _ChildWritableInteger As Integer = 5
Public Property ChildWritableInteger As Integer
Get
Return _ChildWritableInteger
End Get
Set(value As Integer)
_ChildWritableInteger = value
End Set
End Property
Dim _ChildReadOnlyInteger As Integer = 10
Public ReadOnly Property ChildReadOnlyInteger As Integer
Get
Return _ChildReadOnlyInteger
End Get
End Property
End Class
出力を表示する単純なサブ。
Sub Main()
Dim x As New clsChild
x.PrintProperties()
Console.WriteLine("Waiting...")
Console.ReadLine()
Do Until Console.ReadLine = "x"
x.TryWriteProperties()
Console.WriteLine("Waiting...")
Loop
End Sub
これは私に「新しい値を設定できませんでした:プロパティセットメソッドが見つかりません」というエラーが表示されます。それは "ChildReadOnlyInteger"プロパティを設定しようとするとき。
Object type: 'ConsoleApplication1.clsChild'
The number of properties: 3
------------------------------------------------------------
Property name: ChildWritableInteger
Property type: System.Int32
Read-Write: True
Value: 5
------------------------------------------------------------
Property name: ChildReadOnlyInteger
Property type: System.Int32
Read-Write: False
Value: 10
------------------------------------------------------------
Property name: ParentInteger
Property type: System.Int32
Read-Write: True
Value: 0
------------------------------------------------------------
Waiting...
------------------------------------------------------------
ChildWritableInteger
Old Value: 5
New Value: 105
------------------------------------------------------------
ChildReadOnlyInteger
Old Value: 10
Failed to set new value: Property set method not found.
------------------------------------------------------------
ParentInteger
Old Value: 0
New Value: 100
Waiting...
私の質問は、基本クラスは、読み取り/書き込みとして公にプロパティを公開せずとにリンクされている2つのプロパティを持つことなく、プロパティまたは子クラスの変数のいずれかを変更することができる場所を使用するための最良の方法は何か、です同じ保持変数?
ありがとう@the_lotus - 私はプロパティの「一部」の範囲を設定することができませんでした - しかし、私はちょうどテストした、これは完全に初期のテストで動作します... – Bryant1003