File: PropertyBuilderTest.cs

package info (click to toggle)
mono 1.2.2.1-1
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 142,728 kB
  • ctags: 256,408
  • sloc: cs: 1,495,736; ansic: 249,442; sh: 18,304; xml: 12,463; makefile: 5,046; perl: 1,248; asm: 635; yacc: 285; sql: 7
file content (104 lines) | stat: -rw-r--r-- 2,678 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// PropertyBuilderTest.cs - NUnit Test Cases for the PropertyBuilder class
//
// Zoltan Varga (vargaz@freemail.hu)
//
// (C) Ximian, Inc.  http://www.ximian.com

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;

using NUnit.Framework;

namespace MonoTests.System.Reflection.Emit
{

[TestFixture]
public class PropertyBuilderTest : Assertion
{	
    private TypeBuilder tb;

	private ModuleBuilder module;

	[SetUp]
	protected void SetUp () {
		AssemblyName assemblyName = new AssemblyName();
		assemblyName.Name = "MonoTests.System.Reflection.Emit.PropertyBuilderTest";

		AssemblyBuilder assembly 
			= Thread.GetDomain().DefineDynamicAssembly(
				assemblyName, AssemblyBuilderAccess.Run);

		module = assembly.DefineDynamicModule("module1");
		
	    tb = module.DefineType("class1", 
							   TypeAttributes.Public);
	}

	[Test]
	public void TestProperties () {

		FieldBuilder propField = tb.DefineField("propField",
												typeof(int),
												FieldAttributes.Private);

		PropertyBuilder property = 
			tb.DefineProperty ("prop", PropertyAttributes.HasDefault, typeof (int), new Type [0] { });
		property.SetConstant (44);

		MethodBuilder propertyGet = tb.DefineMethod("GetProp",
													MethodAttributes.Public,
													typeof(int),
													new Type[] { });

		{
			ILGenerator il = propertyGet.GetILGenerator();

			il.Emit(OpCodes.Ldarg_0);
			il.Emit(OpCodes.Ldfld, propField);
			il.Emit(OpCodes.Ret);
		}

		MethodBuilder propertySet = tb.DefineMethod("SetProp",
													MethodAttributes.Public,
													null,
													new Type[] { typeof(int) });
		{
			ILGenerator il = propertySet.GetILGenerator();

			il.Emit(OpCodes.Ldarg_0);
			il.Emit(OpCodes.Ldarg_1);
			il.Emit(OpCodes.Stfld, propField);
			il.Emit(OpCodes.Ret);
		}

		property.SetGetMethod(propertyGet);
		property.SetSetMethod(propertySet);

		Type t = tb.CreateType ();

		PropertyInfo[] props = t.GetProperties (BindingFlags.Public|BindingFlags.Instance);
		AssertEquals (1, props.Length);

		PropertyInfo p = t.GetProperty ("prop");

		AssertEquals (PropertyAttributes.HasDefault, p.Attributes);
		AssertEquals (true, p.CanRead);
		AssertEquals (true, p.CanWrite);
		AssertEquals ("prop", p.Name);
		AssertEquals (MemberTypes.Property, p.MemberType);
		AssertEquals (typeof (int), p.PropertyType);
		MethodInfo[] methods = p.GetAccessors ();
		AssertEquals (2, methods.Length);
		AssertNotNull (p.GetGetMethod ());
		AssertNotNull (p.GetSetMethod ());

		object o = Activator.CreateInstance (t);
		p.SetValue (o, 42, null);
		AssertEquals (42, p.GetValue (o, null));
	}
}
}