Public Property Name() As String = "default_name"
Sub InitializeFields()
Name = String.Empty
'
'
'
Name = Name.GetDefaultValue.ToString ' an example
End Sub
以編程方式,如何恢復任何屬性的默認值“default_name”?
uj5u.com熱心網友回復:
這是一個示例,說明您應該如何使用實際的默認值執行此操作:
Imports System.ComponentModel
Public Class Thing
<DefaultValue("Hello World")>
Public Property WithDefault As String
Public Property WithoutDefault As String
End Class
Imports System.ComponentModel
Imports System.Reflection
Module Module1
Sub Main()
Dim something As New Thing
something.WithDefault = "First"
something.WithoutDefault = "Second"
If TrySetDefaultValue(something, NameOf(something.WithDefault)) Then
Console.WriteLine($"{NameOf(something.WithDefault)} reset to ""{something.WithDefault}""")
Else
Console.WriteLine($"No default value for {NameOf(something.WithDefault)}")
End If
If TrySetDefaultValue(something, NameOf(something.WithoutDefault)) Then
Console.WriteLine($"{NameOf(something.WithoutDefault)} reset to ""{something.WithoutDefault}""")
Else
Console.WriteLine($"No default value for {NameOf(something.WithoutDefault)}")
End If
End Sub
Public Function TryGetDefaultValue([object] As Object, propertyName As String, ByRef value As Object) As Boolean
Dim attribute = [object].GetType().GetProperty(propertyName).GetCustomAttribute(Of DefaultValueAttribute)()
If attribute Is Nothing Then
Return False
End If
value = attribute.Value
Return True
End Function
Public Function TrySetDefaultValue([object] As Object, propertyName As String) As Boolean
Dim [property] = [object].GetType().GetProperty(propertyName)
Dim attribute = [property].GetCustomAttribute(Of DefaultValueAttribute)()
If attribute Is Nothing Then
Return False
End If
Dim value = attribute.Value
[property].SetValue([object], value)
Return True
End Function
End Module
uj5u.com熱心網友回復:
沒有開箱即用的方法可以以某種方式將屬性恢復為其(自定義)初始值。一旦改變,就結束了;失去了原來的價值。
假設屬性屬于一個類(并且它沒有在建構式中被修改),您可以通過創建該類的臨時實體來檢索該值。例如:
Class SomeClass
Public Property Name As String = "default_name"
Sub InitializeFields()
Name = String.Empty
'
'
'
Name = New SomeClass().Name
End Sub
End Class
但是,這不是很健壯,并且可能會很快變得丑陋。我這樣做的方法是將默認值存盤在一個常量中:
Private Const DefaultName As String = "default_name"
Public Property Name As String = DefaultName
Sub InitializeFields()
Name = String.Empty
'
'
'
Name = DefaultName
End Sub
然后您可以為以后需要訪問其原始值的每個屬性執行此操作。
uj5u.com熱心網友回復:
回答我自己:
Imports System.Reflection
Imports System.ComponentModel
Public Class clsThisClass
' each property have attribute of default value
<DefaultValue(0)>
Public Property RecordCount() As Long = 0
<DefaultValue("a")>
Public Property SQL() As String = String.Empty
<DefaultValue(0)>
Public Property IndexID() As Long = 0
<DefaultValue("b")>
Public Property Name() As String = String.Empty
<DefaultValue("c")>
Public Property Title() As String = String.Empty
<DefaultValue("d")>
Public Property Document_No() As String = String.Empty
<DefaultValue("abc")>
Public Property Company_Code() As String = String.Empty
Sub InitializeFields()
With Me
RecordCount = 28
IndexID = 10
Name = "name"
Title = "title"
Document_No = "doc_no"
Company_Code = "com_code"
Debug.WriteLine(RecordCount)
Debug.WriteLine(IndexID)
Debug.WriteLine(Name)
Debug.WriteLine(Document_No)
Debug.WriteLine(Company_Code)
' revert or reset all properties to default value upon initialization
Dim aType As Type = GetType(clsThisClass)
' each property of class
For Each pi As System.Reflection.PropertyInfo In aType.GetProperties()
' grab assigned default value of the property
Dim attribute = Me.GetType().GetProperty(pi.Name.ToString).GetCustomAttribute(Of DefaultValueAttribute)()
Try
Dim value As Object = Nothing
' convert value type according to the type of the property
value = CTypeDynamic(value, GetType(Attribute))
If Not attribute Is Nothing Then
value = attribute.Value
' set default value
pi.SetValue(Me, value)
End If
Catch ex As Exception
End Try
Next
Debug.WriteLine(RecordCount)
Debug.WriteLine(IndexID)
Debug.WriteLine(Name)
Debug.WriteLine(Document_No)
Debug.WriteLine(Company_Code)
End With
End Sub
End Class
輸出:
28
10
名稱
doc_no
com_code
0
0
一個
b
C
d
美國廣播公司
一次用默認值初始化所有屬性。在我的場景中,我有很多類并且有幾個屬性。通過執行這幾行代碼。我可以在初始化方法時在每個類中復制粘貼。它節省了大量撰寫代碼的時間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/415370.html
標籤:
