// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using Xunit; namespace System.Reflection.Emit.Tests { public class PropertyBuilderTest5 { [Fact] public void CanWrite_OnlySetAccessor_ReturnsFalse() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); MethodAttributes setMethodAttributes = MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig; MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) }); ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ldarg_1); methodILGenerator.Emit(OpCodes.Stfld, field); methodILGenerator.Emit(OpCodes.Ret); property.SetSetMethod(method); Assert.True(property.CanWrite); } [Fact] public void CanWrite_OnlyGetAccessor_ReturnsFalse() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), new Type[0]); ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ldfld, field); methodILGenerator.Emit(OpCodes.Ret); property.SetGetMethod(method); Assert.False(property.CanWrite); } [Fact] public void CanWrite_NoAccessors_ReturnsFalse() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); Assert.False(property.CanWrite); } [Fact] public void CanWrite_PublicStaticSetAccessor_ReturnsTrue() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); MethodAttributes setMethodAttributes = MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) }); ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldfld, field); methodILGenerator.Emit(OpCodes.Ret); property.SetSetMethod(method); Assert.True(property.CanWrite); } } }