public static bool TestDynamicInvoke()
{
Console.Write("Testing dynamic invoke...");
{
TestValueType t = new TestValueType {
X = 123
};
Func <string, string> d = t.GiveX;
string result = (string)d.DynamicInvoke(new object[] { "MyPrefix" });
if (result != "MyPrefix123")
{
return(false);
}
}
{
Func <int, TestValueType> d = TestValueType.MakeValueType;
TestValueType result = (TestValueType)d.DynamicInvoke(new object[] { 789 });
if (result.X != 789)
{
return(false);
}
}
{
IFoo t = new ClassWithIFoo("Class");
Func <int, string> d = t.DoFoo;
if ((string)d.DynamicInvoke(new object[] { 987 }) != "Class987")
{
return(false);
}
}
{
IFoo t = new StructWithIFoo("Struct");
Func <int, string> d = t.DoFoo;
if ((string)d.DynamicInvoke(new object[] { 654 }) != "Struct654")
{
return(false);
}
}
{
Func <string, string, string> d = ExtensionClass.Combine;
if ((string)d.DynamicInvoke(new object[] { "Hello", "World" }) != "HelloWorld")
{
return(false);
}
}
{
Func <string, string> d = "Hi".Combine;
if ((string)d.DynamicInvoke(new object[] { "There" }) != "HiThere")
{
return(false);
}
}
{
Mutate <int> d = ClassWithByRefs.Mutate;
object[] args = new object[] { 8 };
d.DynamicInvoke(args);
if ((int)args[0] != 50)
{
return(false);
}
}
{
Mutate <string> d = ClassWithByRefs.Mutate;
object[] args = new object[] { "Hello" };
d.DynamicInvoke(args);
if ((string)args[0] != "HelloMutated")
{
return(false);
}
}
unsafe
{
GetAndReturnPointerDelegate d = ClassWithPointers.GetAndReturnPointer;
if (Pointer.Unbox(d.DynamicInvoke(new object[] { (IntPtr)8 })) != (void *)50)
{
return(false);
}
if (Pointer.Unbox(d.DynamicInvoke(new object[] { Pointer.Box((void *)9, typeof(void *)) })) != (void *)51)
{
return(false);
}
}
#if false
// This is hitting an EH bug around throw/rethrow from a catch block (pass is not set properly)
unsafe
{
PassPointerByRefDelegate d = ClassWithPointers.PassPointerByRef;
var args = new object[] { (IntPtr)8 };
bool caught = false;
try
{
d.DynamicInvoke(args);
}
catch (ArgumentException)
{
caught = true;
}
if (!caught)
{
return(false);
}
}
#endif
Console.WriteLine("OK");
return(true);
}