DebuggerDisplayAttribute with partial classes

168 views Asked by At

I have a WCF WebService that generates partial classes. Therefore I cannot put DebuggerDisplayAttribute on any of them, because it would be overriden at each update of the web service reference.

Can I have partial classes that define the DebuggerDisplayAttribute for some properties, just like for instance we can use MetadataType for view display in MVC?

1

There are 1 answers

1
rene On

You are correct that changing the generated DataContract is a waste of time.

Luckily the classes are partial so you can have a second class file that holds logic that you need but doesn't get generated. You can annotate that partial class with the Debugger hints. For your scenario you'll need a DebuggerTypeProxy that holds a type you created to be instantiated by the debugger instead of the instance of your real class:

// namespace should match the namespace in your reference.cs
namespace MyCompany.MyApp.Service
{
    [DebuggerDisplay("Example")]
    [DebuggerTypeProxy(typeof(InternalProxy))]
    public partial class Example // name of your DataContract class in the reference.cs
    {
        internal class InternalProxy
        {
            private Example _realClass;
            // the debugger instantiate this class
            // with a reference to the instance being watched
            public InternalProxy(Example realClass)
            {
                _realClass = realClass;
            }

            [DebuggerDisplay("Description = {Proxiedproperty}")]
            public string Proxiedproperty {
                // description is a genereated property
                get { return _realClass.description; }
            }

                    // add other properties if needed
        }
    }
}