Skip to content
Merged
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
31 changes: 29 additions & 2 deletions xml/System.Reflection.Context/CustomReflectionContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,35 @@
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">Supplemental API remarks for CustomReflectionContext</see>.
</remarks>
<format type="text/markdown"><![CDATA[

## Remarks
<xref:System.Reflection.Context.CustomReflectionContext> provides a way for you to add or remove custom attributes from reflection objects, or add dummy properties to those objects, without re-implementing the complete reflection model. The default <xref:System.Reflection.Context.CustomReflectionContext> simply wraps reflection objects without making any changes, but by subclassing and overriding the relevant methods, you can add, remove, or change the attributes that apply to any reflected parameter or member, or add new properties to a reflected type.

For example, suppose that your code follows the convention of applying a particular attribute to factory methods, but you are now required to work with third-party code that lacks attributes. You can use <xref:System.Reflection.Context.CustomReflectionContext> to specify a rule for identifying the objects that should have attributes and to supply the objects with those attributes when they are viewed from your code.

To use <xref:System.Reflection.Context.CustomReflectionContext> effectively, the code that uses the reflected objects must support the notion of specifying a reflection context, instead of assuming that all reflected objects are associated with the runtime reflection context. Many reflection methods in .NET provide a <xref:System.Reflection.ReflectionContext> parameter for this purpose.

To modify the attributes that are applied to a reflected parameter or member, override the <xref:System.Reflection.Context.CustomReflectionContext.GetCustomAttributes(System.Reflection.ParameterInfo,System.Collections.Generic.IEnumerable{System.Object})> or <xref:System.Reflection.Context.CustomReflectionContext.GetCustomAttributes(System.Reflection.MemberInfo,System.Collections.Generic.IEnumerable{System.Object})> method. These methods take the reflected object and the list of attributes under its current reflection context, and return the list of attributes it should have under the custom reflection context.

> [!WARNING]
> <xref:System.Reflection.Context.CustomReflectionContext> methods should not access the list of attributes of a reflected object or method directly by calling the <xref:System.Reflection.MemberInfo.GetCustomAttributes*> method on the provided <xref:System.Reflection.MemberInfo> or <xref:System.Reflection.ParameterInfo> instance, but should instead use the `declaredAttributes` list, which is passed as a parameter to the <xref:System.Reflection.Context.CustomReflectionContext.GetCustomAttributes*> method overloads.

To add properties to a reflected type, override the <xref:System.Reflection.Context.CustomReflectionContext.AddProperties*> method. The method accepts a parameter that specifies the reflected type, and returns a list of additional properties. You should use the <xref:System.Reflection.Context.CustomReflectionContext.CreateProperty*> method to create property objects to return. You can specify delegates when creating the property that will serve as the property accessor, and you can omit one of the accessors to create a read-only or write-only property. Note that such dummy properties have no metadata or Common Intermediate Language (CIL) backing.

> [!WARNING]
> Be cautious about equality among reflected objects when you work with reflection contexts, because objects may represent the same reflected object in multiple contexts. You can use the <xref:System.Reflection.Context.CustomReflectionContext.MapType*> method to obtain a particular reflection context's version of a reflected object.

> [!WARNING]
> A <xref:System.Reflection.Context.CustomReflectionContext> object alters the attributes returned by a particular reflection object, such as those obtained by the <xref:System.Reflection.MemberInfo.GetCustomAttributes*> method. It does not alter the custom attribute data returned by the <xref:System.Reflection.MemberInfo.GetCustomAttributesData*> method, and these two lists will not match when you use a custom reflection context.

## Examples
The following example demonstrates how to subclass <xref:System.Reflection.Context.CustomReflectionContext> to add a custom attribute to all the members of a given type whose names begin with "To". To run this code, paste it into an empty console project, and make sure to include a reference to System.Reflection.Context.dll.

:::code language="csharp" source="~/snippets/csharp/System.Reflection.Context/CustomReflectionContext/Overview/program.cs" id="Snippet1":::

Comment thread
gewarren marked this conversation as resolved.
]]></format>
</remarks>
</Docs>
<Members>
<MemberGroup MemberName=".ctor">
Expand Down
28 changes: 7 additions & 21 deletions xml/System.Reflection.Emit/AssemblyBuilder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,9 +386,8 @@ The following code example shows how to define and use a dynamic assembly. The e
<format type="text/markdown"><![CDATA[

## Remarks
.NET Core and .NET 5+ only: [Multi-module assemblies are not supported](/dotnet/core/porting/net-framework-tech-unavailable#loading-multi-module-assemblies). Only one dynamic module may be defined in an assembly.

.NET Framework only: It is an error to define multiple dynamic modules with the same name in an assembly.
[Multi-module assemblies are not supported](/dotnet/core/porting/net-framework-tech-unavailable#loading-multi-module-assemblies). Only one dynamic module can be defined in an assembly.

The defined dynamic module is transient. The dynamic module is not saved, even if the parent dynamic assembly was created with <xref:System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave>.

Expand All @@ -413,15 +412,10 @@ The following code example shows how to define and use a dynamic assembly. The e

-or-

The length of <paramref name="name" /> is greater than the system-defined maximum length.

-or-

.NET Framework only: A module with the same <paramref name="name" /> has already been defined in this assembly.
</exception>
The length of <paramref name="name" /> is greater than the system-defined maximum length.</exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="name" /> is <see langword="null" />.</exception>
<exception cref="T:System.InvalidOperationException">.NET Core and .NET 5+ only: A dynamic module has already been defined in this assembly.</exception>
<exception cref="T:System.InvalidOperationException">A dynamic module has already been defined in this assembly.</exception>
<exception cref="T:System.Security.SecurityException">The caller does not have the required permission.</exception>
<exception cref="T:System.ExecutionEngineException">The assembly for default symbol writer cannot be loaded.

Expand Down Expand Up @@ -458,7 +452,7 @@ The following code example shows how to define and use a dynamic assembly. The e
<param name="name">The name of the dynamic module.</param>
<summary>When overridden in a derived class, defines a dynamic module in this assembly.</summary>
<returns>A <see cref="T:System.Reflection.Emit.ModuleBuilder" /> representing the defined dynamic module.</returns>
<remarks>.NET Core and .NET 5+ support only one module per assembly.</remarks>
<remarks>Only one module per assembly is supported.</remarks>
</Docs>
</Member>
<MemberGroup MemberName="DefineResource">
Expand Down Expand Up @@ -870,7 +864,7 @@ The following code example shows how to define and use a dynamic assembly. The e
<param name="name">The name of the requested dynamic module.</param>
<summary>When overridden in a derived class, returns the dynamic module with the specified name.</summary>
<returns>A <see cref="T:System.Reflection.Emit.ModuleBuilder" /> representing the requested dynamic module.</returns>
<remarks>.NET Core and .NET 5+ support only one module per assembly..</remarks>
<remarks>Only one module per assembly is supported.</remarks>
</Docs>
</Member>
<Member MemberName="GetExportedTypes">
Expand Down Expand Up @@ -1524,8 +1518,6 @@ The following code example shows how to define and use a dynamic assembly. The e
## Remarks
This method does not return a complete list of referenced assemblies. For example, if you apply a custom attribute to the <xref:System.Reflection.Emit.AssemblyBuilder>, the assembly in which the attribute was defined is included in the list returned by this method. However, if you use a <xref:System.Type> object to specify the type of a method parameter, that type is not included.

To get a complete list of referenced assemblies, save the assembly, load it into another application domain, and call the <xref:System.Reflection.Assembly.GetReferencedAssemblies*?displayProperty=nameWithType> method.

]]></format>
</remarks>
</Docs>
Expand Down Expand Up @@ -1870,14 +1862,8 @@ The following code example shows how to define and use a dynamic assembly. The e
<summary>Gets a value that indicates whether this dynamic assembly is held in a collectible <see cref="T:System.Runtime.Loader.AssemblyLoadContext" />.</summary>
<value>
<see langword="true" /> if this dynamic assembly is held in a collectible <see cref="T:System.Runtime.Loader.AssemblyLoadContext" />; otherwise, <see langword="false" />.</value>
<remarks>
<format type="text/markdown"><![CDATA[

## Remarks
For more information, see [How to use and debug assembly unloadability in .NET Core](/dotnet/standard/assembly/unloadability).

]]></format>
</remarks>
<remarks>To be added.</remarks>
<related type="Article" href="/dotnet/standard/assembly/unloadability" title="How to use and debug assembly unloadability" />
</Docs>
</Member>
<Member MemberName="IsDefined">
Expand Down
2 changes: 1 addition & 1 deletion xml/System.Reflection.Emit/AssemblyBuilderAccess.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</Attributes>
<Docs>
<summary>Defines the access modes for a dynamic assembly.</summary>
<remarks>This enumeration defines only two fields (<see cref="F:System.Reflection.Emit.AssemblyBuilderAccess.Run" /> and <see cref="F:System.Reflection.Emit.AssemblyBuilderAccess.RunAndCollect" />) in .NET. In .NET Framework, it defines an additional three fields (<see cref="F:System.Reflection.Emit.AssemblyBuilderAccess.ReflectionOnly" />, <see cref="F:System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave" />, and <see cref="F:System.Reflection.Emit.AssemblyBuilderAccess.Save" />). For more information, see <see href="/dotnet/fundamentals/runtime-libraries/system-reflection-emit-assemblybuilder">AssemblyBuilder class remarks</see>.</remarks>
<remarks>For more information about this enumeration, see <see href="/dotnet/fundamentals/runtime-libraries/system-reflection-emit-assemblybuilder">AssemblyBuilder class remarks</see>.</remarks>
<related type="Article" href="/dotnet/fundamentals/runtime-libraries/system-reflection-emit-assemblybuilder" />
</Docs>
<Members>
Expand Down
8 changes: 1 addition & 7 deletions xml/System.Reflection.Emit/ConstructorBuilder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,7 @@
<format type="text/markdown"><![CDATA[

## Remarks
This property is not supported until after the <xref:System.Reflection.Emit.TypeBuilder.CreateType*?displayProperty=nameWithType> method has been called. In the .NET Framework versions 1.0 and 1.1, <xref:System.InvalidOperationException> is thrown. In the .NET Framework version 2.0, <xref:System.NotSupportedException> is thrown.


This property is not supported until after the <xref:System.Reflection.Emit.TypeBuilder.CreateType*?displayProperty=nameWithType> method has been called.

## Examples
The code sample illustrates the use of `GetParameters`.
Expand All @@ -758,10 +756,6 @@

]]></format>
</remarks>
<exception cref="T:System.InvalidOperationException">
<see cref="M:System.Reflection.Emit.TypeBuilder.CreateType" /> has not been called on this constructor's type, in the .NET Framework versions 1.0 and 1.1.</exception>
<exception cref="T:System.NotSupportedException">
<see cref="M:System.Reflection.Emit.TypeBuilder.CreateType" /> has not been called on this constructor's type, in the .NET Framework version 2.0.</exception>
</Docs>
</Member>
<Member MemberName="InitLocals">
Expand Down
Loading
Loading