如何在 .net 中使用类的属性附加附加信息

发布于 2024-12-15 11:32:44 字数 334 浏览 0 评论 0原文

我想创建一个商业实体。我想存储每个属性的附加信息,

例如

    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 技术交流群。

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

发布评论

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

评论(2

永言不败 2024-12-22 11:32:44

如果您希望拥有与 FUND_CITY 直接相关的属性,那么通常您会使其本身成为一个对象,并将属性添加到新对象中,并且您仍然可以将其存储在您的对象中< code>BE 就像你现在一样。

如果您希望 FUND_CITY 保留为字符串,则必须添加 StartOffsetEndOffset 作为 BE 上的属性类,就像 FUND_CITY 一样。我会给你一个例子,但你没有指定这两个属性是什么类型。

编辑:

听起来您需要的是一个带有泛型镜头的包装类。如果您想跟踪 BE每个属性的 StartOffsetEndOffset,那么首先创建一个泛型类,该类将用于 BE 中的每个属性:

public class MyProperty<T> 
{
    public MyProperty(T propertyValue)
    {
        PropertyValue = propertyValue;
    }

    public int StartOffset { get; set; }
    public int EndOffset { get; set; }

    public T PropertyValue { get; set; }
}

PropertyValue 用于保存(或包装)属性的实际值。然后,您的类 BE 会更改为如下所示:

public class BE
{
    public MyProperty<string> FUND_CITY { get; set; }

    public MyProperty<int> SomeOtherProperty { get; set; }
}

然后您可以像这样使用它:

class Program
{
    static void Main(string[] args)
    {

        var myBE = new BE();
        myBE.FUND_CITY = new MyProperty<string>("some random string value") { StartOffset = 0, EndOffset = 10 };
        myBE.SomeOtherProperty = new MyProperty<int>(999);

        Console.ReadKey();
    }
}

如果您想更进一步并保留这些内容的列表,那么您可以使用内置的泛型 列表输入,但是你有一个小问题:List希望其所有内容都属于同一类型。您可以通过使用基类或仅将 List中的所有内容强制转换来解决此问题。输入object。这是一个使用基类的示例,它与上面的代码有一些小变化:

public class MyPropertyBase
{
    public int StartOffset { get; set; }
    public int EndOffset { get; set; }
}

public class MyProperty<T> : MyPropertyBase
{
    public MyProperty(T propertyValue)
    {
        PropertyValue = propertyValue;
    }

    public T PropertyValue { get; set; }
}

public class BE
{
    public MyProperty<string> FUND_CITY { get; set; }

    public MyProperty<int> SomeOtherProperty { get; set; }

    public List<MyPropertyBase> MyDataPoints { get; set; }
}

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 object BE as you are now.

If you want FUND_CITY to remain as a string, then you must add StartOffset and EndOffset as properties on the BE 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 and EndOffset for every property on BE, then first create a generic class that will be used for each property in BE:

public class MyProperty<T> 
{
    public MyProperty(T propertyValue)
    {
        PropertyValue = propertyValue;
    }

    public int StartOffset { get; set; }
    public int EndOffset { get; set; }

    public T PropertyValue { get; set; }
}

PropertyValue is used to hold (or wrap) the actual value of the property. Your class BE then changes to look like this:

public class BE
{
    public MyProperty<string> FUND_CITY { get; set; }

    public MyProperty<int> SomeOtherProperty { get; set; }
}

you can then use it like this:

class Program
{
    static void Main(string[] args)
    {

        var myBE = new BE();
        myBE.FUND_CITY = new MyProperty<string>("some random string value") { StartOffset = 0, EndOffset = 10 };
        myBE.SomeOtherProperty = new MyProperty<int>(999);

        Console.ReadKey();
    }
}

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:

public class MyPropertyBase
{
    public int StartOffset { get; set; }
    public int EndOffset { get; set; }
}

public class MyProperty<T> : MyPropertyBase
{
    public MyProperty(T propertyValue)
    {
        PropertyValue = propertyValue;
    }

    public T PropertyValue { get; set; }
}

public class BE
{
    public MyProperty<string> FUND_CITY { get; set; }

    public MyProperty<int> SomeOtherProperty { get; set; }

    public List<MyPropertyBase> MyDataPoints { get; set; }
}
也只是曾经 2024-12-22 11:32:44

您需要创建一个单独的类(请注意,我的语法使用 VS2010 自动设置器属性;如果您使用的是早期版本,则需要以老式方式创建属性:

public class FundCity
{
  public string Name {get; set;}
  public int StartOffSet {get; set;}
  public int EndOffSet{get; set;}

    public override string ToString()
    {
        return this.Name;
    }
}

public class BE
{
private FundCity _fundCity;
 public FundCity FUND_CITY 
        {
            get
            {
              return _fundCity;
            }
             set 
            {
               _fundCity=value;
            }
        }
}

使用通用解决方案更新

public class DataPoint<T>
{
    public T value {get; set; }

    public int StartOffset {get; set; }
    public int EndOffset { get; set; }

    public override string ToString()
    {
        return value.ToString();
    }
}

public class BE
{
    public DataPoint<String> FUND_CITY { get; set; }
}

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:

public class FundCity
{
  public string Name {get; set;}
  public int StartOffSet {get; set;}
  public int EndOffSet{get; set;}

    public override string ToString()
    {
        return this.Name;
    }
}

public class BE
{
private FundCity _fundCity;
 public FundCity FUND_CITY 
        {
            get
            {
              return _fundCity;
            }
             set 
            {
               _fundCity=value;
            }
        }
}

Update with Generic solution

public class DataPoint<T>
{
    public T value {get; set; }

    public int StartOffset {get; set; }
    public int EndOffset { get; set; }

    public override string ToString()
    {
        return value.ToString();
    }
}

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