2008-12-18 23 views
3

如何爲使用DLL的人員在程序集(DLL)外創建一個屬性「ReadOnly」,但仍然能夠從程序集內爲其讀取屬性?ReadOnly vs大會內部屬性問題/難題

舉例來說,如果我有一個交易對象,需要填充在文檔對象的屬性(這是一個子類的交易類),當事情發生在交易對象,但我只想開發人員使用我的DLL只能讀取該屬性,而不能更改它(它應該只在DLL本身內進行更改)。

回答

7

C#

public object MyProp { 
    get { return val; } 
    internal set { val = value; } 
} 

VB

Public Property MyProp As Object 
    Get 
     Return StoredVal 
    End Get 
    Friend Set(ByVal value As Object) 
     StoredVal = value 
    End Set 
End Property 
1

什麼語言?在VB中,將setter標記爲Friend,在C#中使用internal。

3

C#

public bool MyProp {get; internal set;} //Uses "Automatic Property" sytax 

VB

private _MyProp as Boolean 
Public Property MyProp as Boolean 
    Get 
     Return True 
    End Get 
    Friend Set(ByVal value as Boolean) 
     _MyProp = value 
    End Set 
End Property