Skip to content

Commit 67c73d2

Browse files
authored
Merge pull request #1157 from Unity-Technologies/unity-master-bug-1109657
[corlib] Allow DM with an extra param to have the first one bound to …
2 parents 6138727 + 05e67e3 commit 67c73d2

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

mcs/class/corlib/System.Reflection.Emit/DynamicMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public Delegate CreateDelegate (Type delegateType)
172172

173173
CreateDynMethod ();
174174

175-
deleg = Delegate.CreateDelegate (delegateType, this);
175+
deleg = Delegate.CreateDelegate (delegateType, null, this);
176176
return deleg;
177177
}
178178

mcs/class/corlib/Test/System.Reflection.Emit/DynamicMethodTest.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,55 @@ private static void AssertTypedRef (TypedReference tr)
744744
Assert.AreEqual (typeof (TypedRefTarget), TypedReference.GetTargetType (tr));
745745
}
746746
#endif
747+
748+
static Action GenerateProblematicMethod (bool add_extra, bool mismatch = false, bool use_vts = false)
749+
{
750+
Type this_type = typeof(object);
751+
Type bound_type = typeof(object);
752+
if (mismatch) {
753+
this_type = typeof (string);
754+
bound_type = typeof (DynamicMethodTest);
755+
} else if (use_vts) {
756+
this_type = typeof (int);
757+
bound_type = typeof (long);
758+
}
759+
760+
Type[] args;
761+
if (add_extra)
762+
args = new[] { this_type };
763+
else
764+
args = new Type [0];
765+
766+
var mb = new DynamicMethod("Peek", null, args, bound_type, true);
767+
var il = mb.GetILGenerator ();
768+
il.Emit(OpCodes.Ret);
769+
return (Action) mb.CreateDelegate(typeof(Action));
770+
}
771+
772+
[Test]
773+
public void ExtraArgGetsIgnored ()
774+
{
775+
GenerateProblematicMethod (true) ();
776+
}
777+
778+
[Test]
779+
public void ExactNumberOfArgsWork ()
780+
{
781+
GenerateProblematicMethod (false) ();
782+
}
783+
784+
[Test]
785+
public void ExtraArgWithMismatchedTypes ()
786+
{
787+
GenerateProblematicMethod (true, mismatch: true) ();
788+
}
789+
790+
[Test]
791+
[ExpectedException (typeof (ArgumentException))]
792+
public void ExtraArgWithValueType ()
793+
{
794+
GenerateProblematicMethod (true, use_vts: true) ();
795+
}
747796
}
748797
}
749798

0 commit comments

Comments
 (0)