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 GenericTypeParameterBuilder implementation and save a generic type into assembly. #85658

Merged
merged 3 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -491,14 +491,11 @@ internal RuntimeTypeBuilder(
int[]? interfaceTokens = null;
if (interfaces != null)
{
interfaceTokens = new int[interfaces.Length + 1];
for (i = 0; i < interfaces.Length; i++)
{
// cannot contain null in the interface list
ArgumentNullException.ThrowIfNull(interfaces[i], nameof(interfaces));
}
interfaceTokens = new int[interfaces.Length + 1];
for (i = 0; i < interfaces.Length; i++)
{
interfaceTokens[i] = m_module.GetTypeTokenInternal(interfaces[i]);
}
}
Expand Down Expand Up @@ -1144,15 +1141,18 @@ internal void SetInterfaces(params Type[]? interfaces)

protected override GenericTypeParameterBuilder[] DefineGenericParametersCore(params string[] names)
{
for (int i = 0; i < names.Length; i++)
ArgumentNullException.ThrowIfNull(names[i], nameof(names));

if (m_inst != null)
{
throw new InvalidOperationException();
}

m_inst = new RuntimeGenericTypeParameterBuilder[names.Length];
for (int i = 0; i < names.Length; i++)
m_inst[i] = new RuntimeGenericTypeParameterBuilder(new RuntimeTypeBuilder(names[i], i, this));
{
string name = names[i];
ArgumentNullException.ThrowIfNull(name, nameof(names));
m_inst[i] = new RuntimeGenericTypeParameterBuilder(new RuntimeTypeBuilder(name, i, this));
}

return m_inst;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<Compile Include="System\Reflection\Emit\CustomAttributeWrapper.cs" />
<Compile Include="System\Reflection\Emit\AssemblyBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\FieldBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\GenericTypeParameterBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\MethodBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\ModuleBuilderImpl.cs" />
<Compile Include="System\Reflection\Emit\ParameterBuilderImpl.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace System.Reflection.Emit
{
internal sealed class GenericTypeParameterBuilderImpl : GenericTypeParameterBuilder
{
private readonly string _name;
private readonly TypeBuilderImpl _type;
private readonly int _genParamPosition;
private GenericParameterAttributes _genParamAttributes;
private bool _isGenericType;
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
private Type? _parent;

internal List<CustomAttributeWrapper>? _customAttributes;
internal List<Type>? _interfaces;

internal GenericTypeParameterBuilderImpl(string name, int genParamPosition, TypeBuilderImpl typeBuilder)
{
_name = name;
_genParamPosition = genParamPosition;
_type = typeBuilder;
_isGenericType = true;
}

protected override void SetBaseTypeConstraintCore([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? baseTypeConstraint)
{
_parent = baseTypeConstraint;

if (_parent != null)
{
_interfaces ??= new List<Type>();
_interfaces.Add(_parent);
}
}

protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute)
{
_customAttributes ??= new List<CustomAttributeWrapper>();
_customAttributes.Add(new CustomAttributeWrapper(con, binaryAttribute));
}

protected override void SetGenericParameterAttributesCore(GenericParameterAttributes genericParameterAttributes) =>
_genParamAttributes = genericParameterAttributes;

protected override void SetInterfaceConstraintsCore(params Type[]? interfaceConstraints)
{
if (interfaceConstraints != null)
{
_interfaces ??= new List<Type>(interfaceConstraints.Length);
_interfaces.AddRange(interfaceConstraints);
}
}
public override Type[] GetGenericParameterConstraints() =>
_interfaces == null ? EmptyTypes : _interfaces.ToArray();
public override bool IsGenericTypeParameter => _isGenericType;
public override bool IsGenericMethodParameter => !_isGenericType;
public override int GenericParameterPosition => _genParamPosition;
public override GenericParameterAttributes GenericParameterAttributes => _genParamAttributes;
public override string Name => _name;
public override Module Module => _type.Module;
public override Assembly Assembly => _type.Assembly;
public override string? FullName => null;
public override string? Namespace => null;
public override string? AssemblyQualifiedName => null;
public override Type UnderlyingSystemType => this;
public override bool IsGenericTypeDefinition => false;
public override bool IsGenericType => false;
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
public override bool IsGenericParameter => true;
public override bool IsConstructedGenericType => false;
public override bool ContainsGenericParameters => _type.ContainsGenericParameters;
public override MethodBase? DeclaringMethod => throw new NotImplementedException();
public override Type? BaseType => _parent;
public override RuntimeTypeHandle TypeHandle => throw new NotSupportedException();
public override Guid GUID => throw new NotSupportedException();
protected override bool IsArrayImpl() => false;
protected override bool IsByRefImpl() => false;
protected override bool IsPointerImpl() => false;
protected override bool IsPrimitiveImpl() => false;
protected override bool IsCOMObjectImpl() => false;
protected override bool HasElementTypeImpl() => false;
protected override TypeAttributes GetAttributeFlagsImpl() => TypeAttributes.Public;
public override Type GetElementType() => throw new NotSupportedException();
public override object[] GetCustomAttributes(bool inherit) => throw new NotSupportedException();
public override object[] GetCustomAttributes(Type attributeType, bool inherit) => throw new NotSupportedException();
public override bool IsDefined(Type attributeType, bool inherit) => throw new NotSupportedException();
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
protected override ConstructorInfo GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
public override ConstructorInfo[] GetConstructors(BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
protected override MethodInfo GetMethodImpl(string name, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
public override MethodInfo[] GetMethods(BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public override FieldInfo GetField(string name, BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
public override FieldInfo[] GetFields(BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
[return: DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
public override Type GetInterface(string name, bool ignoreCase) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
public override Type[] GetInterfaces() => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public override EventInfo GetEvent(string name, BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)]
public override EventInfo[] GetEvents() => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
public override Type[] GetNestedTypes(BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
public override Type GetNestedType(string name, BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(TypeBuilderImpl.GetAllMembers)]
public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) => throw new NotSupportedException();

public override InterfaceMapping GetInterfaceMap([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] Type interfaceType) => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
public override EventInfo[] GetEvents(BindingFlags bindingAttr) => throw new NotSupportedException();

[DynamicallyAccessedMembers(TypeBuilderImpl.GetAllMembers)]
public override MemberInfo[] GetMembers(BindingFlags bindingAttr) => throw new NotSupportedException();
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) => throw new NotSupportedException();
}
}
Loading