如何在 .net 中使用类的属性附加附加信息
我想创建一个商业实体。我想存储每个属性的附加信息,
例如
Public Class BE
{
private string _fundCity;
public string FUND_CITY
{
get { return _fundCity; }
set { _fundCity = value; }
}
}
对于 FUND_CITY,我想在 BE 中存储“StartOffSet”和“EndOffSet”值。 有人可以帮忙吗?
I want to create a business entity. I want to store additional information with each property
e.g.
Public Class BE
{
private string _fundCity;
public string FUND_CITY
{
get { return _fundCity; }
set { _fundCity = value; }
}
}
For FUND_CITY i want to store "StartOffSet" and "EndOffSet" values in BE.
Can some one help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望拥有与
FUND_CITY
直接相关的属性,那么通常您会使其本身成为一个对象,并将属性添加到新对象中,并且您仍然可以将其存储在您的对象中< code>BE 就像你现在一样。如果您希望
FUND_CITY
保留为字符串,则必须添加StartOffset
和EndOffset
作为BE
上的属性类,就像 FUND_CITY 一样。我会给你一个例子,但你没有指定这两个属性是什么类型。编辑:
听起来您需要的是一个带有泛型镜头的包装类。如果您想跟踪
BE
上每个属性的StartOffset
和EndOffset
,那么首先创建一个泛型类,该类将用于 BE 中的每个属性:PropertyValue
用于保存(或包装)属性的实际值。然后,您的类BE
会更改为如下所示:然后您可以像这样使用它:
如果您想更进一步并保留这些内容的列表,那么您可以使用内置的泛型 列表输入 ,但是你有一个小问题:List希望其所有内容都属于同一类型。您可以通过使用基类或仅将 List中的所有内容强制转换来解决此问题。输入
object
。这是一个使用基类的示例,它与上面的代码有一些小变化:If you want to have properties that are directly related to
FUND_CITY
then normally you would make it an object in its own right and add the properties to the new object, and you can still store it in your objectBE
as you are now.If you want
FUND_CITY
to remain as a string, then you must addStartOffset
andEndOffset
as properties on theBE
class, just as you have with FUND_CITY. I would give you an example but you haven't specified what types those two properties are.EDIT:
It sounds like what you need is a wrapper class with a shot of generics. If you want to track
StartOffset
andEndOffset
for every property onBE
, then first create a generic class that will be used for each property in BE:PropertyValue
is used to hold (or wrap) the actual value of the property. Your classBE
then changes to look like this:you can then use it like this:
If you want to go still further and keep a list of these, then you can use the inbuilt generic List<T> type, but then you have a small issue: the List<T> wants all its contents to be of the same type. You can get round this either by using a base class, or by just casting everything in the List<T> to type
object
. Here is an example of using the base class, it has a small change from the code above:您需要创建一个单独的类(请注意,我的语法使用 VS2010 自动设置器属性;如果您使用的是早期版本,则需要以老式方式创建属性:
使用通用解决方案更新
You need to create a separate class (note that my syntax is using VS2010 auto-setter properties; if you are using an earlier version you will need to create the properties the old-fashioned way:
Update with Generic solution