Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DebuggerDisplay for a dictionary's key / value pair?

Tags:

c#

.net

Is it possible to assign a System.Diagnostics.DebuggerDisplay attribute to the definition of a Dictionary?

e.g.

Dictionary<int, SomeClass> 
[System.Diagnostics.DebuggerDisplay("{Info,nq}")]
public class SomeClass{

    string code {get;set;}

    public string Info { get { return "info" + code; }}
}


// place an attribute here??
[System.Diagnostics.DebuggerDisplay("{???,nq}")]
Dictionary<int, SomeClass> dict = new Dictionary<int, SomeClass>();
like image 433
sgtz Avatar asked Mar 01 '12 15:03

sgtz


People also ask

How to add a key value pair to dictionary in Python?

Let’s see how to add a key:value pair to dictionary in Python. Code #1: Using Subscript notation. This method will create a new key:value pair on a dictionary by assigning a value to that key. # Python program to add a key:value pair to dictionary. dict = {'key1':'geeks', 'key2':'for'}.

What is the argument of debuggerdisplay?

The DebuggerDisplay attribute has a single argument, which is a string to be displayed in the value column for instances of the type. This string can contain braces ( { and } ). Text within a pair of braces is evaluated as a field, property or method.

What is a key-value pair in JavaScript?

Key-value pairs may be added when building a JavaScript dictionary; however, these techniques can also add values. The key stands for the identifier of the unique key, and the value is the corresponding data the key relates to.

What is a dictionary key and value?

The key stands for the identifier of the unique key, and the value is the corresponding data the key relates to. If the dictionary already includes the name you have supplied as the key, you could either change your key or use the following code to update the value. Accessing things is likewise fairly basic; the following code may be used.


1 Answers

EDIT: see also this answer.

I haven't tried it, but from the documentation, it's possible to apply this attribute at the assembly level. So in theory, you could do something like this:

[assembly: DebuggerDisplay("{Key,nq}: {Value,nq}", Target = typeof(KeyValuePair<int, SomeClass>))]

I'd be surprised if it lets you get away with specifying a Target which is an open generic type, e.g. Target = typeof(KeyValuePair<,>) which would work for a KVP of any type. But if you need that, it's worth a try!

like image 184
anton.burger Avatar answered Nov 15 '22 08:11

anton.burger