Most useful property [off]

Keywords: Windows Attribute xml

I know properties are very useful. There are predefined ones, such as [Browsable(false)] that allow you to hide properties in the Properties tab. This is a good question to explain attributes: What are the properties in. NET?

What are the predefined properties (and their namespaces) that you actually use in your project?

#1st floor

[EditorBrowsable(EditorBrowsableState.Never)] allows you to hide properties and methods in IntelliSense if the project is not in your solution. It is useful for hiding invalid flows of fluent interfaces. How long do you want GetHashCode() or Equals()?

For MVC [ActionName("Name")] allows you to use the same method signature to Get the Get action and Post action, or use dashes in the action name, otherwise dashes cannot be used without creating a path for it.

#2nd floor

I think what's important here is that the following attributes are also very important:

STAThreadAttribute 

The COM thread model representing an application is a single thread unit (STA).

For example, use this property in a Windows Forms application:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

And

SuppressMessageAttribute

Reporting specific static analysis tool rule violations is prohibited, allowing multiple suppression of a single code artifact.

For example:

[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "isChecked")]
[SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fileIdentifier")]
static void FileNode(string name, bool isChecked)
{
    string fileIdentifier = name;
    string fileName = name;
    string version = String.Empty;
}

#3rd floor

DebuggerHiddenAttribute Allows you to avoid stepping into code that should not be debugged.

public static class CustomDebug
{
    [DebuggerHidden]
    public static void Assert(Boolean condition, Func<Exception> exceptionCreator) { ... }
}

...

// The following assert fails, and because of the attribute the exception is shown at this line
// Isn't affecting the stack trace
CustomDebug.Assert(false, () => new Exception()); 

It also prevents methods from being displayed in the stack trace, which is useful when there is a method that wraps only another method:

[DebuggerHidden]
public Element GetElementAt(Vector2 position)
{
    return GetElementAt(position.X, position.Y);
}

public Element GetElementAt(Single x, Single y) { ... }

If GetElementAt(new Vector2(10, 10)) is now called and an error occurs in the wrapper method, the call stack does not display the method that called the method that threw the error.

#4th floor

[DeploymentItem("myFile1.txt")] MSDN Doc on DeploymentItem

This is useful if you are testing a file or using it as input to the test.

#5th floor

[Serializable] is always used to serialize and deserialize objects to or from an external data source such as xml. More about it.

Posted by Mr.x on Fri, 20 Dec 2019 01:11:33 -0800