c# - Why is System.ComponentModel.AttributeCollection indexer inconsistent? -


in reference system.componentmodel.attributecollection.this[type t] indexer, docs @ http://msdn.microsoft.com/en-us/library/yadycs8s.aspx following

if attribute not exist in collection, property returns default value attribute type.

with in mind following code works expected: (> represents output)

using system.componentmodel; var attrcollection = new attributecollection(); console.writeline(attrcollection[typeof(browsableattribute)] != null); > "true" 

prints "true" expect. trying random attribute debuggerdisplay, indexer returns null:

var attrcollection = new attributecollection(); console.writeline(attrcollection[typeof(system.diagnostics.debuggerdisplayattribute)] != null); > "false" 

any ideas on different between these attributes, causing different behavior? unclear me msdn means 'default value attribute type' not null. thought perhaps problem attribute type no parameterless constructor, browsableattribute requires 1 argument, debuggerdisplayattribute.

it explicitly documented in msdn library article attributecollection:

while attributes have default values, default values not required. if attribute has no default value, null returned indexed property takes type. when defining own attributes, can declare default value either providing constructor takes no arguments, or defining public static field of attribute type named "default".

browsableattribute has such default value, provided default field, test succeeds. makes class members browsable default when attribute missing.

debuggerdisplayattribute has neither default constructor nor default field. makes sense if think it, there no meaningful default useful in debugger.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -