views:

192

answers:

6

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 anyway 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.

A: 

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.

Earlz
Unfortunately, you can not change the structure of an existing type using reflection (i.e. change its access modifiers or add/remove members). `Reflection.Emit` will let you create new types, but there is still no way to replace a given type since that involves changing a signed assembly.
Steve Guidi
+1  A: 

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.

fireeyedboy
I wouldn't consider that a "hack", at most a simple workaround :).
Ed Swangren
+2  A: 

Your 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"
  }
}
Sean
+4  A: 

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.

Dan Tao
Creating a Converter works great for going to string, but for setting a value it is harder (to take a String and go to WorkItemType). ToString allows you to still show the actual item rather than a converted item. Still, I think this is my best choice.
Vaccano
Yeah, if you also need to be able to get the object *from* whatever control it is you're using, you basically have, as I see it, one of two choices. You could pair your `FormatWorkItem` with a `ParseWorkItem` (or `TryParseWorkItem`) method, or you could write your own user control that maintains a collection internally and provides an interface to supply a custom formatter. The first option is the fastest, but can be an imperfect solution particularly if each of your objects isn't guaranteed to have a unique string representation.
Dan Tao
+5  A: 

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();)
SciFi
+1  A: 

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.
John Bowen
Sweet answer! I upvoted some of your other answers as "payment". Thanks!
Vaccano