Skip to content

Commit ff921f6

Browse files
Fix argument validation in RuntimeType.InvokeMember (#75006)
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.) Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent b617f4f commit ff921f6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,6 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)
459459
string name, BindingFlags bindingFlags, Binder? binder, object? target,
460460
object?[]? providedArgs, ParameterModifier[]? modifiers, CultureInfo? culture, string[]? namedParams)
461461
{
462-
ArgumentNullException.ThrowIfNull(name);
463-
464462
const BindingFlags MemberBindingMask = (BindingFlags)0x000000FF;
465463
const BindingFlags InvocationMask = (BindingFlags)0x0000FF00;
466464
const BindingFlags BinderGetSetField = BindingFlags.GetField | BindingFlags.SetField;
@@ -567,10 +565,12 @@ public override bool IsAssignableFrom([NotNullWhen(true)] Type? c)
567565
// PutDispProperty and\or PutRefDispProperty ==> SetProperty.
568566
if ((bindingFlags & (BindingFlags.PutDispProperty | BindingFlags.PutRefDispProperty)) != 0)
569567
bindingFlags |= BindingFlags.SetProperty;
568+
569+
ArgumentNullException.ThrowIfNull(name);
570570
if (name.Length == 0 || name.Equals("[DISPID=0]"))
571571
{
572572
// in InvokeMember we always pretend there is a default member if none is provided and we make it ToString
573-
name = GetDefaultMemberName()! ?? "ToString";
573+
name = GetDefaultMemberName() ?? "ToString";
574574
}
575575

576576
// GetField or SetField

src/libraries/System.Reflection/tests/DefaultBinderTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ public static void InvokeWithNamedParametersOutOfOrder()
167167
Assert.Equal(8, result);
168168
}
169169

170+
[Theory]
171+
[InlineData("")]
172+
[InlineData(null)]
173+
public static void InvokeWithCreateInstance(string name)
174+
{
175+
Assert.IsType<Sample>(typeof(Sample).InvokeMember(name, BindingFlags.CreateInstance, null, null, null));
176+
}
177+
170178
public class Test
171179
{
172180
public void TestMethod(int param1) { }

0 commit comments

Comments
 (0)