Open sandboxFocus

Method IntroduceExtensionBlock

IntroduceExtensionBlock(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
IIntroductionAdviceResult<IExtensionBlock> IntroduceExtensionBlock(INamedType targetStaticClass, IType receiverType, string? receiverParameterName = null, Action<IExtensionBlockBuilder>? buildExtensionBlock = null)
Parameters
Type Name Description
INamedType targetStaticClass

The static class into which the extension block must be introduced. Only static classes can contain extension blocks.

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(INamedType, Type, 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 a Type). Requires C# 14+ and Roslyn 5.0+.

Declaration
IIntroductionAdviceResult<IExtensionBlock> IntroduceExtensionBlock(INamedType targetStaticClass, Type receiverType, string? receiverParameterName = null, Action<IExtensionBlockBuilder>? buildExtensionBlock = null)
Parameters
Type Name Description
INamedType targetStaticClass

The static class into which the extension block must be introduced.

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>