| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- //
- // DynamicMethodTest.cs - NUnit Test Cases for the DynamicMethod class
- //
- // Gert Driesen ([email protected])
- //
- // (C) 2006 Novell
- #if NET_2_0
- using System;
- using System.Reflection;
- using System.Reflection.Emit;
- using NUnit.Framework;
- namespace MonoTests.System.Reflection.Emit
- {
- [TestFixture]
- public class DynamicMethodTest
- {
- private delegate int HelloInvoker (string msg);
- [Test]
- public void Constructor1_Name_Null ()
- {
- try {
- new DynamicMethod (null,
- typeof (void),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.AreEqual ("name", ex.ParamName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- }
- }
- [Test]
- public void Constructor2_Name_Null ()
- {
- try {
- new DynamicMethod (null,
- typeof (void),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest));
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.AreEqual ("name", ex.ParamName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- }
- }
- [Test]
- public void Constructor3_Name_Null ()
- {
- try {
- new DynamicMethod (null,
- typeof (void),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module, true);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.AreEqual ("name", ex.ParamName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- }
- }
- [Test]
- public void Constructor4_Name_Null ()
- {
- try {
- new DynamicMethod (null,
- typeof (void),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest), true);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.AreEqual ("name", ex.ParamName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- }
- }
- [Test]
- public void Constructor5_Name_Null ()
- {
- try {
- new DynamicMethod (null,
- MethodAttributes.Public | MethodAttributes.Static,
- CallingConventions.Standard,
- typeof (void),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module, true);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.AreEqual ("name", ex.ParamName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- }
- }
- [Test]
- public void Constructor6_Name_Null ()
- {
- try {
- new DynamicMethod (null,
- MethodAttributes.Public | MethodAttributes.Static,
- CallingConventions.Standard,
- typeof (void),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest), true);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.AreEqual ("name", ex.ParamName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- }
- }
- [Test] // bug #78253
- public void DynamicMethodReference ()
- {
- DynamicMethod hello = new DynamicMethod ("Hello",
- typeof (int),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module);
- Assert.IsNull (hello.DeclaringType, "#1");
- DynamicMethod write = new DynamicMethod ("Write",
- typeof (int),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest));
- Assert.IsNull (hello.DeclaringType, "#2");
- MethodInfo invokeWrite = write.GetBaseDefinition ();
- ILGenerator helloIL = hello.GetILGenerator ();
- helloIL.Emit (OpCodes.Ldarg_0);
- helloIL.EmitCall (OpCodes.Call, invokeWrite, null);
- helloIL.Emit (OpCodes.Ret);
- ILGenerator writeIL = write.GetILGenerator ();
- writeIL.Emit (OpCodes.Ldc_I4_2);
- writeIL.Emit (OpCodes.Ret);
- HelloInvoker hi =
- (HelloInvoker) hello.CreateDelegate (typeof (HelloInvoker));
- int ret = hi ("Hello, World!");
- Assert.AreEqual (2, ret, "#3");
- object[] invokeArgs = { "Hello, World!" };
- object objRet = hello.Invoke (null, invokeArgs);
- Assert.AreEqual (2, objRet, "#4");
- }
- [Test]
- public void EmptyMethodBody ()
- {
- DynamicMethod hello = new DynamicMethod ("Hello",
- typeof (int),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module);
- object[] invokeArgs = { "Hello, World!" };
- // no IL generator
- try {
- hello.Invoke (null, invokeArgs);
- Assert.Fail ("#1");
- } catch (InvalidOperationException) {
- }
- // empty method body
- hello.GetILGenerator ();
- try {
- hello.Invoke (null, invokeArgs);
- Assert.Fail ("#2");
- } catch (InvalidOperationException) {
- }
- }
- private delegate string ReturnString (string msg);
- private delegate void DoNothing (string msg);
- private static string private_method (string s) {
- return s;
- }
- [Test]
- public void SkipVisibility ()
- {
- DynamicMethod hello = new DynamicMethod ("Hello",
- typeof (string),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module, true);
- ILGenerator helloIL = hello.GetILGenerator ();
- helloIL.Emit (OpCodes.Ldarg_0);
- helloIL.EmitCall (OpCodes.Call, typeof (DynamicMethodTest).GetMethod ("private_method", BindingFlags.Static|BindingFlags.NonPublic), null);
- helloIL.Emit (OpCodes.Ret);
- ReturnString del =
- (ReturnString) hello.CreateDelegate (typeof (ReturnString));
- Assert.AreEqual ("ABCD", del ("ABCD"));
- }
- [Test]
- public void ReturnType_Null ()
- {
- DynamicMethod hello = new DynamicMethod ("Hello",
- null,
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module, true);
- Assert.AreEqual (typeof (void), hello.ReturnType, "#1");
- ILGenerator helloIL = hello.GetILGenerator ();
- helloIL.Emit (OpCodes.Ret);
- DoNothing dn = (DoNothing) hello.CreateDelegate (typeof (DoNothing));
- dn ("whatever");
- object[] invokeArgs = { "Hello, World!" };
- object objRet = hello.Invoke (null, invokeArgs);
- Assert.IsNull (objRet, "#2");
- }
- [Test]
- public void Name_Empty ()
- {
- DynamicMethod hello = new DynamicMethod (string.Empty,
- typeof (int),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest).Module);
- Assert.AreEqual (string.Empty, hello.Name, "#1");
- DynamicMethod write = new DynamicMethod ("Write",
- typeof (int),
- new Type[] { typeof (string) },
- typeof (DynamicMethodTest));
- MethodInfo invokeWrite = write.GetBaseDefinition ();
- ILGenerator helloIL = hello.GetILGenerator ();
- helloIL.Emit (OpCodes.Ldarg_0);
- helloIL.EmitCall (OpCodes.Call, invokeWrite, null);
- helloIL.Emit (OpCodes.Ret);
- ILGenerator writeIL = write.GetILGenerator ();
- writeIL.Emit (OpCodes.Ldc_I4_2);
- writeIL.Emit (OpCodes.Ret);
- HelloInvoker hi =
- (HelloInvoker) hello.CreateDelegate (typeof (HelloInvoker));
- int ret = hi ("Hello, World!");
- Assert.AreEqual (2, ret, "#2");
- object[] invokeArgs = { "Hello, World!" };
- object objRet = hello.Invoke (null, invokeArgs);
- Assert.AreEqual (2, objRet, "#3");
- }
- [Test]
- public void Circular_Refs () {
- DynamicMethod m1 = new DynamicMethod("f1", typeof(int), new Type[] { typeof (int) },
- typeof(object));
- DynamicMethod m2 = new DynamicMethod("f2", typeof(int), new Type[] { typeof (int) },
- typeof(object));
- ILGenerator il1 = m1.GetILGenerator();
- ILGenerator il2 = m2.GetILGenerator();
- Label l = il1.DefineLabel ();
- //il1.EmitWriteLine ("f1");
- il1.Emit (OpCodes.Ldarg_0);
- il1.Emit (OpCodes.Ldc_I4_0);
- il1.Emit (OpCodes.Bne_Un, l);
- il1.Emit (OpCodes.Ldarg_0);
- il1.Emit (OpCodes.Ret);
- il1.MarkLabel (l);
- il1.Emit (OpCodes.Ldarg_0);
- il1.Emit (OpCodes.Ldc_I4_1);
- il1.Emit (OpCodes.Sub);
- il1.Emit (OpCodes.Call, m2);
- il1.Emit (OpCodes.Ret);
- //il2.EmitWriteLine("f2");
- il2.Emit(OpCodes.Ldarg_0);
- il2.Emit(OpCodes.Call, m1);
- il2.Emit(OpCodes.Ret);
- m1.Invoke(null, new object[] { 5 });
- }
- class Host {
- static string Field = "foo";
- }
- [Test]
- [Category ("NotDotNet")] // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=297416
- public void TestOwnerMemberAccess ()
- {
- DynamicMethod method = new DynamicMethod ("GetField",
- typeof (string), new Type [0], typeof (Host));
- ILGenerator il = method.GetILGenerator ();
- il.Emit (OpCodes.Ldsfld, typeof (Host).GetField (
- "Field", BindingFlags.Static | BindingFlags.NonPublic));
- il.Emit (OpCodes.Ret);
- string ret = (string) method.Invoke (null, new object [] {});
- Assert.AreEqual ("foo", ret, "#1");
- }
- }
- }
- #endif
|