Finding and using hidden properties(such as DisplayRectangle)

你。 提交于 2019-12-11 12:21:49

问题


In many code examples I have found the DisplayRectangle property of a Control object being used. However this property does not appear in the intellisense popup, neither does it get any syntax highlighting, but it does compile and work as expected.

Should I use this kind of Property?

How can I find out about more of them, can they be activated in intellisense?

Update/Clarification: I have now found out that it does seem that it depends on which control. The following code does compile:

        Control c = sender as Control;
        Form f = sender as Form;
        PictureBox p = sender as PictureBox;
        Console.Write(c.DisplayRectangle); // No Intellisense
        Console.Write(f.DisplayRectangle); // Intellisense
        Console.Write(p.DisplayRectangle); // No Intellisense

My question was about the DisplayRectangle for PictureBox, or Controls in general.


回答1:


This is the declaration of the property:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
[Browsable(false)]
[SRDescription("ControlDisplayRectangleDescr")]
public virtual Rectangle DisplayRectangle
{
    get
    {
        return new Rectangle(0x0, 0x0, this.clientWidth, this.clientHeight);
    }
}

Starting with [Browsable], that attribute ensures that the property doesn't show up in the Properties window. Which makes sense because it is a runtime property and there is no setter. That's also the relevance to [DesignerSerializationVisibility], it ensures that the property value doesn't get written to the InitializeComponent() method. [SRDescription] is for localization.

[EditorBrowsable] is relevant to your question. Using EditorBrowsableState.Advanced ensures that IntelliSense will only display the property if the editor is operating in 'Show advanced IntelliSense info' mode. The only IDE that I know that uses this feature is VB.NET, its IntelliSense popup window has an "All" tab but defaults to "Common". But not the C# IDE, the language you tagged your question with.

I have to guess that you are actually programming in VB.NET, not C#. Click the All tab on the popup.




回答2:


Instead can you modify the line slightely to : (c as Control).DisplayRectangle . In this style just after closing brace, the intellisense should show the property. Is that solve your problem ?




回答3:


To expand on Hans Passant's answer, those EditorBrowsableState.Advanced Properties will not show up in autocomplete from IntelliSense unless you have the "Hide advanced members" unchecked:

According to http://msdn.microsoft.com/en-us/library/system.componentmodel.editorbrowsableattribute.aspx , "In Visual C#, you can control when advanced properties appear in IntelliSense and the Properties Window with the Hide Advanced Members setting under Tools | Options | Text Editor | C#."

Note that after I complete the full (and correctly-cased) Property name, the tooltip always pops up for me, no matter how i set up the Options checkbox... Maybe it's because I have been using VS with advanced turned on, and now its cached database contains the details, but I believe that isn't the full story: I've noticed that using a Test method declared as "[EditorBrowsable(EditorBrowsableState.Advanced)] public void Test(object o)" with a string parameter brings up the "Test(object)" signature, so it does retrieve more details than it shows...



来源:https://stackoverflow.com/questions/3708771/finding-and-using-hidden-propertiessuch-as-displayrectangle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!