Skip to content

Class TypeExtensions

Namespace: Momentum.ServiceDefaults.Extensions
Assembly: Momentum.ServiceDefaults.dll

Provides extension methods for reflection operations on types, with special support for C# records and init-only properties.

csharp
public static class TypeExtensions

Inheritance

objectTypeExtensions

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Remarks

TypeExtensions Detailed Documentation

This file contains detailed documentation for the TypeExtensions class and its methods.

Class Overview

This class contains utilities specifically designed to work with modern C# language features including records, init-only properties, and primary constructors. These extensions are particularly useful for framework code that needs to discover attributes and metadata from both traditional properties and record parameters.

Key scenarios supported:

  • Attribute discovery on record parameters and their corresponding properties
  • Primary constructor detection for records and classes
  • Init-only property identification for immutable data patterns
  • Reflection-based metadata extraction for validation and serialization

GetPropertiesWithAttribute Method

This method is essential for working with C# records where attributes can be applied to constructor parameters and are not automatically inherited by the generated properties. It searches both the property itself and the matching constructor parameter.

The method handles the following scenarios:

  • Traditional properties with attributes directly applied
  • Record properties where attributes are applied to constructor parameters
  • Mixed scenarios where some properties have direct attributes and others have parameter attributes
  • Classes with primary constructors (C# 12+ feature)

This functionality is commonly used by validation frameworks, serializers, and other reflection-based libraries that need to discover metadata consistently across different property declaration styles.

GetCustomAttribute Method

This method implements a two-stage attribute lookup strategy:

  1. First, check if the attribute is directly applied to the property
  2. If not found and a primary constructor is provided, check the corresponding constructor parameter

This approach is necessary because in C# records, attributes applied to constructor parameters are not automatically inherited by the generated properties. This method provides a unified way to retrieve attributes regardless of where they were originally declared.

The method matches constructor parameters to properties by name and type, ensuring that the correct parameter attribute is associated with the corresponding property.

GetPrimaryConstructor Method

This method implements a heuristic approach to identify primary constructors by:

  1. Finding all init-only properties on the type
  2. Locating non-compiler-generated constructors
  3. Matching constructor parameters to init-only properties by name and type
  4. Returning the constructor where all parameters have corresponding init-only properties

This approach is particularly effective for:

  • C# records (which always have primary constructors)
  • Classes with primary constructors (C# 12+ feature)
  • Immutable classes following init-only property patterns
  • DTOs and value objects designed for immutability

Important: This method relies on naming conventions and property patterns. It may not work correctly for types with multiple constructors that could match the heuristics, or for types that don't follow standard immutable object patterns.

IsInitOnly Method

This method detects init-only properties by examining the property's setter method for the presence of the IsExternalInit modifier. Init-only properties can only be set during object initialization (in constructors, initializers, or with statements).

Init-only properties are commonly used in:

  • Immutable data transfer objects (DTOs)
  • Value objects and entity models
  • Configuration objects
  • Record types (which use init-only properties by default)
  • API request/response models

This information is useful for framework code that needs to distinguish between mutable and immutable properties, such as serializers, validators, and ORM mapping.

Methods

GetCustomAttribute<TAttribute>(PropertyInfo, ConstructorInfo?)

Gets a custom attribute from a property, with fallback to the corresponding constructor parameter for records.

csharp
public static TAttribute? GetCustomAttribute<TAttribute>(this PropertyInfo propertyInfo, ConstructorInfo? primaryConstructor) where TAttribute : Attribute

Parameters

propertyInfo PropertyInfo

The property to examine for the attribute.

primaryConstructor ConstructorInfo?

The primary constructor to check for parameter attributes, if the property doesn't have the attribute directly.

Returns

TAttribute?

The attribute instance if found on the property or corresponding constructor parameter; otherwise, null.

Type Parameters

TAttribute

The type of attribute to retrieve.

Examples

Remarks

TypeExtensions Detailed Documentation

This file contains detailed documentation for the TypeExtensions class and its methods.

Class Overview

This class contains utilities specifically designed to work with modern C# language features including records, init-only properties, and primary constructors. These extensions are particularly useful for framework code that needs to discover attributes and metadata from both traditional properties and record parameters.

Key scenarios supported:

  • Attribute discovery on record parameters and their corresponding properties
  • Primary constructor detection for records and classes
  • Init-only property identification for immutable data patterns
  • Reflection-based metadata extraction for validation and serialization

GetPropertiesWithAttribute Method

This method is essential for working with C# records where attributes can be applied to constructor parameters and are not automatically inherited by the generated properties. It searches both the property itself and the matching constructor parameter.

The method handles the following scenarios:

  • Traditional properties with attributes directly applied
  • Record properties where attributes are applied to constructor parameters
  • Mixed scenarios where some properties have direct attributes and others have parameter attributes
  • Classes with primary constructors (C# 12+ feature)

This functionality is commonly used by validation frameworks, serializers, and other reflection-based libraries that need to discover metadata consistently across different property declaration styles.

GetCustomAttribute Method

This method implements a two-stage attribute lookup strategy:

  1. First, check if the attribute is directly applied to the property
  2. If not found and a primary constructor is provided, check the corresponding constructor parameter

This approach is necessary because in C# records, attributes applied to constructor parameters are not automatically inherited by the generated properties. This method provides a unified way to retrieve attributes regardless of where they were originally declared.

The method matches constructor parameters to properties by name and type, ensuring that the correct parameter attribute is associated with the corresponding property.

GetPrimaryConstructor Method

This method implements a heuristic approach to identify primary constructors by:

  1. Finding all init-only properties on the type
  2. Locating non-compiler-generated constructors
  3. Matching constructor parameters to init-only properties by name and type
  4. Returning the constructor where all parameters have corresponding init-only properties

This approach is particularly effective for:

  • C# records (which always have primary constructors)
  • Classes with primary constructors (C# 12+ feature)
  • Immutable classes following init-only property patterns
  • DTOs and value objects designed for immutability

Important: This method relies on naming conventions and property patterns. It may not work correctly for types with multiple constructors that could match the heuristics, or for types that don't follow standard immutable object patterns.

IsInitOnly Method

This method detects init-only properties by examining the property's setter method for the presence of the IsExternalInit modifier. Init-only properties can only be set during object initialization (in constructors, initializers, or with statements).

Init-only properties are commonly used in:

  • Immutable data transfer objects (DTOs)
  • Value objects and entity models
  • Configuration objects
  • Record types (which use init-only properties by default)
  • API request/response models

This information is useful for framework code that needs to distinguish between mutable and immutable properties, such as serializers, validators, and ORM mapping.

GetPrimaryConstructor(Type)

Finds the primary constructor of a type using reflection heuristics optimized for records and primary constructor patterns.

csharp
public static ConstructorInfo? GetPrimaryConstructor(this Type type)

Parameters

type Type

The type to examine for a primary constructor.

Returns

ConstructorInfo?

The for the primary constructor if found; otherwise, null if no primary constructor pattern is detected.

Examples

Remarks

TypeExtensions Detailed Documentation

This file contains detailed documentation for the TypeExtensions class and its methods.

Class Overview

This class contains utilities specifically designed to work with modern C# language features including records, init-only properties, and primary constructors. These extensions are particularly useful for framework code that needs to discover attributes and metadata from both traditional properties and record parameters.

Key scenarios supported:

  • Attribute discovery on record parameters and their corresponding properties
  • Primary constructor detection for records and classes
  • Init-only property identification for immutable data patterns
  • Reflection-based metadata extraction for validation and serialization

GetPropertiesWithAttribute Method

This method is essential for working with C# records where attributes can be applied to constructor parameters and are not automatically inherited by the generated properties. It searches both the property itself and the matching constructor parameter.

The method handles the following scenarios:

  • Traditional properties with attributes directly applied
  • Record properties where attributes are applied to constructor parameters
  • Mixed scenarios where some properties have direct attributes and others have parameter attributes
  • Classes with primary constructors (C# 12+ feature)

This functionality is commonly used by validation frameworks, serializers, and other reflection-based libraries that need to discover metadata consistently across different property declaration styles.

GetCustomAttribute Method

This method implements a two-stage attribute lookup strategy:

  1. First, check if the attribute is directly applied to the property
  2. If not found and a primary constructor is provided, check the corresponding constructor parameter

This approach is necessary because in C# records, attributes applied to constructor parameters are not automatically inherited by the generated properties. This method provides a unified way to retrieve attributes regardless of where they were originally declared.

The method matches constructor parameters to properties by name and type, ensuring that the correct parameter attribute is associated with the corresponding property.

GetPrimaryConstructor Method

This method implements a heuristic approach to identify primary constructors by:

  1. Finding all init-only properties on the type
  2. Locating non-compiler-generated constructors
  3. Matching constructor parameters to init-only properties by name and type
  4. Returning the constructor where all parameters have corresponding init-only properties

This approach is particularly effective for:

  • C# records (which always have primary constructors)
  • Classes with primary constructors (C# 12+ feature)
  • Immutable classes following init-only property patterns
  • DTOs and value objects designed for immutability

Important: This method relies on naming conventions and property patterns. It may not work correctly for types with multiple constructors that could match the heuristics, or for types that don't follow standard immutable object patterns.

IsInitOnly Method

This method detects init-only properties by examining the property's setter method for the presence of the IsExternalInit modifier. Init-only properties can only be set during object initialization (in constructors, initializers, or with statements).

Init-only properties are commonly used in:

  • Immutable data transfer objects (DTOs)
  • Value objects and entity models
  • Configuration objects
  • Record types (which use init-only properties by default)
  • API request/response models

This information is useful for framework code that needs to distinguish between mutable and immutable properties, such as serializers, validators, and ORM mapping.

GetPropertiesWithAttribute<TAttribute>(Type)

Gets all properties that have a specified attribute, either directly on the property or on the corresponding parameter of the type's primary constructor (for records).

csharp
public static IReadOnlySet<PropertyInfo> GetPropertiesWithAttribute<TAttribute>(this Type type) where TAttribute : Attribute

Parameters

type Type

The type to examine for attributed properties.

Returns

IReadOnlySet<PropertyInfo>

A read-only set of properties that have the specified attribute, either directly applied to the property or to the corresponding constructor parameter.

Type Parameters

TAttribute

The type of attribute to search for.

Examples

Remarks

TypeExtensions Detailed Documentation

This file contains detailed documentation for the TypeExtensions class and its methods.

Class Overview

This class contains utilities specifically designed to work with modern C# language features including records, init-only properties, and primary constructors. These extensions are particularly useful for framework code that needs to discover attributes and metadata from both traditional properties and record parameters.

Key scenarios supported:

  • Attribute discovery on record parameters and their corresponding properties
  • Primary constructor detection for records and classes
  • Init-only property identification for immutable data patterns
  • Reflection-based metadata extraction for validation and serialization

GetPropertiesWithAttribute Method

This method is essential for working with C# records where attributes can be applied to constructor parameters and are not automatically inherited by the generated properties. It searches both the property itself and the matching constructor parameter.

The method handles the following scenarios:

  • Traditional properties with attributes directly applied
  • Record properties where attributes are applied to constructor parameters
  • Mixed scenarios where some properties have direct attributes and others have parameter attributes
  • Classes with primary constructors (C# 12+ feature)

This functionality is commonly used by validation frameworks, serializers, and other reflection-based libraries that need to discover metadata consistently across different property declaration styles.

GetCustomAttribute Method

This method implements a two-stage attribute lookup strategy:

  1. First, check if the attribute is directly applied to the property
  2. If not found and a primary constructor is provided, check the corresponding constructor parameter

This approach is necessary because in C# records, attributes applied to constructor parameters are not automatically inherited by the generated properties. This method provides a unified way to retrieve attributes regardless of where they were originally declared.

The method matches constructor parameters to properties by name and type, ensuring that the correct parameter attribute is associated with the corresponding property.

GetPrimaryConstructor Method

This method implements a heuristic approach to identify primary constructors by:

  1. Finding all init-only properties on the type
  2. Locating non-compiler-generated constructors
  3. Matching constructor parameters to init-only properties by name and type
  4. Returning the constructor where all parameters have corresponding init-only properties

This approach is particularly effective for:

  • C# records (which always have primary constructors)
  • Classes with primary constructors (C# 12+ feature)
  • Immutable classes following init-only property patterns
  • DTOs and value objects designed for immutability

Important: This method relies on naming conventions and property patterns. It may not work correctly for types with multiple constructors that could match the heuristics, or for types that don't follow standard immutable object patterns.

IsInitOnly Method

This method detects init-only properties by examining the property's setter method for the presence of the IsExternalInit modifier. Init-only properties can only be set during object initialization (in constructors, initializers, or with statements).

Init-only properties are commonly used in:

  • Immutable data transfer objects (DTOs)
  • Value objects and entity models
  • Configuration objects
  • Record types (which use init-only properties by default)
  • API request/response models

This information is useful for framework code that needs to distinguish between mutable and immutable properties, such as serializers, validators, and ORM mapping.

IsInitOnly(PropertyInfo)

Determines whether a property is declared with the init-only setter (C# 9+ feature).

csharp
public static bool IsInitOnly(this PropertyInfo property)

Parameters

property PropertyInfo

The property to examine.

Returns

bool

true if the property has an init-only setter; otherwise, false.

Examples

Remarks

TypeExtensions Detailed Documentation

This file contains detailed documentation for the TypeExtensions class and its methods.

Class Overview

This class contains utilities specifically designed to work with modern C# language features including records, init-only properties, and primary constructors. These extensions are particularly useful for framework code that needs to discover attributes and metadata from both traditional properties and record parameters.

Key scenarios supported:

  • Attribute discovery on record parameters and their corresponding properties
  • Primary constructor detection for records and classes
  • Init-only property identification for immutable data patterns
  • Reflection-based metadata extraction for validation and serialization

GetPropertiesWithAttribute Method

This method is essential for working with C# records where attributes can be applied to constructor parameters and are not automatically inherited by the generated properties. It searches both the property itself and the matching constructor parameter.

The method handles the following scenarios:

  • Traditional properties with attributes directly applied
  • Record properties where attributes are applied to constructor parameters
  • Mixed scenarios where some properties have direct attributes and others have parameter attributes
  • Classes with primary constructors (C# 12+ feature)

This functionality is commonly used by validation frameworks, serializers, and other reflection-based libraries that need to discover metadata consistently across different property declaration styles.

GetCustomAttribute Method

This method implements a two-stage attribute lookup strategy:

  1. First, check if the attribute is directly applied to the property
  2. If not found and a primary constructor is provided, check the corresponding constructor parameter

This approach is necessary because in C# records, attributes applied to constructor parameters are not automatically inherited by the generated properties. This method provides a unified way to retrieve attributes regardless of where they were originally declared.

The method matches constructor parameters to properties by name and type, ensuring that the correct parameter attribute is associated with the corresponding property.

GetPrimaryConstructor Method

This method implements a heuristic approach to identify primary constructors by:

  1. Finding all init-only properties on the type
  2. Locating non-compiler-generated constructors
  3. Matching constructor parameters to init-only properties by name and type
  4. Returning the constructor where all parameters have corresponding init-only properties

This approach is particularly effective for:

  • C# records (which always have primary constructors)
  • Classes with primary constructors (C# 12+ feature)
  • Immutable classes following init-only property patterns
  • DTOs and value objects designed for immutability

Important: This method relies on naming conventions and property patterns. It may not work correctly for types with multiple constructors that could match the heuristics, or for types that don't follow standard immutable object patterns.

IsInitOnly Method

This method detects init-only properties by examining the property's setter method for the presence of the IsExternalInit modifier. Init-only properties can only be set during object initialization (in constructors, initializers, or with statements).

Init-only properties are commonly used in:

  • Immutable data transfer objects (DTOs)
  • Value objects and entity models
  • Configuration objects
  • Record types (which use init-only properties by default)
  • API request/response models

This information is useful for framework code that needs to distinguish between mutable and immutable properties, such as serializers, validators, and ORM mapping.