3

如何在程序集 (DLL) 之外为使用 DLL 的人创建一个“只读”属性,但仍然能够从程序集中填充该属性以供他们阅读?

例如,如果我有一个Transaction对象,当Transaction对象发生某些事情时,它需要填充Document对象(这是Transaction类的子类)中的属性,但我只想开发人员使用我的 DLL能够读取该属性而不更改它(它只能从 DLL 本身中更改)。

4

4 回答 4

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
于 2008-12-18T17:21:35.420 回答
6

如果您使用的是 C#,您可以在 and 上有不同的访问修饰符getset例如以下内容应该可以实现您想要的:

public int MyProp { get; internal set; }

VB.NET 也有这个能力:http ://weblogs.asp.net/pwilson/archive/2003/10/28/34333.aspx

于 2008-12-18T17:21:00.787 回答
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
于 2008-12-18T17:23:18.940 回答
1

什么语言?在 VB 中,您将 setter 标记为 Friend,在 C# 中,您使用内部。

于 2008-12-18T17:21:22.940 回答