public void TestMethodHandleComplete () {
MethodBuilder mb = genClass.DefineMethod (
genMethodName (), 0, typeof (void), new Type [0]);
- mb.CreateMethodBody (new byte[2], 0);
+ mb.CreateMethodBody (new byte[2], 1);
genClass.CreateType ();
RuntimeMethodHandle handle = mb.MethodHandle;
try {
mb.CreateMethodBody (new byte[1], -1);
Fail ();
- } catch (ArgumentException) {
+ } catch (ArgumentOutOfRangeException) {
}
// Check arguments 2.
try {
mb.CreateMethodBody (new byte[1], 2);
Fail ();
- } catch (ArgumentException) {
+ } catch (ArgumentOutOfRangeException) {
}
mb.CreateMethodBody (new byte[2], 1);
new Type[2] {
typeof(int), typeof(int)
});
- mb.CreateMethodBody (new byte[2], 0);
+ mb.CreateMethodBody (new byte[2], 1);
tb.CreateType ();
mb.DefineParameter (-5, ParameterAttributes.None, "param1");
}
new Type[2] {
typeof(int), typeof(int)
});
- mb.CreateMethodBody (new byte[2], 0);
+ mb.CreateMethodBody (new byte[2], 1);
tb.CreateType ();
mb.DefineParameter (1, ParameterAttributes.None, "param1");
}
mb.DefineParameter (1, 0, "param1");
mb.DefineParameter (2, 0, null);
- // Can not be called on a created type
- mb.CreateMethodBody (new byte[2], 0);
+ mb.CreateMethodBody (new byte[2], 1);
tb.CreateType ();
try {
mb.DefineParameter (1, 0, "param1");
}
}
+ [Test]
+#if NET_2_0
+ // MS.NET 2.x no longer allows a zero length method body
+ // to be emitted
+ [ExpectedException (typeof (InvalidOperationException))]
+#endif
+ public void ZeroLengthBodyTest1 ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [2] { typeof(int), typeof(int) });
+ mb.CreateMethodBody (new byte[2], 0);
+ genClass.CreateType ();
+ }
+
+ // A zero length method body can be created
+ [Test]
+ public void ZeroLengthBodyTest2 ()
+ {
+ MethodBuilder mb = genClass.DefineMethod (
+ genMethodName (), 0, typeof (void),
+ new Type [2] { typeof(int), typeof(int) });
+ mb.CreateMethodBody (new byte[2], 0);
+ }
+
[Test]
public void TestHashCode ()
{
MethodImplAttributes.OPTIL,
mb.GetMethodImplementationFlags ());
- // Can not be called on a created type
- mb.CreateMethodBody (new byte[2], 0);
+ mb.CreateMethodBody (new byte[2], 1);
mb.SetImplementationFlags (MethodImplAttributes.Managed);
tb.CreateType ();
try {
}
}
+ [AttributeUsage (AttributeTargets.Parameter)]
+ class PrivateAttribute : Attribute {
+
+ public PrivateAttribute () {
+ }
+ }
+
+ [Test]
+ public void GetCustomAttributes () {
+ TypeBuilder tb = module.DefineType (genTypeName (), TypeAttributes.Public);
+ MethodBuilder mb = tb.DefineMethod ("foo", MethodAttributes.Public,
+ typeof (void),
+ new Type [1] {typeof(int)});
+ mb.GetILGenerator ().Emit (OpCodes.Ret);
+
+ Type attrType = typeof (ObsoleteAttribute);
+ ConstructorInfo ctorInfo =
+ attrType.GetConstructor (new Type [] { typeof (String) });
+
+ mb.SetCustomAttribute (new CustomAttributeBuilder (ctorInfo, new object [] { "FOO" }));
+
+ // Check that attributes not accessible are not returned
+ mb.SetCustomAttribute (new CustomAttributeBuilder (typeof (PrivateAttribute).GetConstructor (new Type [0]), new object [] { }));
+
+ Type t = tb.CreateType ();
+
+ // Try the created type
+ {
+ MethodInfo mi = t.GetMethod ("foo");
+ object[] attrs = mi.GetCustomAttributes (true);
+
+ AssertEquals (1, attrs.Length);
+ Assert (attrs [0] is ObsoleteAttribute);
+ AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
+ }
+
+ // Try the type builder
+ {
+ MethodInfo mi = tb.GetMethod ("foo");
+ object[] attrs = mi.GetCustomAttributes (true);
+
+ AssertEquals (1, attrs.Length);
+ Assert (attrs [0] is ObsoleteAttribute);
+ AssertEquals ("FOO", ((ObsoleteAttribute)attrs [0]).Message);
+ }
+ }
+
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void TestAddDeclarativeSecurityAlreadyCreated () {
try {
mb.AddDeclarativeSecurity (action, set);
Fail ();
- }
- catch (ArgumentException) {
+ } catch (ArgumentOutOfRangeException) {
}
}
}