Attributes in C#

前端 未结 8 739
时光说笑
时光说笑 2020-11-30 04:08

I know that C# (and .NET in general) is big on attributes. However, despite the fact I have programmed in C# for many years, I haven\'t found myself ever using them. Would s

8条回答
  •  再見小時候
    2020-11-30 04:20

    I like to use attributes as metadata to my code. We have created some simple attributes that let us tag who wrote what code, when, and why. This lets us have both documented changes in code and in runtime. If there are any exceptions during runtime, we can inspect the callstack, look at any attributes on the methods along the way, and track down the people responsible:

    [Author("Erich", "2009/04/06", Comment = "blah blah blah")]
    public void MyFunction()
    {
    ...
    }
    

    Of course, we could use our source control to look at who checked in what code, but this I've found makes the information more available in the place where you need it. Also, if we ever change source control, that information will not be lost since it is persisted in code.

提交回复
热议问题