title: PropertyGrid Browsable not found for entity framework created property, how to find it?
body: <p>Trying to remove or place items on a property grid by changing the Browsable attribute.</p>
  
  <p>But unless browsable is set on object creation my code to change Browsable doesn't work.  Now I can manually add browsable, but when I make a change to my entity (still developing project so lots of changes to entity) any additional attributes I add go away.</p>
  
  <p>I attempted to set [Browsable(true)] two ways other ways: <a href="http://ardalis.com/adding-attributes-to-generated-classes" rel="nofollow">http://ardalis.com/adding-attributes-to-generated-classes</a> and <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/617ebfca-0f68-4b90-83fd-0da758fadbd0/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/617ebfca-0f68-4b90-83fd-0da758fadbd0/</a> </p>
  
  <p>Both seem to actually set the Browsable correctly, but when I loop thru the Attributes in Property Descriptor it is not there (for me to change).</p>
  
  <pre><code>  String fieldname = "browsable"; // I also edit "description"
    PropertyDescriptor pd = TypeDescriptor.GetProperties(o.GetType())[propertyName];
    object attrib = null;
    AttributeCollection attribs = pd.Attributes;
  
    foreach (Attribute a in attribs)
      {
      if (a.GetType() == attributeType)
        {
        attrib = a;
        break;
        }
      }
  // The microsoft documentation leads one to believe the following line of code would find the desired attribute,
  // negating the need for the more complete foreach statement above.
  // However,  it appears to find attribute even when it does not exist. Setting value for "found" attribute 
  // will result in random memory being changed, which results in very unpredictable behavior.
  // attrib = pd.Attributes[t];
  
  if (attrib != null)
    {
    // locate field that contains value 
    FieldInfo field = attrib.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
    if (field != null)
      {
      if (field.FieldType == value.GetType())
        {
        // set field to desired value
        field.SetValue(attrib, value);
        }
      }
      }
    else
      {
      throw new Exception("Attribute (" + attributeType.Name + ") does not exist for Property(" + propertyName + ")");
      }
  </code></pre>
  
  <p>So I keep getting the Exception that I throw if it doesn't find "browsable" - but only if not set in Model.Designer.cs first.</p>
  
  <p>Below is what my Model.Designer.cs looks like.</p>
  
  <pre><code>  /// &lt;summary&gt;
    /// No Metadata Documentation available.
    /// &lt;/summary&gt;
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    [Browsable(false)] // this works, but goes away if change my entity
    public Nullable&lt;global::System.TimeSpan&gt; SignoutAfter
      {
      get
        {
        return _SignoutAfter;
        }
      set
        {
        OnSignoutAfterChanging(value);
        ReportPropertyChanging("SignoutAfter");
        _SignoutAfter = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("SignoutAfter");
        OnSignoutAfterChanged();
        }
      }
    private Nullable&lt;global::System.TimeSpan&gt; _SignoutAfter;
    partial void OnSignoutAfterChanging(Nullable&lt;global::System.TimeSpan&gt; value);
    partial void OnSignoutAfterChanged();
  </code></pre>
  
  <p>So I need a way to either 1. add browsable to entity when I edit them so it is always on perhaps editing the t4, but I don't even know where to begin with that or 2. Another way to add or remove (and edit) the properties (see I might edit the description based on some logic) or 3 find the hole in my code so I can find and edit browsable (description and displayname).</p>
  
  <p><strong>Update</strong> The second link above, <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/617ebfca-0f68-4b90-83fd-0da758fadbd0/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/617ebfca-0f68-4b90-83fd-0da758fadbd0/</a> , has a lot of what I need, I think.  Adding a Attribute array variable to the class and some code to see if that is set seems to have the effect that I am looking for.  But leaving this open to find a better answer.</p>
  
  <pre><code>partial class Client : ICustomTypeDescriptor
    {
     public Attribute[] SignOutAttributes; // added this
  
     #region ICustomTypeDescriptor Members
  
     ... // see the link for the other code
  
     public PropertyDescriptorCollection GetProperties (Attribute[] attributes)
        {
        var propsColl = TypeDescriptor.GetProperties (this, attributes, true);
        var props = new List&lt;PropertyDescriptor&gt; ();
        foreach (PropertyDescriptor prop in propsColl)
           {
           String strUPPERCaseName = prop.Name.ToUpper (); // for my thick fingers
           // make sure case values are upper case 
           switch (strUPPERCaseName)
              {
              case "SIGNOUTAFTER":
                 if (SignOutAttributes != null)
                    {
                    props.Add(new CustomPropertyDescriptor(prop, SignOutAttributes));
                    }
                  else
                    {
                     props.Add (new CustomPropertyDescriptor (prop, new Attribute[]
                       {
                       new CategoryAttribute("Settings"),
                       new DisplayNameAttribute("Signout After"),
                       new BrowsableAttribute(true),
                       new ReadOnlyAttribute(false)
                       }));
                    }
                  break;
              default:
                 props.Add (prop);
                 break;
              }
           }
         return new PropertyDescriptorCollection (props.ToArray ());
         }
  </code></pre>
  
  <p>In my code I can change the Attribute Array to have what Attribute values I want.</p>
  
  <pre><code> _client.SignOutAttributes = new Attribute[]
                  {
                      new CategoryAttribute ("My Category"),
                      new DisplayNameAttribute("Signout After"),
                      new BrowsableAttribute(true),
                      new ReadOnlyAttribute(false)
                  };
  </code></pre>
  
  <p>I'm not 100% happy with this.  I have to write code for each Property.</p>