TypeParamAttribute
The TypeParamAttribute
class provides metadata about type parameters in generic type definitions. It is primarily used by tools and documentation generators to describe generic constraints and relationships.
Namespace
System.Runtime.CompilerServices
Assembly
System.Runtime.dll
Inheritance
System.Attribute
→ TypeParamAttribute
Syntax
public sealed class TypeParamAttribute : Attribute
{
public TypeParamAttribute(string name);
public string Name { get; }
public string? Description { get; set; }
public Type[]? Constraints { get; set; }
}
Members
Member | Signature | Description |
---|---|---|
Constructor | TypeParamAttribute(string name) |
Initializes a new instance with the required name of the type parameter. |
Property | string Name { get; } |
Gets the name of the type parameter. |
Property | string? Description { get; set; } |
Optional human‑readable description. |
Property | Type[]? Constraints { get; set; } |
Array of constraint types applied to the type parameter. |
Usage
Applied to generic type declarations to provide additional information for documentation tools.
[TypeParam("T", Description = "The item type.", Constraints = new[] { typeof(IDisposable) })]
public class Repository<T> where T : IDisposable
{
// Implementation...
}
Examples
Example 1 – Basic Documentation
using System;
using System.Runtime.CompilerServices;
[AttributeUsage(AttributeTargets.GenericParameter, Inherited = false)]
public sealed class TypeParamAttribute : Attribute
{
public TypeParamAttribute(string name) => Name = name;
public string Name { get; }
public string? Description { get; set; }
public Type[]? Constraints { get; set; }
}
[TypeParam("T", Description = "The data type stored in the collection.")]
public class MyCollection<T>
{
// …
}
Example 2 – Multiple Parameters
[TypeParam("TKey", Description = "Key type.", Constraints = new[] { typeof(IComparable) })]
[TypeParam("TValue", Description = "Value type.")]
public class PairDictionary<TKey, TValue> where TKey : IComparable
{
// …
}