Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConstructorBuilder implementation, integrate TypeBuilder.GetXyz() static methods #94732

Merged
merged 13 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ internal sealed class RuntimeMethodBuilder : MethodBuilder
}
else if ((attributes & MethodAttributes.Virtual) != 0)
{
// On an interface, the rule is slighlty different
if (((attributes & MethodAttributes.Abstract) == 0))
// On an interface, the rule is slightly different
if ((attributes & MethodAttributes.Abstract) == 0)
throw new ArgumentException(SR.Arg_NoStaticVirtual);
}

Expand Down Expand Up @@ -122,7 +122,7 @@ internal sealed class RuntimeMethodBuilder : MethodBuilder
m_ubBody = null;
m_ilGenerator = null;

// Default is managed IL. Manged IL has bit flag 0x0020 set off
// Default is managed IL. Managed IL has bit flag 0x0020 set off
m_dwMethodImplFlags = MethodImplAttributes.IL;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,6 @@

namespace System.Reflection.Emit
{
public abstract partial class TypeBuilder
{
#region Public Static Methods
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static MethodInfo GetMethod(Type type, MethodInfo method)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));

// The following checks establishes invariants that more simply put require type to be generic and
// method to be a generic method definition declared on the generic type definition of type.
// To create generic method G<Foo>.M<Bar> these invariants require that G<Foo>.M<S> be created by calling
// this function followed by MakeGenericMethod on the resulting MethodInfo to finally get G<Foo>.M<Bar>.
// We could also allow G<T>.M<Bar> to be created before G<Foo>.M<Bar> (BindGenParm followed by this method)
// if we wanted to but that just complicates things so these checks are designed to prevent that scenario.

if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
throw new ArgumentException(SR.Argument_NeedGenericMethodDefinition, nameof(method));

if (method.DeclaringType == null || !method.DeclaringType.IsGenericTypeDefinition)
throw new ArgumentException(SR.Argument_MethodNeedGenericDeclaringType, nameof(method));

if (type.GetGenericTypeDefinition() != method.DeclaringType)
throw new ArgumentException(SR.Argument_InvalidMethodDeclaringType, nameof(type));

// The following converts from Type or TypeBuilder of G<T> to TypeBuilderInstantiation G<T>. These types
// both logically represent the same thing. The runtime displays a similar convention by having
// G<M>.M() be encoded by a typeSpec whose parent is the typeDef for G<M> and whose instantiation is also G<M>.
if (type.IsGenericTypeDefinition)
type = type.MakeGenericType(type.GetGenericArguments());

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));

return MethodOnTypeBuilderInstantiation.GetMethod(method, typeBuilderInstantiation);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));

if (!constructor.DeclaringType!.IsGenericTypeDefinition)
throw new ArgumentException(SR.Argument_ConstructorNeedGenericDeclaringType, nameof(constructor));

if (type.GetGenericTypeDefinition() != constructor.DeclaringType)
throw new ArgumentException(SR.Argument_InvalidConstructorDeclaringType, nameof(type));

// TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
if (type.IsGenericTypeDefinition)
type = type.MakeGenericType(type.GetGenericArguments());

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));

return ConstructorOnTypeBuilderInstantiation.GetConstructor(constructor, typeBuilderInstantiation);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static FieldInfo GetField(Type type, FieldInfo field)
{
if (type is not TypeBuilder and not TypeBuilderInstantiation)
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));

if (!field.DeclaringType!.IsGenericTypeDefinition)
throw new ArgumentException(SR.Argument_FieldNeedGenericDeclaringType, nameof(field));

if (type.GetGenericTypeDefinition() != field.DeclaringType)
throw new ArgumentException(SR.Argument_InvalidFieldDeclaringType, nameof(type));

// TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
if (type.IsGenericTypeDefinition)
type = type.MakeGenericType(type.GetGenericArguments());

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));

return FieldOnTypeBuilderInstantiation.GetField(field, typeBuilderInstantiation);
}
#endregion
}

internal sealed partial class RuntimeTypeBuilder : TypeBuilder
{
public override bool IsAssignableFrom([NotNullWhen(true)] TypeInfo? typeInfo)
Expand Down Expand Up @@ -1358,7 +1272,6 @@ private RuntimeConstructorBuilder DefineDefaultConstructorNoLock(MethodAttribute

// Define the constructor Builder
constBuilder = (RuntimeConstructorBuilder)DefineConstructor(attributes, CallingConventions.Standard, null);
m_constructorCount++;

// generate the code to call the parent's default constructor
ILGenerator il = constBuilder.GetILGenerator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\TypeBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\TypeBuilderInstantiation.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\TypeNameBuilder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\Emit\TypeBuilder.Static.cs" Condition="'$(FeatureNativeAot)' != 'true'" />
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\EventAttributes.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\EventInfo.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Reflection\ExceptionHandlingClause.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override object GetValueDirect(TypedReference obj)
throw new NotImplementedException();
}
public override RuntimeFieldHandle FieldHandle => throw new NotImplementedException();
public override Type FieldType => throw new NotImplementedException();
public override Type FieldType => _field.FieldType;
public override object GetValue(object? obj) { throw new InvalidOperationException(); }
public override void SetValue(object? obj, object? value, BindingFlags invokeAttr, Binder? binder, CultureInfo? culture) { throw new InvalidOperationException(); }
public override FieldAttributes Attributes => _field.Attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal override Type[] GetParameterTypes()
#endregion

#region MethodBase Members
public override ParameterInfo[] GetParameters() { throw new NotSupportedException(); }
public override ParameterInfo[] GetParameters() => _method.GetParameters();
public override MethodImplAttributes GetMethodImplementationFlags() { return _method.GetMethodImplementationFlags(); }
public override RuntimeMethodHandle MethodHandle => throw new NotSupportedException(SR.NotSupported_DynamicModule);
public override MethodAttributes Attributes => _method.Attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public override Type MakeArrayType(int rank)
public override int GetArrayRank()
{
if (!IsArray)
throw new NotSupportedException(SR.NotSupported_SubclassOverride);
throw new ArgumentException(SR.Argument_HasToBeArrayClass);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InvalidOperationException instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed exception type and message for similar cases

if (!arrayClass.IsArray)
{
throw new ArgumentException(SR.Argument_HasToBeArrayClass);
}


return _rank;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;

namespace System.Reflection.Emit
{
public abstract partial class TypeBuilder
{
#region Public Static Methods
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static MethodInfo GetMethod(Type type, MethodInfo method)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
{
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));
}

// The following checks establishes invariants that more simply put require type to be generic and
// method to be a generic method definition declared on the generic type definition of type.
// To create generic method G<Foo>.M<Bar> these invariants require that G<Foo>.M<S> be created by calling
// this function followed by MakeGenericMethod on the resulting MethodInfo to finally get G<Foo>.M<Bar>.
// We could also allow G<T>.M<Bar> to be created before G<Foo>.M<Bar> (BindGenParm followed by this method)
// if we wanted to but that just complicates things so these checks are designed to prevent that scenario.

if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
{
throw new ArgumentException(SR.Argument_NeedGenericMethodDefinition, nameof(method));
}

if (method.DeclaringType == null || !method.DeclaringType.IsGenericTypeDefinition)
{
throw new ArgumentException(SR.Argument_MethodNeedGenericDeclaringType, nameof(method));
}

if (type.GetGenericTypeDefinition() != method.DeclaringType)
{
throw new ArgumentException(SR.Argument_InvalidMethodDeclaringType, nameof(type));
}

// The following converts from Type or TypeBuilder of G<T> to TypeBuilderInstantiation G<T>. These types
// both logically represent the same thing. The runtime displays a similar convention by having
// G<M>.M() be encoded by a typeSpec whose parent is the typeDef for G<M> and whose instantiation is also G<M>.
if (type.IsGenericTypeDefinition)
{
type = type.MakeGenericType(type.GetGenericArguments());
}

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
{
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));
}

return MethodOnTypeBuilderInstantiation.GetMethod(method, typeBuilderInstantiation);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
{
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));
}

if (!constructor.DeclaringType!.IsGenericTypeDefinition)
{
throw new ArgumentException(SR.Argument_ConstructorNeedGenericDeclaringType, nameof(constructor));
}

if (type.GetGenericTypeDefinition() != constructor.DeclaringType)
{
throw new ArgumentException(SR.Argument_InvalidConstructorDeclaringType, nameof(type));
}

// TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
if (type.IsGenericTypeDefinition)
{
type = type.MakeGenericType(type.GetGenericArguments());
}

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
{
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));
}

return ConstructorOnTypeBuilderInstantiation.GetConstructor(constructor, typeBuilderInstantiation);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static FieldInfo GetField(Type type, FieldInfo field)
{
if (type is not TypeBuilder and not TypeBuilderInstantiation)
{
throw new ArgumentException(SR.Argument_MustBeTypeBuilder, nameof(type));
}

if (!field.DeclaringType!.IsGenericTypeDefinition)
{
throw new ArgumentException(SR.Argument_FieldNeedGenericDeclaringType, nameof(field));
}

if (type.GetGenericTypeDefinition() != field.DeclaringType)
{
throw new ArgumentException(SR.Argument_InvalidFieldDeclaringType, nameof(type));
}

// TypeBuilder G<T> ==> TypeBuilderInstantiation G<T>
if (type.IsGenericTypeDefinition)
{
type = type.MakeGenericType(type.GetGenericArguments());
}

if (type is not TypeBuilderInstantiation typeBuilderInstantiation)
{
throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(type));
}

return FieldOnTypeBuilderInstantiation.GetField(field, typeBuilderInstantiation);
}
#endregion
}
}
18 changes: 18 additions & 0 deletions src/libraries/System.Reflection.Emit/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@
<data name="InvalidOperation_ShouldNotHaveMethodBody" xml:space="preserve">
<value>Method body should not exist.</value>
</data>
<data name="InvalidOperation_ConstructorNotAllowedOnInterface" xml:space="preserve">
<value>Interface cannot have constructors.</value>
</data>
<data name="Argument_UnmatchedMethodForLocal" xml:space="preserve">
<value>Local passed in does not belong to this ILGenerator.</value>
</data>
Expand All @@ -198,6 +201,21 @@
<data name="Argument_NotMethodCallOpcode" xml:space="preserve">
<value>The specified opcode cannot be passed to EmitCall.</value>
</data>
<data name="InvalidOperation_DefaultConstructorILGen" xml:space="preserve">
<value>Unable to access ILGenerator on a constructor created with DefineDefaultConstructor.</value>
</data>
<data name="NotSupported_NoParentDefaultConstructor" xml:space="preserve">
<value>Parent does not have a default constructor. The default constructor must be explicitly defined.</value>
</data>
<data name="InvalidOperation_TypeHasBeenCreated" xml:space="preserve">
<value>Unable to change after type has been created.</value>
</data>
<data name="NotSupported_TypeNotYetCreated" xml:space="preserve">
<value>The invoked member is not supported before the type is created.</value>
</data>
<data name="Argument_NoStaticVirtual" xml:space="preserve">
<value>Method cannot be both static and virtual.</value>
</data>
<data name="Argument_BadExceptionCodeGen" xml:space="preserve">
<value>Incorrect code generation for exception block.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<Compile Include="$(CoreLibSharedDir)System\Reflection\Emit\TypeNameBuilder.cs" Link="System\Reflection\Emit\TypeNameBuilder.cs" />
<Compile Include="System\Reflection\Emit\ConstructorBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\CustomAttributeWrapper.cs" />
<Compile Include="System\Reflection\Emit\AssemblyBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\EnumBuilderImpl.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ protected override ModuleBuilder DefineDynamicModuleCore(string name)
throw new InvalidOperationException(SR.InvalidOperation_NoMultiModuleAssembly);
}

_module = new ModuleBuilderImpl(name, _coreAssembly, _metadataBuilder);
_module = new ModuleBuilderImpl(name, _coreAssembly, _metadataBuilder, this);
return _module;
}

Expand Down