Class TypeDataModelGenerator
Provides functionality and extensibility points for generating TypeDataModel instances for a given set of ITypeSymbol inputs.
public class TypeDataModelGenerator
- Inheritance
-
TypeDataModelGenerator
- Inherited Members
Constructors
TypeDataModelGenerator(ISymbol, KnownSymbols, CancellationToken)
Creates a new TypeDataModelGenerator instance.
public TypeDataModelGenerator(ISymbol generationScope, KnownSymbols knownSymbols, CancellationToken cancellationToken)
Parameters
generationScopeISymbolThe context symbol used to determine accessibility for processed types.
knownSymbolsKnownSymbolsThe known symbols cache constructed from the current Compilation.
cancellationTokenCancellationTokenThe cancellation token to be used by the generator.
Properties
AssociatedTypes
Gets the associated types, indexed by their originating types.
public ImmutableDictionary<ITypeSymbol, ImmutableArray<AssociatedTypeModel>> AssociatedTypes { get; }
Property Value
CancellationToken
The cancellation token to be used by the generator.
public CancellationToken CancellationToken { get; }
Property Value
DefaultLocation
The default location to be used for diagnostics.
public virtual Location? DefaultLocation { get; }
Property Value
Diagnostics
Gets the list of diagnostics that have been recorded by this model generator.
public List<Diagnostic> Diagnostics { get; }
Property Value
FlattenSystemTupleTypes
Determines whether Tuple tuples should be mapped to TupleDataModel instances and flattened to a single type for tuples containing over 8 elements.
protected virtual bool FlattenSystemTupleTypes { get; }
Property Value
Remarks
Defaults to false, since C# treats Tuple types as regular classes. Set to true if there is a need to handle F# models that do have syntactic sugar support for Tuple.
GeneratedModels
Full generated models, keyed by their type symbol.
public ImmutableDictionary<ITypeSymbol, TypeDataModel> GeneratedModels { get; }
Property Value
GenerationScope
The context symbol used to determine accessibility for processed types.
public ISymbol GenerationScope { get; }
Property Value
IncludeDelegateParameters
Determines whether delegate parameters should be included in the generated model.
protected virtual bool IncludeDelegateParameters { get; }
Property Value
KnownSymbols
The known symbols cache constructed from the current Compilation.
public KnownSymbols KnownSymbols { get; }
Property Value
SymbolComparer
The SymbolEqualityComparer used to identify type symbols. Defaults to Default.
public virtual SymbolEqualityComparer SymbolComparer { get; }
Property Value
Methods
GetEffectiveReturnType(IMethodSymbol, out MethodReturnTypeKind)
Extracts the actual return type from async wrappers like Task or ValueTask, returning null if the method returns void or the non-generic Task or ValueTask.
protected virtual ITypeSymbol? GetEffectiveReturnType(IMethodSymbol method, out MethodReturnTypeKind kind)
Parameters
methodIMethodSymbolThe method from which to extract the return type.
kindMethodReturnTypeKindThe inferred type kind.
Returns
- ITypeSymbol
A type symbol representing the underlying type being returned.
GetEnumValueName(IFieldSymbol)
Gets the name of given enum value.
protected virtual string GetEnumValueName(IFieldSymbol field)
Parameters
fieldIFieldSymbolThe field symbol of the enum.
Returns
- string
The name of the enum value.
IncludeAssociatedShapes(ITypeSymbol, ImmutableArray<AssociatedTypeModel>, ref TypeDataModelGenerationContext)
Includes the specified associated types to the type graph traversal.
protected void IncludeAssociatedShapes(ITypeSymbol type, ImmutableArray<AssociatedTypeModel> associatedTypes, ref TypeDataModelGenerationContext ctx)
Parameters
typeITypeSymbolThe type from which the associated types originate.
associatedTypesImmutableArray<AssociatedTypeModel>The associated types to include.
ctxTypeDataModelGenerationContextThe type graph traversal context.
IncludeField(IFieldSymbol, out string?, out int, out bool, out bool)
Determines whether the given field should be included in the object data model.
protected virtual bool IncludeField(IFieldSymbol field, out string? customName, out int order, out bool includeGetter, out bool includeSetter)
Parameters
fieldIFieldSymbolcustomNamestringorderintincludeGetterboolincludeSetterbool
Returns
Remarks
Defaults to including public fields only.
IncludeNestedType(ITypeSymbol, ref TypeDataModelGenerationContext, TypeShapeRequirements)
Wraps the MapType(ITypeSymbol, TypeDataKind?, BindingFlags?, ImmutableArray<AssociatedTypeModel>, ref TypeDataModelGenerationContext, TypeShapeRequirements, out TypeDataModel?) method with pre- and post-processing steps necessary for a type graph traversal.
protected TypeDataModelGenerationStatus IncludeNestedType(ITypeSymbol type, ref TypeDataModelGenerationContext ctx, TypeShapeRequirements requirements = TypeShapeRequirements.Full)
Parameters
typeITypeSymbolThe type for which to generate a data model.
ctxTypeDataModelGenerationContextThe context token holding state for the current type graph traversal.
requirementsTypeShapeRequirementsThe detail to include in the shape.
Returns
- TypeDataModelGenerationStatus
The model generation status for the given type.
IncludeProperty(IPropertySymbol, out string?, out int, out bool, out bool)
Determines whether the given property should be included in the object data model.
protected virtual bool IncludeProperty(IPropertySymbol property, out string? customName, out int order, out bool includeGetter, out bool includeSetter)
Parameters
propertyIPropertySymbolcustomNamestringorderintincludeGetterboolincludeSetterbool
Returns
Remarks
Defaults to including public getters and setters only.
IncludeType(ITypeSymbol)
Attempt to generate a TypeDataModel for the given type.
public TypeDataModelGenerationStatus IncludeType(ITypeSymbol type)
Parameters
typeITypeSymbolThe type for which to generate a data model.
Returns
- TypeDataModelGenerationStatus
The generation status for the given type.
IsAccessibleSymbol(ISymbol)
Determines if the specified symbol is accessible within the GenerationScope symbol.
protected virtual bool IsAccessibleSymbol(ISymbol symbol)
Parameters
symbolISymbol
Returns
IsRequiredByPolicy(IFieldSymbol)
Determines whether a member should be considered required.
protected virtual bool? IsRequiredByPolicy(IFieldSymbol member)
Parameters
memberIFieldSymbolThe member.
Returns
- bool?
A value indicating whether the member is required.
IsRequiredByPolicy(IPropertySymbol)
Determines whether a member should be considered required.
protected virtual bool? IsRequiredByPolicy(IPropertySymbol member)
Parameters
memberIPropertySymbolThe member.
Returns
- bool?
A value indicating whether the member is required.
IsSupportedType(ITypeSymbol)
Determines if the specified symbol is supported for data model generation.
protected virtual bool IsSupportedType(ITypeSymbol type)
Parameters
typeITypeSymbol
Returns
Remarks
By default, unsupported types are void, pointers, and generic type definitions.
MapEvent(ResolvedEventSymbol, ref TypeDataModelGenerationContext, out EventDataModel)
Maps the given resolvedEvent and its parameters to a EventDataModel.
protected virtual TypeDataModelGenerationStatus MapEvent(ResolvedEventSymbol resolvedEvent, ref TypeDataModelGenerationContext ctx, out EventDataModel result)
Parameters
resolvedEventResolvedEventSymbolThe event to try to map.
ctxTypeDataModelGenerationContextThe current model generation context.
resultEventDataModelThe mapped event data model.
Returns
- TypeDataModelGenerationStatus
The result of the mapping operation.
MapEvents(ITypeSymbol, ref TypeDataModelGenerationContext, BindingFlags?)
Maps the events resolved from the current type and their parameters to an array of EventDataModel.
protected ImmutableArray<EventDataModel> MapEvents(ITypeSymbol type, ref TypeDataModelGenerationContext ctx, BindingFlags? bindingFlags)
Parameters
typeITypeSymbolThe type from which to resolve methods.
ctxTypeDataModelGenerationContextThe current model generation context.
bindingFlagsBindingFlags?The binding flags to use when resolving methods.
Returns
- ImmutableArray<EventDataModel>
An array of mapped method data models.
MapMethod(ResolvedMethodSymbol, ref TypeDataModelGenerationContext, out MethodDataModel)
Maps the given resolvedMethod and its parameters to a MethodDataModel.
protected virtual TypeDataModelGenerationStatus MapMethod(ResolvedMethodSymbol resolvedMethod, ref TypeDataModelGenerationContext ctx, out MethodDataModel result)
Parameters
resolvedMethodResolvedMethodSymbolThe method to try to map.
ctxTypeDataModelGenerationContextThe current model generation context.
resultMethodDataModelThe mapped method data model.
Returns
- TypeDataModelGenerationStatus
The result of the mapping operation.
MapMethods(ITypeSymbol, ref TypeDataModelGenerationContext, BindingFlags?)
Maps the methods resolved from the current type and their parameters to an array of MethodDataModel.
protected ImmutableArray<MethodDataModel> MapMethods(ITypeSymbol type, ref TypeDataModelGenerationContext ctx, BindingFlags? bindingFlags)
Parameters
typeITypeSymbolThe type from which to resolve methods.
ctxTypeDataModelGenerationContextThe current model generation context.
bindingFlagsBindingFlags?The binding flags to use when resolving methods.
Returns
- ImmutableArray<MethodDataModel>
An array of mapped method data models.
MapType(ITypeSymbol, TypeDataKind?, BindingFlags?, ImmutableArray<AssociatedTypeModel>, ref TypeDataModelGenerationContext, TypeShapeRequirements, out TypeDataModel?)
The core, overridable data model mapping method for a given type.
protected virtual TypeDataModelGenerationStatus MapType(ITypeSymbol type, TypeDataKind? requestedKind, BindingFlags? methodBindingFlags, ImmutableArray<AssociatedTypeModel> associatedTypes, ref TypeDataModelGenerationContext ctx, TypeShapeRequirements requirements, out TypeDataModel? model)
Parameters
typeITypeSymbolThe type for which to generate a data model.
requestedKindTypeDataKind?The target kind as specified in configuration.
methodBindingFlagsBindingFlags?The binding flags used to resolve method shapes.
associatedTypesImmutableArray<AssociatedTypeModel>Associated types for this shape.
ctxTypeDataModelGenerationContextThe context token holding state for the current type graph traversal.
requirementsTypeShapeRequirementsThe detail to include in the shape.
modelTypeDataModelThe model that the current symbol is being mapped to.
Returns
- TypeDataModelGenerationStatus
The model generation status for the given type.
Remarks
The method should only be overridden but not invoked directly. Call IncludeNestedType(ITypeSymbol, ref TypeDataModelGenerationContext, TypeShapeRequirements) instead.
NormalizeType(ITypeSymbol)
When overridden, performs normalization operations on the given type before it is processed.
protected virtual ITypeSymbol NormalizeType(ITypeSymbol type)
Parameters
typeITypeSymbol
Returns
ParseCustomAssociatedTypeAttributes(ISymbol, out ImmutableArray<AssociatedTypeModel>)
Gets the associated types for a given type, as specified by 3rd party custom attributes.
protected virtual void ParseCustomAssociatedTypeAttributes(ISymbol symbol, out ImmutableArray<AssociatedTypeModel> associatedTypes)
Parameters
symbolISymbolThe type (or property or field with a type of interest) whose associated types are sought.
associatedTypesImmutableArray<AssociatedTypeModel>The associated types for the given
symbol.
ReportDiagnostic(DiagnosticDescriptor, Location?, params object?[])
Adds a new diagnostic to the Diagnostics property.
public void ReportDiagnostic(DiagnosticDescriptor descriptor, Location? location, params object?[] messageArgs)
Parameters
descriptorDiagnosticDescriptorlocationLocationmessageArgsobject[]
ResolveConstructors(ITypeSymbol, ImmutableArray<PropertyDataModel>)
Resolves the constructor symbols that should be included for the given type.
protected virtual IEnumerable<IMethodSymbol> ResolveConstructors(ITypeSymbol type, ImmutableArray<PropertyDataModel> properties)
Parameters
typeITypeSymbolpropertiesImmutableArray<PropertyDataModel>
Returns
ResolveDerivedTypes(ITypeSymbol)
When overridden, returns the derived types of the given type.
protected virtual IEnumerable<DerivedTypeModel> ResolveDerivedTypes(ITypeSymbol type)
Parameters
typeITypeSymbolThe base type to resolve derived types from.
Returns
- IEnumerable<DerivedTypeModel>
An IEnumerable containing derived type models
ResolveEvents(ITypeSymbol, BindingFlags)
Resolves the event symbols that should be included for the given type.
protected virtual IEnumerable<ResolvedEventSymbol> ResolveEvents(ITypeSymbol type, BindingFlags bindingFlags)
Parameters
typeITypeSymbolThe declaring type from which to resolve the methods.
bindingFlagsBindingFlagsThe binding flags of the methods to include.
Returns
- IEnumerable<ResolvedEventSymbol>
A struct containing resolved event information.
ResolveMethods(ITypeSymbol, BindingFlags)
Resolves the method symbols that should be included for the given type.
protected virtual IEnumerable<ResolvedMethodSymbol> ResolveMethods(ITypeSymbol type, BindingFlags bindingFlags)
Parameters
typeITypeSymbolThe declaring type from which to resolve the methods.
bindingFlagsBindingFlagsThe binding flags of the methods to include.
Returns
- IEnumerable<ResolvedMethodSymbol>
A struct containing resolved method information.
ResolveProperties(ITypeSymbol)
Resolves the properties and fields that should be included for the given type.
protected virtual IEnumerable<ResolvedPropertySymbol> ResolveProperties(ITypeSymbol type)
Parameters
typeITypeSymbol
Returns
SkipObjectMemberResolution(INamedTypeSymbol)
Whether property or field resolution should be skipped for the given type.
protected virtual bool SkipObjectMemberResolution(INamedTypeSymbol type)
Parameters
typeINamedTypeSymbol
Returns
Remarks
Currently skipped for simple types, nullable types, and MemberInfo.