# Attribute

***

**1. Custom Validation Attributes**

```csharp
[AttributeUsage(AttributeTargets.Property)]
public class ValidateLongAttribute : ValidationAttribute
{
    public override bool IsValid(object value) => value is long && (long)value >= 0;
}

[ValidateLong]
public long MyLong { get; set; }
```

**2. Code Contract Attributes**

```csharp
[ContractClass(typeof(DisposableContract<>))]
public interface IDisposable
{
    void Dispose();
}

[ContractClassFor(typeof(IDisposable))]
public abstract class DisposableContract : IDisposable
{
    public void Dispose() { Contract.Requires<ObjectDisposedException>(!Disposed); }
    public bool Disposed { get; protected set; }
}
```

**3. XML Documentation Attributes**

```csharp
/// <summary>Gets or sets the name of the customer.</summary>
[XmlAttribute]
public string CustomerName { get; set; }
```

**4. Entity Framework Data Annotations**

```csharp
[Key]
public int Id { get; set; }

[Required]
public string Name { get; set; }
```

**5. Unit Test Framework Attributes**

```csharp
[Fact]
public void Example_Test() { }

[Theory]
[InlineData(1, 2, 3)]
[InlineData(4, 5, 9)]
public void Example_Theory(int a, int b, int expected) { }
```

**6. ASP.NET MVC Action Attributes**

```csharp
[HttpGet]
public ActionResult Index() { }

[HttpPost]
public ActionResult Create(Product product) { }
```

**7. ASP.NET MVC Model Binding Attributes**

```csharp
public class Product
{
    [BindRequired]
    public string Name { get; set; }
    [BindNever]
    public int HiddenValue { get; set; }
}
```

**8. Web API Attributes**

```csharp
[Authorize]
public class ProductsController : ApiController { }

[HttpPost]
[Route("api/products")]
public IHttpActionResult Create(Product product) { }
```

**9. Dependency Injection Attributes**

```csharp
[Injectable]
public class ProductService { }

[Injectable("MyCustomServiceName")]
public class MyCustomService { }
```

**10. Logging Attributes**

```csharp
[Log]
public void Example_Method() { }

[Log(Level = LogLevel.Info)]
public void Example_Info() { }
```

**11. Exception Handling Attributes**

```csharp
[HandleException(typeof(ArgumentNullException))]
public int Example_Method(string s) { }

[HandleException(DefaultHandler = "CustomExceptionHandler")]
public void Example_CustomHandler() { }
```

**12. Performance Monitoring Attributes**

```csharp
[PerformanceCounter("CustomCounter")]
public void Example_Method() { }

[PerformanceCounter("CustomCounter", Increment = false)]
public void Example_NoIncrement() { }
```

**13. Caching Attributes**

```csharp
[Cache(Duration = 60)]
public string Example_Cache() { }

[Cache(Key = "customKey")]
public object Example_CustomKey() { }
```

**14. Concurrency Attributes**

```csharp
[ConcurrencyCheck]
public class Product
{
    [Timestamp]
    public byte[] Version { get; set; }
}
```

**15. Threading Attributes**

```csharp
[ThreadStatic]
public int SharedVariable { get; set; }

[MethodImpl(MethodImplOptions.Synchronized)]
public void Example_Synchronized() { }
```

**16. Security Attributes**

```csharp
[Permissions(PermissionLevel.Admin)]
public void Example_AdminPermission() { }

[Permissions(PermissionLevel.User, PermissionLevel.Manager)]
public void Example_MultiplePermissions() { }
```

**17. Globalization Attributes**

```csharp
[Globalizable(false)]
public class NonGlobalizableClass { }

[Globalizable(false)]
public class PartiallyGlobalizable : NonGlobalizableClass
{
    [Globalizable(true)]
    public string GlobalizableProperty { get; set; }
}
```

**18. Serialization Attributes**

```csharp
[Serializable]
public class Example_Serialized { }

[NonSerialized]
public int NonSerializedValue { get; set; }
```

**19. Designer Attributes**

```csharp
[Designer("MyCustomDesigner")]
public class Example_CustomDesigner { }

[DesignerCategory("MyCustomCategory")]
public class Example_CustomCategory { }
```

**20. Assembly Attributes**

```csharp
[AssemblyInformationalVersion("1.0.0.0")]
public class Example_AssemblyInfo { }

[AssemblyCulture("en-US")]
public class Example_AssemblyCulture { }
```

**21. Type Attributes**

```csharp
[StructLayout(LayoutKind.Sequential)]
public struct Example_Struct { }

[Flags]
public enum Example_Flags { A = 1, B = 2, C = 4 }
```

**22. Parameter Attributes**

```csharp
public void Example_Method([Optional]string s) { }

[ParamArray]
public void Example_ParamArray(params string[] args) { }
```

**23. Return Value Attributes**

```csharp
public [NotNull]string Example_NotNull() { }

[return: MarshalAs(UnmanagedType.LPStr)]
public string Example_LPString() { }
```

**24. Conditional Attributes**

```csharp
#if DEBUG
public void Example_Debug() { }
#else
public void Example_Release() { }
#endif
```

**25. Obsolete Attributes**

```csharp
[Obsolete("Use Example_New instead.")]
public void Example_Obsolete() { }

[Obsolete("Removed in version 2.0.", true)]
public void Example_Removed() { }
```

**26. InternalsVisibleTo Attributes**

```csharp
[assembly: InternalsVisibleTo("MyOtherAssembly")]
public class Example_InternalsVisible { }
```

**27. Conditional Compilation Attributes**

```csharp
#define USE_MY_METHOD

public void Example_ConditionalCompilation()
{
#if USE_MY_METHOD
    MyMethod();
#endif
}
```

**28. Covariance and Contravariance Attributes**

```csharp
[Out]
public void Example_Out(out string s) { }

[In]
public string Example_In([In]string s) { }
```

**29. CLI Compatible Attributes**

```csharp
[DllImport("MyLibrary.dll")]
public static extern void Example_DllImport();

[ComImport]
[Guid("00000000-0000-0000-0000-000000000000")]
public class Example_ComImport { }
```

**30. Event Attributes**

```csharp
public event EventHandler Example_Event;

[AddOnly]
public event EventHandler Example_AddOnly;
```

**31. Field Attributes**

```csharp
public const string Example_Const = "MyConstant";

public readonly int Example_Readonly = 10;
```

**32. Method Attributes**

```csharp
public abstract void Example_Abstract();

public extern void Example_Extern();
```

**33. Property Attributes**

```csharp
public virtual string Example_Virtual { get; set; }

public override int Example_Override { get; set; }
```

**34. Interface Attributes**

```csharp
[ComVisible(false)]
public interface IExample { }

[Guid("00000000-0000-0000-0000-000000000000")]
public interface IExample2 { }
```

**35. Delegate Attributes**

```csharp
public delegate void Example_Delegate();

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void Example_UnmanagedDelegate();
```

**36. Enum Attributes**

```csharp
public enum Example_Enum
{
    [Description("My Description")]
    Value = 10
}
```

**37. Flags Enum Attributes**

```csharp
[Flags]
public enum Example_FlagsEnum : long
{
    Value1 = 1L << 0,
    Value2 = 1L << 1,
    Value3 = 1L << 2
}
```

**38. Struct Attributes**

```csharp
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Example_Struct
{
    public byte Value1;
    public byte Value2;
}
```

**39. Union Attributes**

```csharp
[StructLayout(LayoutKind.Explicit)]
public struct Example_Union
{
    [FieldOffset(0)]
    public int Value1;
    [FieldOffset(0)]
    public string Value2;
}
```

**40. Marshaling Attributes**

```csharp
[MarshalAs(UnmanagedType.LPWStr)]
public string Example_LPWStr { get; set; }

[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)]
public object[] Example_SafeArray { get; set; }
```

**41. COM Interop Attributes**

```csharp
[ComVisible(false)]
public class Example_ComVisible { }

[Guid("00000000-0000-0000-0000-000000000000")]
public interface IExample_ComInterface { }
```

**42. Globalization Attributes**

```csharp
[Globalizable(false)]
public class Example_NonGlobalizable { }

[SatelliteContractVersion("1.0")]
public class Example_SatelliteContract { }
```

**43. Security Attributes**

```csharp
[SecurityCritical]
public class Example_SecurityCritical { }

[Permissions(PermissionState.Unrestricted)]
public void Example_Unrestricted() { }
```

**44. Threading Attributes**

```csharp
[ThreadStatic]
public int Example_ThreadStatic { get; set; }

[MethodImpl(MethodImplOptions.NoInlining)]
public void Example_NoInlining() { }
```

**45. Concurrency Attributes**

```csharp
[MethodImpl(MethodImplOptions.Synchronized)]
public void Example_Synchronized() { }

[MethodImpl(MethodImplOptions.NoOptimization)]
public void Example_NoOptimization() { }
```

**46. Obsolete Attributes**

```csharp
[Obsolete("Use Example_New instead.")]
public void Example_Obsolete() { }

[Obsolete("Removed in version 2.0.", true)]
public void Example_Removed() { }
```

**47. Code Contract Attributes**

```csharp
[ContractClass(typeof(Example_Contract))]
public interface IExample { }

[ContractClassFor(typeof(IExample))]
public abstract class Example_Contract : IExample
{
    public void Dispose() { Contract.Requires<ObjectDisposedException>(!Disposed); }
    public bool Disposed { get; protected set; }
}
```

**48. Unit Test Framework Attributes**

```csharp
[Fact]
public void Example_Test() { }

[Theory]
[InlineData(1, 2, 3)]
[InlineData(4, 5, 9)]
public void Example_Theory(int a, int b, int expected) { }
```

**49. ASP.NET MVC Action Attributes**

```csharp
[HttpGet]
public ActionResult Index() { }

[HttpPost]
public ActionResult Create(Product product) { }
```

**50. ASP.NET MVC Model Binding Attributes**

```csharp
public class Product
{
    [BindRequired]
    public string Name { get; set; }
    [BindNever]
    public int HiddenValue { get; set; }
}
```
