Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppPrevious);$(NetCoreAppMinimum);netstandard2.1;netstandard2.0</TargetFrameworks>
<UseCompilerGeneratedDocXmlFile>false</UseCompilerGeneratedDocXmlFile>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<IsPackable>true</IsPackable>
<AddNETFrameworkPlaceholderFileToPackage>true</AddNETFrameworkPlaceholderFileToPackage>
<AddNETFrameworkAssemblyReferenceToPackage>true</AddNETFrameworkAssemblyReferenceToPackage>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ internal sealed class IdentityReflectionContext : ReflectionContext
public override TypeInfo MapType(TypeInfo type) { return type; }
}

/// <summary>
/// Represents a customizable reflection context.
/// </summary>
/// <remarks>
/// For more information about this API, see <see href="https://github.com/dotnet/docs/raw/main/docs/fundamentals/runtime-libraries/system-reflection-context-customreflectioncontext.md">CustomReflectionContext</see>.
/// </remarks>
public abstract partial class CustomReflectionContext : ReflectionContext
{
private readonly ReflectionContextProjector _projector;

/// <summary>
/// Initializes a new instance of the <see cref="CustomReflectionContext"/> class.
/// </summary>
protected CustomReflectionContext() : this(new IdentityReflectionContext()) { }

/// <summary>
/// Initializes a new instance of the <see cref="CustomReflectionContext"/> class with the specified reflection context as a base.
/// </summary>
/// <param name="source">The reflection context to use as a base.</param>
protected CustomReflectionContext(ReflectionContext source)
{
ArgumentNullException.ThrowIfNull(source);
Expand All @@ -28,37 +41,78 @@ protected CustomReflectionContext(ReflectionContext source)
_projector = new ReflectionContextProjector(this);
}

/// <summary>
/// Gets the representation, in this reflection context, of an assembly that is represented by an object from another reflection context.
/// </summary>
/// <param name="assembly">The external representation of the assembly to represent in this context.</param>
/// <returns>The representation of the assembly in this reflection context.</returns>
public override Assembly MapAssembly(Assembly assembly)
{
ArgumentNullException.ThrowIfNull(assembly);

return _projector.ProjectAssemblyIfNeeded(assembly);
}

/// <summary>
/// Gets the representation, in this reflection context, of a type represented by an object from another reflection context.
/// </summary>
/// <param name="type">The external representation of the type to represent in this context.</param>
/// <returns>The representation of the type in this reflection context.</returns>
public override TypeInfo MapType(TypeInfo type)
{
ArgumentNullException.ThrowIfNull(type);

return _projector.ProjectTypeIfNeeded(type);
}

/// <summary>
/// When overridden in a derived class, provides a list of custom attributes for the specified member, as represented in this reflection context.
/// </summary>
/// <param name="member">The member whose custom attributes will be returned.</param>
/// <param name="declaredAttributes">A collection of the member's attributes in its current context.</param>
/// <returns>A collection that represents the custom attributes of the specified member in this reflection context.</returns>
protected virtual IEnumerable<object> GetCustomAttributes(MemberInfo member, IEnumerable<object> declaredAttributes)
{
return declaredAttributes;
}

/// <summary>
/// When overridden in a derived class, provides a list of custom attributes for the specified parameter, as represented in this reflection context.
/// </summary>
/// <param name="parameter">The parameter whose custom attributes will be returned.</param>
/// <param name="declaredAttributes">A collection of the parameter's attributes in its current context.</param>
/// <returns>A collection that represents the custom attributes of the specified parameter in this reflection context.</returns>
protected virtual IEnumerable<object> GetCustomAttributes(ParameterInfo parameter, IEnumerable<object> declaredAttributes)
{
return declaredAttributes;
}

// The default implementation of GetProperties: just return an empty list.
/// <summary>
/// When overridden in a derived class, provides a collection of additional properties for the specified type, as represented in this reflection context.
/// </summary>
/// <param name="type">The type to add properties to.</param>
/// <returns>A collection of additional properties for the specified type.</returns>
/// <remarks>
/// Override this method to specify which properties should be added to a given type. To create the properties, use the <see cref="CreateProperty(Type, string, Func{object, object?}?, Action{object, object?}?)"/> method.
/// </remarks>
protected virtual IEnumerable<PropertyInfo> AddProperties(Type type)
{
// return an empty enumeration
yield break;
}

/// <summary>
/// Creates an object that represents a property to be added to a type, to be used with the <see cref="AddProperties(Type)"/> method.
/// </summary>
/// <param name="propertyType">The type of the property to create.</param>
/// <param name="name">The name of the property to create.</param>
/// <param name="getter">An object that represents the property's <see langword="get"/> accessor.</param>
/// <param name="setter">An object that represents the property's <see langword="set"/> accessor.</param>
/// <returns>An object that represents the property.</returns>
/// <remarks>
/// Objects that are returned by this method are not complete <see cref="PropertyInfo"/> objects, and should be used only in the context of the <see cref="AddProperties(Type)"/> method.
/// </remarks>
protected PropertyInfo CreateProperty(
Type propertyType,
string name,
Expand All @@ -76,6 +130,20 @@ protected PropertyInfo CreateProperty(
this);
}

/// <summary>
/// Creates an object that represents a property to be added to a type, to be used with the <see cref="AddProperties(Type)"/> method and using the specified custom attributes.
/// </summary>
/// <param name="propertyType">The type of the property to create.</param>
/// <param name="name">The name of the property to create.</param>
/// <param name="getter">An object that represents the property's <see langword="get"/> accessor.</param>
/// <param name="setter">An object that represents the property's <see langword="set"/> accessor.</param>
/// <param name="propertyCustomAttributes">A collection of custom attributes to apply to the property.</param>
/// <param name="getterCustomAttributes">A collection of custom attributes to apply to the property's <see langword="get"/> accessor.</param>
/// <param name="setterCustomAttributes">A collection of custom attributes to apply to the property's <see langword="set"/> accessor.</param>
/// <returns>An object that represents the property.</returns>
/// <remarks>
/// Objects that are returned by this method are not complete <see cref="PropertyInfo"/> objects, and should be used only in the context of the <see cref="AddProperties(Type)"/> method.
/// </remarks>
protected PropertyInfo CreateProperty(
Type propertyType,
string name,
Expand Down
Loading