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

Replace DAMT.All with more restricted annotation on InvokeMember/FindMembers/DeclaredMembers #109801

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -58,7 +58,7 @@ protected override TypeInfo CreateTypeInfoCore()
/// </summary>
public override Guid GUID => m_typeBuilder.GUID;

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object? InvokeMember(
string name,
BindingFlags invokeAttr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override Type MakeArrayType(int rank)

public override Guid GUID => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); }

public override Assembly Assembly => m_type.Assembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ public override Guid GUID
}
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target,
object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ public override MemberInfo[] GetMembers(BindingFlags bindingAttr)
public override MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo member)
=> GetRuntimeTypeInfo().GetMemberWithSameMetadataDefinitionAs(member);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters)
=> GetRuntimeTypeInfo().InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ protected override bool IsPointerImpl()
public override PropertyInfo[] GetProperties(BindingFlags bindingAttr) { Debug.Assert(false); throw NotImplemented.ByDesign; }
public override bool IsDefined(Type attributeType, bool inherit) { Debug.Assert(false); throw NotImplemented.ByDesign; }
public override Guid GUID { get { Debug.Assert(false); throw NotImplemented.ByDesign; } }
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields |
DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties |
DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) { Debug.Assert(false); throw NotImplemented.ByDesign; }
public override Module Module { get { Debug.Assert(false); throw NotImplemented.ByDesign; } }
public override string Namespace { get { Debug.Assert(false); throw NotImplemented.ByDesign; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,10 @@ public override int GetArrayRank()

public override Guid GUID => throw new NotSupportedException(SR.NotSupported_NonReflectedType);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields |
DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties |
DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target,
object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public override Type MakeArrayType(int rank)
}
public override Guid GUID => throw new NotSupportedException();

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) { throw new NotSupportedException(); }

public override Assembly Assembly => _genericType.Assembly;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public interface IReflect
// For the default binder, the most specific method will be selected.
//
// This will invoke a specific member...
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(Type.InvokeMemberMembers)]
object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters);

// Return the underlying Type that represents the IReflect Object. For expando object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override Type[] GetOptionalCustomModifiers()
public override Type GetGenericTypeDefinition() => _unmodifiedType.GetGenericTypeDefinition();
public override bool IsGenericType => _unmodifiedType.IsGenericType;

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target,
object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters)
=> throw new NotSupportedException(SR.NotSupported_ModifiedType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public sealed override Type MakeArrayType(int rank)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public sealed override PropertyInfo[] GetProperties(BindingFlags bindingAttr) => throw new NotSupportedException(SR.NotSupported_SignatureType);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public sealed override object InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters) => throw new NotSupportedException(SR.NotSupported_SignatureType);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
Expand All @@ -147,7 +147,7 @@ public sealed override Type MakeArrayType(int rank)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
protected sealed override PropertyInfo GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers) => throw new NotSupportedException(SR.NotSupported_SignatureType);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(GetAllMembers)]
public sealed override MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter? filter, object? filterCriteria) => throw new NotSupportedException(SR.NotSupported_SignatureType);

[DynamicallyAccessedMembers(GetAllMembers)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public TypeDelegator([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.
public override Guid GUID => typeImpl.GUID;
public override int MetadataToken => typeImpl.MetadataToken;

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target,
object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public virtual IEnumerable<FieldInfo> DeclaredFields

public virtual IEnumerable<MemberInfo> DeclaredMembers
{
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(GetAllMembers)]
get => GetMembers(DeclaredOnlyLookup);
}

Expand Down
25 changes: 21 additions & 4 deletions src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)

[DebuggerStepThrough]
[DebuggerHidden]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public override object? InvokeMember(
string name, BindingFlags bindingFlags, Binder? binder, object? target,
object?[]? providedArgs, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParams)
Expand Down Expand Up @@ -448,7 +448,12 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)

// Lookup Field
FieldInfo? selFld = null;
FieldInfo[]? flds = GetMember(name, MemberTypes.Field, bindingFlags) as FieldInfo[];
FieldInfo[]? flds = GetFields(this, name, bindingFlags);

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070",
Justification = $"MemberTypes.Field is satisfied by ({nameof(InvokeMemberMembers)}) on this method")]
static FieldInfo[]? GetFields(RuntimeType thisType, string name, BindingFlags bindingFlags)
=> thisType.GetMember(name, MemberTypes.Field, bindingFlags) as FieldInfo[];

Debug.Assert(flds != null);

Expand Down Expand Up @@ -560,7 +565,13 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)
if ((bindingFlags & BindingFlags.InvokeMethod) != 0)
{
// Lookup Methods
MethodInfo[] semiFinalists = (GetMember(name, MemberTypes.Method, bindingFlags) as MethodInfo[])!;
MethodInfo[] semiFinalists = GetMethods(this, name, bindingFlags)!;

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070",
Justification = $"MemberTypes.Method is satisfied by ({nameof(InvokeMemberMembers)}) on this method")]
static MethodInfo[]? GetMethods(RuntimeType thisType, string name, BindingFlags bindingFlags)
=> thisType.GetMember(name, MemberTypes.Method, bindingFlags) as MethodInfo[];

List<MethodInfo>? results = null;

for (int i = 0; i < semiFinalists.Length; i++)
Expand Down Expand Up @@ -595,7 +606,13 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)
if (finalist == null && isGetProperty || isSetProperty)
{
// Lookup Property
PropertyInfo[] semiFinalists = (GetMember(name, MemberTypes.Property, bindingFlags) as PropertyInfo[])!;
PropertyInfo[] semiFinalists = GetProperties(this, name, bindingFlags)!;

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070",
Justification = $"MemberTypes.Property is satisfied by ({nameof(InvokeMemberMembers)}) on this method")]
static PropertyInfo[]? GetProperties(RuntimeType thisType, string name, BindingFlags bindingFlags)
=> thisType.GetMember(name, MemberTypes.Property, bindingFlags) as PropertyInfo[];

List<MethodInfo>? results = null;

for (int i = 0; i < semiFinalists.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public virtual Type[] FindInterfaces(TypeFilter filter, object? filterCriteria)
return ret;
}

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(GetAllMembers)]
public virtual MemberInfo[] FindMembers(MemberTypes memberType, BindingFlags bindingAttr, MemberFilter? filter, object? filterCriteria)
{
// Define the work arrays
Expand Down
11 changes: 8 additions & 3 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,15 @@ protected virtual TypeCode GetTypeCodeImpl()

[DebuggerHidden]
[DebuggerStepThrough]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args) => InvokeMember(name, invokeAttr, binder, target, args, null, null, null);

[DebuggerHidden]
[DebuggerStepThrough]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, CultureInfo? culture) => InvokeMember(name, invokeAttr, binder, target, args, null, culture, null);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
[DynamicallyAccessedMembers(InvokeMemberMembers)]
public abstract object? InvokeMember(string name, BindingFlags invokeAttr, Binder? binder, object? target, object?[]? args, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParameters);

[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]
Expand Down Expand Up @@ -744,5 +744,10 @@ public override int GetHashCode()
DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties |
DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors |
DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes;

internal const DynamicallyAccessedMemberTypes InvokeMemberMembers = DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields |
DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods |
DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties |
DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;
}
}
Loading
Loading