| 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 PropertyBuilderTest14 |
| 10 |
{ |
| 11 |
[Theory] |
| 12 |
[InlineData(MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] |
| 13 |
[InlineData(MethodAttributes.Family | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] |
| 14 |
[InlineData(MethodAttributes.FamORAssem | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] |
| 15 |
[InlineData(MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] |
| 16 |
[InlineData(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] |
| 17 |
public void SetGetMethod(MethodAttributes methodAttributes) |
| 18 |
{ |
| 19 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 20 |
FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); |
| 21 |
|
| 22 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); |
| 23 |
MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes, typeof(int), null); |
| 24 |
|
| 25 |
ILGenerator methodILGenerator = method.GetILGenerator(); |
| 26 |
methodILGenerator.Emit(OpCodes.Ldarg_0); |
| 27 |
methodILGenerator.Emit(OpCodes.Ldfld, field); |
| 28 |
methodILGenerator.Emit(OpCodes.Ret); |
| 29 |
|
| 30 |
property.SetGetMethod(method); |
| 31 |
MethodInfo actualMethod = property.GetGetMethod(true); |
| 32 |
Assert.Equal(method.Name, actualMethod.Name); |
| 33 |
} |
| 34 |
|
| 35 |
[Fact] |
| 36 |
public void SetGetMethod_NullMethodBuilder_ThrowsArgumentNullException() |
| 37 |
{ |
| 38 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 39 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); |
| 40 |
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => property.SetGetMethod(null)); |
| 41 |
} |
| 42 |
|
| 43 |
[Fact] |
| 44 |
public void SetGetMethod_TypeAlreadyCreated_ThrowsInvalidOperationException() |
| 45 |
{ |
| 46 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 47 |
FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); |
| 48 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); |
| 49 |
|
| 50 |
MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; |
| 51 |
MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null); |
| 52 |
|
| 53 |
ILGenerator methodILGenerator = method.GetILGenerator(); |
| 54 |
methodILGenerator.Emit(OpCodes.Ldfld, field); |
| 55 |
methodILGenerator.Emit(OpCodes.Ret); |
| 56 |
|
| 57 |
type.CreateTypeInfo().AsType(); |
| 58 |
Assert.Throws<InvalidOperationException>(() => property.SetGetMethod(method)); |
| 59 |
} |
| 60 |
} |
| 61 |
} |