只读与程序集中的属性问题/难题

发布于 2024-07-09 19:27:07 字数 285 浏览 9 评论 0原文

如何在程序集 (DLL) 外部为使用 DLL 的人员设置“只读”属性,但仍然能够从程序集中填充该属性以供他们读取?

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

How can I make a Property "ReadOnly" outside the Assembly (DLL) for people using the DLL but still be able to populate that property from within the assembly for them to read?

For example, if I have a Transaction object that needs to populate a property in a Document object (which is a child class of the Transaction class) when something happens in the Transaction object, but I just want to developer using my DLL to only be able to read that property and not change it (it should only be changed from within the DLL itself).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与之呼应 2024-07-16 19:27:07

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

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
李白 2024-07-16 19:27:07

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

public int MyProp { get; internal set; }

VB.NET 也具有此功能:< a href="http://weblogs.asp.net/pwilson/archive/2003/10/28/34333.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/pwilson/archive/2003 /10/28/34333.aspx

If you're using C# you can have different access modifiers on the get and set, e.g. the following should achieve what you want:

public int MyProp { get; internal set; }

VB.NET also has this capability: http://weblogs.asp.net/pwilson/archive/2003/10/28/34333.aspx

呆° 2024-07-16 19:27:07

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

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
不再见 2024-07-16 19:27:07

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

What language? In VB you mark the setter as Friend, in C# you use internal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文