Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanRead.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 PropertyBuilderTest
10 {
11 [Fact]
12 public void CanRead_OnlyGetAccessor_ReturnsTrue()
13 {
14 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
15 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
16 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
17
18 MethodAttributes getMethodAttributes = MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
19 MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), new Type[0]);
20 ILGenerator methodILGenerator = method.GetILGenerator();
21 methodILGenerator.Emit(OpCodes.Ldarg_0);
22 methodILGenerator.Emit(OpCodes.Ldfld, field);
23 methodILGenerator.Emit(OpCodes.Ret);
24
25 property.SetGetMethod(method);
26 Assert.True(property.CanRead);
27 }
28
29 [Fact]
30 public void CanRead_OnlyGetAccessor_ReturnsFalse()
31 {
32 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
33 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
34 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
35
36 MethodAttributes setMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
37 MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) });
38
39 ILGenerator methodILGenerator = method.GetILGenerator();
40 methodILGenerator.Emit(OpCodes.Ldarg_0);
41 methodILGenerator.Emit(OpCodes.Ldarg_1);
42 methodILGenerator.Emit(OpCodes.Stfld, field);
43 methodILGenerator.Emit(OpCodes.Ret);
44
45 property.SetSetMethod(method);
46 Assert.False(property.CanRead);
47 }
48
49 [Fact]
50 public void CanRead_NoAccessors_ReturnsFalse()
51 {
52 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
53 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
54 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
55
56 Assert.False(property.CanRead);
57 }
58
59 [Fact]
60 public void CanRead_PublicStaticGetAccessor_ReturnsTrue()
61 {
62 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public);
63 FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private);
64 PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null);
65
66 MethodAttributes getMethodAttributes = MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
67 MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null);
68
69 ILGenerator methodILGenerator = method.GetILGenerator();
70 methodILGenerator.Emit(OpCodes.Ldfld, field);
71 methodILGenerator.Emit(OpCodes.Ret);
72
73 property.SetGetMethod(method);
74 Assert.True(property.CanRead);
75 }
76 }
77 }