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

Fix argument validation in RuntimeType.InvokeMember #74998

Merged
merged 1 commit into from
Sep 2, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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