| 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 PropertyBuilderTest15 |
| 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 SetSetMethod(MethodAttributes methodAttributes) |
| 18 |
{ |
| 19 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 20 |
FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); |
| 21 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); |
| 22 |
MethodBuilder method = ImplementMethod( type, "TestMethod", methodAttributes, null, new Type[] { typeof(int) }, field); |
| 23 |
|
| 24 |
property.SetSetMethod(method); |
| 25 |
MethodInfo actualMethod = property.GetSetMethod(true); |
| 26 |
Assert.Equal(method.Name, actualMethod.Name); |
| 27 |
} |
| 28 |
|
| 29 |
[Fact] |
| 30 |
public void SetSetMethod_NullMethodBuilder_ThrowsArgumentNullException() |
| 31 |
{ |
| 32 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 33 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); |
| 34 |
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => property.SetSetMethod(null)); |
| 35 |
} |
| 36 |
|
| 37 |
[Fact] |
| 38 |
public void TestThrowsExceptionForCreateTypeCalled() |
| 39 |
{ |
| 40 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); |
| 41 |
FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); |
| 42 |
PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); |
| 43 |
|
| 44 |
MethodAttributes setMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; |
| 45 |
MethodBuilder method = ImplementMethod(type, "TestMethod", setMethodAttributes, null, new Type[] { typeof(int) }, field); |
| 46 |
|
| 47 |
type.CreateTypeInfo().AsType(); |
| 48 |
Assert.Throws<InvalidOperationException>(() => { property.SetSetMethod(method); }); |
| 49 |
} |
| 50 |
|
| 51 |
private MethodBuilder ImplementMethod(TypeBuilder type, string methodName, MethodAttributes methodAttr, Type returnType, Type[] paramTypes, FieldBuilder field) |
| 52 |
{ |
| 53 |
MethodBuilder method = type.DefineMethod(methodName, methodAttr, returnType, paramTypes); |
| 54 |
|
| 55 |
ILGenerator methodILGenerator = method.GetILGenerator(); |
| 56 |
methodILGenerator.Emit(OpCodes.Ldarg_0); |
| 57 |
methodILGenerator.Emit(OpCodes.Ldarg_1); |
| 58 |
methodILGenerator.Emit(OpCodes.Stfld, field); |
| 59 |
methodILGenerator.Emit(OpCodes.Ret); |
| 60 |
|
| 61 |
return method; |
| 62 |
} |
| 63 |
} |
| 64 |
} |