将“ToString”更改为对于密封类

发布于 2024-08-19 02:13:11 字数 371 浏览 14 评论 0原文

我正在使用一个类:

public sealed class WorkItemType

它的 ToString 很弱(仅显示 Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType)。

有什么方法可以覆盖它以显示 WorkItemType 的名称?

通常我只会聚合一个新类中的值,但我将其用于 WPF 中的绑定(我希望在组合框中有一个 WorkItemTypes 列表,并将选定的值分配给绑定的 WorkItemType)变量。)

我想我在这里运气不好,但我想我会问。

I have a class I am working with:

public sealed class WorkItemType

It's ToString is weak (Just shows Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemType).

Is there any way to override this to show the name of the WorkItemType?

Normally I would just aggregate the value in a new class, but I am using this for bindings in WPF (I want to have a list of WorkItemTypes in a combo box and assign the selected value to a bound WorkItemType variable.)

I think I am out of luck here, but I thought I would ask.

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

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

发布评论

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

评论(7

寄居人 2024-08-26 02:13:11

一种相当巧妙的方法可能是向 WorkItemType 对象添加扩展方法。像这样的东西:

public static class ToStringExtension
    {
        public static string MyToString(this WorkItemType w)
        {
           return "Some Stuff"
        }
    }

然后你可以调用类似的东西

WorkItemType w = new WorkItemType;
Debug.WriteLine(w.MyToString();)

A fairly neat way to do it might be to add an extenesion method to the WorkItemType object. Something like this:

public static class ToStringExtension
    {
        public static string MyToString(this WorkItemType w)
        {
           return "Some Stuff"
        }
    }

Then you could call something like

WorkItemType w = new WorkItemType;
Debug.WriteLine(w.MyToString();)
面犯桃花 2024-08-26 02:13:11

需要覆盖ToString吗?如果您可以控制显示对象的代码,则始终可以提供 FormatWorkItemType 方法或达到此效果的方法。

Do you need to override ToString? If you are in control of the code where the object is displayed, you can always provide a FormatWorkItemType method, or something to that effect.

血之狂魔 2024-08-26 02:13:11

WPF 提供了几种不同的内置方法来在 UI 中正确执行此操作。我推荐两个:

  • 您可以使用 ComboBox
    DisplayMemberPath 显示单个
    属性值但仍从中选择
    WorkItemType 对象。
  • 如果你想显示一个复合的
    您可以更改一些属性
    ComboBox 的 ItemTemplate 来制作
    看起来很漂亮,随心所欲 -
    设置文本格式、添加边框、
    颜色等。您甚至可以设置
    DataTemplate 自动成为
    应用于任何 WorkItemType 对象
    绑定在你的 UI 中的任何地方
    (与 UI 的基本效果相同
    改变 ToString 的视角)
    将其投入资源并给予
    它只是一个没有 x:Key 的数据类型。

WPF provides a few different built-in ways to do this right in the UI. Two I'd recommend:

  • You can use ComboBox's
    DisplayMemberPath to display a single
    property value but still select from
    the WorkItemType objects.
  • If you want to display a composite of
    a few properties you can change the
    ComboBox's ItemTemplate to make it
    look pretty much however you want -
    formatting text, adding borders,
    colors, etc. You can even set up the
    DataTemplate to automatically be
    applied to any WorkItemType object
    that gets bound anywhere in your UI
    (same basic effect from UI
    perspective as changing ToString) by
    putting it into Resources and giving
    it only a DataType with no x:Key.
郁金香雨 2024-08-26 02:13:11

你运气不好:-(

你可以编写自己的类来包装 WorkItemType 并委托给它(代理),除了 ToString:

class MyWorkItemType
{
  private WorItemType _outer;

  public MyWorkItemType(WorkItemType outer)
  {
    _outer=outer;
  }

  public void DoAction()
  {
    _outer.DoAction();
  }

  // etc

  public override string ToString()
  {
    return "my value"
  }
}

You're out of luck :-(

You could write your own class that wraps the WorkItemType and delegate down to it (a proxy) expect for the ToString:

class MyWorkItemType
{
  private WorItemType _outer;

  public MyWorkItemType(WorkItemType outer)
  {
    _outer=outer;
  }

  public void DoAction()
  {
    _outer.DoAction();
  }

  // etc

  public override string ToString()
  {
    return "my value"
  }
}
我做我的改变 2024-08-26 02:13:11

我没有任何 C# 知识,但是你不能将你的扩展类包装在另一个类中吗?代理对扩展类的所有方法调用,除了 toString() 之外,也非常hackish,但我想无论如何我都会提出它。

I don't have any C# knowledge, but can't you wrap your extended class inside another class? Proxy all method calls to the extended class, except toString(), Also very hackish, but I thought I'ld bring it up anyway.

鸩远一方 2024-08-26 02:13:11

通过反思做一些魔术可能是你唯一的希望。我知道你可以用它实例化私有构造函数,所以也许你可以覆盖密封类...注意,如果确实没有其他方法,这应该是你的最后手段。使用反射是一种非常黑客/不正确的方法。

Doing some sorta magic with reflection is probably your only hope. I know you can instantiate private constructors with it, so maybe you can override a sealed class... Note, this should be your last resort if there is seriously no other way. Using reflection is a very hackish/improper way of doing it.

伴随着你 2024-08-26 02:13:11

除了其他特定于WPF的答案之外,您还可以在格式绑定中使用IValueConverter / 根据需要显示 WorkItemType。这样做的优点是可重用(例如,如果您想在其他控件中显示对象。)

这里有许多使用转换器的示例。这个其他问题 应该与此处提到的 ComboBox 用法非常相似。答案指出,您可以使转换器处理整个对象集合,也可以一次处理一个项目。后者可能是更可重用的方法。

In addition to the other WPF-specific answer you could use an IValueConverter in the binding to format / display the WorkItemType however you want. This has an advantage of being reusable (if you want to display the object in some other control, for instance.)

There are many examples of using converters here. This other question should be pretty similar to the ComboBox usage mentioned here. The answers note that you can either make the converter work on the entire collection of objects, or work on one item at a time. The latter might be the more reusable approach.

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