Tutorial by Examples

//1) All attributes should be inherited from System.Attribute //2) You can customize your attribute usage (e.g. place restrictions) by using System.AttributeUsage Attribute //3) You can use this attribute only via reflection in the way it is supposed to be used //4) MethodMetadataAttribute is jus...
[StackDemo(Text = "Hello, World!")] public class MyClass { [StackDemo("Hello, World!")] static void MyMethod() { } }
Method GetCustomAttributes returns an array of custom attributes applied to the member. After retrieving this array you can search for one or more specific attributes. var attribute = typeof(MyClass).GetCustomAttributes().OfType<MyCustomAttribute>().Single(); Or iterate through them forea...
Adding the DebuggerDisplay Attribute will change the way the debugger displays the class when it is hovered over. Expressions that are wrapped in {} will be evaluated by the debugger. This can be a simple property like in the following sample or more complex logic. [DebuggerDisplay("{StringPr...
Caller info attributes can be used to pass down information about the invoker to the invoked method. The declaration looks like this: using System.Runtime.CompilerServices; public void LogException(Exception ex, [CallerMemberName]string callerMemberName = "", ...
There is no simple way to obtain attributes from an interface, since classes does not inherit attributes from an interface. Whenever implementing an interface or overriding members in a derived class, you need to re-declare the attributes. So in the example below output would be True in all three c...
System.Obsolete is an attribute that is used to mark a type or a member that has a better version, and thus should not be used. [Obsolete("This class is obsolete. Use SomeOtherClass instead.")] class SomeClass { // } In case the class above is used, the compiler will give the w...

Page 1 of 1