Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanWrite.cs

raw
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using Xunit;
6
7 namespace System.Reflection.Emit.Tests
8 {
9 public class PropertyBuilderTest5
10 {
11 [Fact]
12 public void CanWrite_OnlySetAccessor_ReturnsFalse()
13 {
14 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
15
16 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
17 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
18
19 MethodAttributes setMethodAttributes = MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
20 MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) });
21
22 ILGenerator methodILGenerator = method.GetILGenerator();
23 methodILGenerator.Emit(OpCodes.Ldarg_0);
24 methodILGenerator.Emit(OpCodes.Ldarg_1);
25 methodILGenerator.Emit(OpCodes.Stfld, field);
26 methodILGenerator.Emit(OpCodes.Ret);
27
28 property.SetSetMethod(method);
29 Assert.True(property.CanWrite);
30 }
31
32 [Fact]
33 public void CanWrite_OnlyGetAccessor_ReturnsFalse()
34 {
35 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
36 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
37 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
38 MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
39 MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), new Type[0]);
40
41 ILGenerator methodILGenerator = method.GetILGenerator();
42 methodILGenerator.Emit(OpCodes.Ldarg_0);
43 methodILGenerator.Emit(OpCodes.Ldfld, field);
44 methodILGenerator.Emit(OpCodes.Ret);
45
46 property.SetGetMethod(method);
47 Assert.False(property.CanWrite);
48 }
49
50 [Fact]
51 public void CanWrite_NoAccessors_ReturnsFalse()
52 {
53 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
54 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
55 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
56
57 Assert.False(property.CanWrite);
58 }
59
60 [Fact]
61 public void CanWrite_PublicStaticSetAccessor_ReturnsTrue()
62 {
63 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
64 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
65 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
66
67 MethodAttributes setMethodAttributes = MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
68 MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) });
69
70 ILGenerator methodILGenerator = method.GetILGenerator();
71 methodILGenerator.Emit(OpCodes.Ldfld, field);
72 methodILGenerator.Emit(OpCodes.Ret);
73
74 property.SetSetMethod(method);
75 Assert.True(property.CanWrite);
76 }
77 }
78 }