diff options
author | Martin Baulig <martin@novell.com> | 2004-09-21 19:52:07 +0400 |
---|---|---|
committer | Martin Baulig <martin@novell.com> | 2004-09-21 19:52:07 +0400 |
commit | 0bbb5a8b19150e754df903a7f527df744f494ae7 (patch) | |
tree | ba6164528bd6e5ffc34f678d7c1bfa0e5ad95dae /mcs/tests/test-295.cs | |
parent | e90e84ff616f1b3ead744fa575d2a41446bb81c0 (diff) |
2004-09-21 Martin Baulig <martin@ximian.com>
* test-295.cs: Added Geoff Norton's patch for #62984.
svn path=/trunk/mcs/; revision=34176
Diffstat (limited to 'mcs/tests/test-295.cs')
-rw-r--r-- | mcs/tests/test-295.cs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/mcs/tests/test-295.cs b/mcs/tests/test-295.cs new file mode 100644 index 00000000000..b0bc5d0d9bc --- /dev/null +++ b/mcs/tests/test-295.cs @@ -0,0 +1,83 @@ +using System; +using System.Threading; +using System.Reflection; +using System.Reflection.Emit; + + +[AttributeUsage (AttributeTargets.All)] +public class MyAttribute : Attribute { + public MyAttribute (object o) { + this.o = o; + } + + public object my + { + get { + return o; + } + } + + object o; +} + +public class MyConstructorBuilder +{ + public static int Main() + { + Type myHelloworld = MyCreateCallee(Thread.GetDomain()); + ConstructorInfo myConstructor = myHelloworld.GetConstructor(new Type[]{typeof(String)}); + object[] myAttributes1 = myConstructor.GetCustomAttributes(true); + if (myAttributes1 == null) + return 1; + if (myAttributes1.Length != 1) + return 2; + MyAttribute myAttribute = myAttributes1[0] as MyAttribute; + if (myAttribute == null) + return 3; + if (myAttribute.my.GetType() != typeof(TypeCode)) + return 4; + return 0; + } + + private static Type MyCreateCallee(AppDomain domain) + { + AssemblyName myAssemblyName = new AssemblyName(); + myAssemblyName.Name = "EmittedAssembly"; + // Define a dynamic assembly in the current application domain. + AssemblyBuilder myAssembly = + domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run); + // Define a dynamic module in this assembly. + ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule"); + // Construct a 'TypeBuilder' given the name and attributes. + TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld", + TypeAttributes.Public); + // Define a constructor of the dynamic class. + ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor( + MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)}); + ILGenerator myILGenerator = myConstructor.GetILGenerator(); + myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked"); + myILGenerator.Emit(OpCodes.Ldarg_1); + MethodInfo myMethodInfo = + typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)}); + myILGenerator.Emit(OpCodes.Call, myMethodInfo); + myILGenerator.Emit(OpCodes.Ret); + Type myType = typeof(MyAttribute); + ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[]{typeof(object)}); + try + { + CustomAttributeBuilder methodCABuilder = new CustomAttributeBuilder (myConstructorInfo, new object [] { TypeCode.Double } ); + + myConstructor.SetCustomAttribute(methodCABuilder); + } + catch(ArgumentNullException ex) + { + Console.WriteLine("The following exception has occured : "+ex.Message); + } + catch(Exception ex) + { + Console.WriteLine("The following exception has occured : "+ex.Message); + } + return myTypeBuilder.CreateType(); + } +} + |