Skip to content

Commit

Permalink
[corlib] Allow DM with an extra param to have the first one bound to …
Browse files Browse the repository at this point in the history
…null. Fixes mono#9033.
  • Loading branch information
kumpera authored and marek-safar committed Jun 12, 2018
1 parent 03bdf34 commit 626ab10
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs
Expand Up @@ -172,7 +172,7 @@ public Delegate CreateDelegate (Type delegateType)

CreateDynMethod ();

deleg = Delegate.CreateDelegate (delegateType, this);
deleg = Delegate.CreateDelegate (delegateType, null, this);
return deleg;
}

Expand Down
49 changes: 49 additions & 0 deletions mcs/class/corlib/Test/System.Reflection.Emit/DynamicMethodTest.cs
Expand Up @@ -740,6 +740,55 @@ private static void AssertTypedRef (TypedReference tr)
Assert.AreEqual (typeof (TypedRefTarget), TypedReference.GetTargetType (tr));
}
#endif

static Action GenerateProblematicMethod (bool add_extra, bool mismatch = false, bool use_vts = false)
{
Type this_type = typeof(object);
Type bound_type = typeof(object);
if (mismatch) {
this_type = typeof (string);
bound_type = typeof (DynamicMethodTest);
} else if (use_vts) {
this_type = typeof (int);
bound_type = typeof (long);
}

Type[] args;
if (add_extra)
args = new[] { this_type };
else
args = new Type [0];

var mb = new DynamicMethod("Peek", null, args, bound_type, true);
var il = mb.GetILGenerator ();
il.Emit(OpCodes.Ret);
return (Action) mb.CreateDelegate(typeof(Action));
}

[Test]
public void ExtraArgGetsIgnored ()
{
GenerateProblematicMethod (true) ();
}

[Test]
public void ExactNumberOfArgsWork ()
{
GenerateProblematicMethod (false) ();
}

[Test]
public void ExtraArgWithMismatchedTypes ()
{
GenerateProblematicMethod (true, mismatch: true) ();
}

[Test]
[ExpectedException (typeof (ArgumentException))]
public void ExtraArgWithValueType ()
{
GenerateProblematicMethod (true, use_vts: true) ();
}
}
}

0 comments on commit 626ab10

Please sign in to comment.