Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue.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 PropertyBuilderTest16
10 {
11 [Fact]
12 public void SetValue_ThrowsNotSupportedException()
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.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig;
19 MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null);
20
21 ILGenerator methodILGenerator = method.GetILGenerator();
22 methodILGenerator.Emit(OpCodes.Ldarg_0);
23 methodILGenerator.Emit(OpCodes.Ldfld, field);
24 methodILGenerator.Emit(OpCodes.Ret);
25 property.SetGetMethod(method);
26
27 Type createdType = type.CreateTypeInfo().AsType();
28 object obj = createdType.GetConstructor(new Type[0]).Invoke(null);
29 Assert.Throws<NotSupportedException>(() => property.SetValue(obj, 99, null));
30 }
31 }
32 }