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
Fix argument validation in RuntimeType.InvokeMember
The rollout of `!!` erroneously moved an ArgumentNullException to be thrown earlier in the method, preventing a null name from being used (which is valid with BindingFlags.CreateInstance).

(Separately, we should consider fixing the nullable reference type annotation on `string name`, since null is allowed in some circumstances.)
  • Loading branch information
stephentoub authored and github-actions committed Sep 2, 2022
commit 2184cc3d901b3ca3aa95f45b3788d5f048243a4b
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,6 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)
string name, BindingFlags bindingFlags, Binder? binder, object? target,
object?[]? providedArgs, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParams)
{
ArgumentNullException.ThrowIfNull(name);

const BindingFlags MemberBindingMask = (BindingFlags)0x000000FF;
const BindingFlags InvocationMask = (BindingFlags)0x0000FF00;
const BindingFlags BinderGetSetField = BindingFlags.GetField | BindingFlags.SetField;
Expand Down Expand Up @@ -567,10 +565,12 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)
// PutDispProperty and\or PutRefDispProperty ==> SetProperty.
if ((bindingFlags & (BindingFlags.PutDispProperty | BindingFlags.PutRefDispProperty)) != 0)
bindingFlags |= BindingFlags.SetProperty;

ArgumentNullException.ThrowIfNull(name);
if (name.Length == 0 || name.Equals("[DISPID=0]"))
{
// in InvokeMember we always pretend there is a default member if none is provided and we make it ToString
name = GetDefaultMemberName()! ?? "ToString";
name = GetDefaultMemberName() ?? "ToString";
}

// GetField or SetField
Expand Down
8 changes: 8 additions & 0 deletions src/libraries/System.Reflection/tests/DefaultBinderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ public static void InvokeWithNamedParametersOutOfOrder()
Assert.Equal(8, result);
}

[Theory]
[InlineData("")]
[InlineData(null)]
public static void InvokeWithCreateInstance(string name)
{
Assert.IsType<Sample>(typeof(Sample).InvokeMember(name, BindingFlags.CreateInstance, null, null, null));
}

public class Test
{
public void TestMethod(int param1) { }
Expand Down