Table of Contents

Class GenerateShapeForAttribute

Namespace
PolyType
Assembly
PolyType.dll

Instructs the PolyType source generator to include types in the ITypeShapeProvider that it generates.

[AttributeUsage(AttributeTargets.Class|AttributeTargets.Struct, AllowMultiple = true, Inherited = false)]
[Conditional("NEVER")]
public sealed class GenerateShapeForAttribute : Attribute
Inheritance
GenerateShapeForAttribute
Inherited Members

Remarks

The source generator will include a static property in the annotated class pointing to the ITypeShapeProvider that was generated for the entire project.

For projects targeting .NET 10 or later, this additionally augments the class with an implementation of IShapeable for the generated types.

Constructors

GenerateShapeForAttribute(string)

Initializes a new instance of the GenerateShapeForAttribute class with a glob pattern.

public GenerateShapeForAttribute(string typeNamePattern)

Parameters

typeNamePattern string

Glob pattern matching fully qualified type names.

Remarks

Patterns support wildcards: '' matches any sequence of characters, '?' matches a single character. Patterns without wildcard characters are matched exactly against fully qualified type names. Examples: "MyNamespace.", ".Dtos.", "MyNamespace.Person*", "MyNamespace.ExactTypeName". Use multiple attribute declarations to specify multiple patterns.

GenerateShapeForAttribute(Type)

Initializes a new instance of the GenerateShapeForAttribute class with a specific type.

public GenerateShapeForAttribute(Type target)

Parameters

target Type

The type for which shape metadata will be generated. This must not be an open-generic type.

Properties

IncludeMethods

Gets the binding flags that determine what method or event shapes should be included in the type shape.

public MethodShapeFlags IncludeMethods { get; init; }

Property Value

MethodShapeFlags

Remarks

This property controls how the Methods and Events collections will be populated. If left unspecified, only methods annotated with MethodShapeAttribute or EventShapeAttribute will be included.

This type can only be used to control inclusion of public methods in the shape models. Non-public methods can only be included individually via explicit attribute annotations.

InferClosedTypePolymorphism

Gets a value indicating whether compiler-provided closed-class metadata should be used to infer a polymorphic type hierarchy.

public bool InferClosedTypePolymorphism { get; init; }

Property Value

bool

The default value is false.

Remarks

When enabled, the annotated type must be a C# closed class and its shape is a union, including when the hierarchy contains no cases. Closed subclasses are expanded recursively; only terminal, non-closed subclasses are registered. Configuration on intermediate closed subclasses applies to their own shapes and does not affect inference for the annotated type.

Explicit DerivedTypeShapeAttribute registrations take precedence over KnownTypeAttribute registrations, which take precedence over inference. Inferred cases preserve compiler metadata order and receive implicit tags. Those tags are not stable across changes to the hierarchy.

This setting cannot be combined with a Marshaler or an explicitly requested Kind other than Union.

Kind

Gets the kind that should be generated for the annotated type.

public TypeShapeKind Kind { get; init; }

Property Value

TypeShapeKind

Remarks

Passing None will result in the generation of an IObjectTypeShape that does not contain any properties or constructors.

Exceptions

ArgumentOutOfRangeException

The specified value is not a valid TypeShapeKind.

Marshaler

Gets a type implementing an IMarshaler<TSource, TTarget> to a surrogate type.

public Type? Marshaler { get; init; }

Property Value

Type

Remarks

The type should have a parameterless constructor and must implement IMarshaler<TSource, TTarget> where either of the two generic types should match the annotated type.

Types that specify a Marshaler will be of shape ISurrogateTypeShape.

Target

Gets the target type being generated, or null if a pattern is used.

public Type? Target { get; }

Property Value

Type

TypeNamePattern

Gets the type name pattern to match, or null if a specific type is used.

public string? TypeNamePattern { get; }

Property Value

string

See Also