Open sandboxFocus

Method IntroduceExtensionBlock

IntroduceExtensionBlock(IAdviser<INamedType>, IType, string?, Action<IExtensionBlockBuilder>?)

Introduces a new extension block into a static class. Extension blocks allow adding extension members (methods, properties, indexers) to a type (represented as an IType). Requires C# 14+ and Roslyn 5.0+.

Declaration
public static IIntroductionAdviceResult<IExtensionBlock> IntroduceExtensionBlock(this IAdviser<INamedType> adviser, IType receiverType, string? receiverParameterName = null, Action<IExtensionBlockBuilder>? buildExtensionBlock = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for a named type. Must be a static class.

IType receiverType

The type being extended. Members introduced into this extension block will appear as members of this type.

string receiverParameterName

The name of the receiver parameter. Set to null or empty for a static extension (members appear as static members of the extended type). Set to a non-empty string (e.g., "self", "value") for an instance extension.

Action<IExtensionBlockBuilder> buildExtensionBlock

An optional callback that allows you to configure the extension block, such as adding type parameters or attributes to the receiver parameter.

Returns
Type Description
IIntroductionAdviceResult<IExtensionBlock>

An IIntroductionAdviceResult<T> representing the result of the advice. The Declaration property provides access to the introduced extension block. The result also implements IAdviser<T> and can be used to introduce members.

IntroduceExtensionBlock(IAdviser<INamedType>, Type, string?, Action<IExtensionBlockBuilder>?)

Introduces a new extension block into the target static class. Extension blocks allow adding extension members (methods, properties, indexers) to a type (represented as an Type). Requires C# 14+ and Roslyn 5.0+.

Declaration
public static IIntroductionAdviceResult<IExtensionBlock> IntroduceExtensionBlock(this IAdviser<INamedType> adviser, Type receiverType, string? receiverParameterName = null, Action<IExtensionBlockBuilder>? buildExtensionBlock = null)
Parameters
Type Name Description
IAdviser<INamedType> adviser

An adviser for the target static class.

Type receiverType

The Type being extended.

string receiverParameterName

The name of the receiver parameter. Set to null or empty for a static extension. Set to a non-empty string for an instance extension.

Action<IExtensionBlockBuilder> buildExtensionBlock

An optional callback to configure the extension block.

Returns
Type Description
IIntroductionAdviceResult<IExtensionBlock>