如何提取关键对象的字符串表示

发布于 2024-11-30 17:29:28 字数 1087 浏览 0 评论 0原文

我将项目存储在强类型 IDictionary 中,以便值也代表键:

public class MyObject
    {

        public string Name { get; private set; }
        public SectionId Section { get; private set; }

        public MyObject(SectionId section, string name)
        {
            Section = section;
            Name = name;
        }
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(MyObject)) return false;
            return Equals((MyObject)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Name.ToLower().GetHashCode() * 397) ^ Section.GetHashCode();
            }
        }

    }

在我的表示层中,我需要迭代此字典,将每个项目添加到 ListBox控制。我很难弄清楚如何将 MyObject (也充当键)转换为 ListBox 控件可以用作值的字符串。我应该像这样显式调用 MyObject.GetHashCode() 吗:

MyListBox.Add(new ListItem(myObject.Name, myObject.GetHashCode())

I'm storing items in a strongly typed IDictionary<TKey, TValue> such that the value also represents the key:

public class MyObject
    {

        public string Name { get; private set; }
        public SectionId Section { get; private set; }

        public MyObject(SectionId section, string name)
        {
            Section = section;
            Name = name;
        }
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(MyObject)) return false;
            return Equals((MyObject)obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Name.ToLower().GetHashCode() * 397) ^ Section.GetHashCode();
            }
        }

    }

In my presentation tier, I need to iterate through this Dictionary, adding each item to a ListBox control. I'm having a difficult time figuring out how to transform MyObject (which also acts as a key) into a string that the ListBox control can use as a value. Should I just make an explicit call to MyObject.GetHashCode() like this:

MyListBox.Add(new ListItem(myObject.Name, myObject.GetHashCode())

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

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

发布评论

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

评论(2

野心澎湃 2024-12-07 17:29:28

我会想到覆盖 toString 方法 在这里,您基本上将编写代码,生成一个有意义的字符串并在用户界面中显示

希望我正确理解您的问题。

I would think of overriding the toString method and in here you will basically write code that will generate a meaningful string to be displayed in the ui

Hope I understood your question correctly.

溺深海 2024-12-07 17:29:28

我应该显式调用 MyObject.GetHashCode()

不,GetHashCode() 是:

  1. 不能保证为您提供唯一的值,并且
  2. 很难进行逆向工程来生成 <代码>MyObject 来自。

相反,每个 MyObject 都应该有某种唯一的标识符键。这可以是一个enum值或由数据库中的IDENTITY列生成的数字,或者只是一个唯一标识每个特定MyObject字符串,您可以从中检索 MyObject 从您用作存储库的任何集合或数据库。

如果只能有一个具有给定 NameSectionMyObject,您可以将两者组合起来:SectionId + " :" + 名称。这样您就可以在事后解析这两个值。

Should I just make an explicit call to MyObject.GetHashCode()

No, GetHashCode() is:

  1. Not guaranteed to give you a unique value, and
  2. Going to be very difficult to reverse-engineer to produce a MyObject from.

Instead, each of your MyObjects should have some kind of unique identifier key. This can be an enum value or a number generated by an IDENTITY column in your database, or just a string that uniquely identifies each particular MyObject, and from which you can retrieve the MyObject from whatever collection or database you're using as a repository.

If there can only ever be a single MyObject with a given Name and Section, you could just combine the two: SectionId + ":" + Name. That way you can parse those two values out after the fact.

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