| 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 ConstructorBuilderSetImplementationFlags |
| 10 |
{ |
| 11 |
[Fact] |
| 12 |
public void MethodImplementationFlags_SetToCustomValue() |
| 13 |
{ |
| 14 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); |
| 15 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) }); |
| 16 |
|
| 17 |
constructor.SetImplementationFlags(MethodImplAttributes.Runtime); |
| 18 |
MethodImplAttributes methodImplementationFlags = constructor.MethodImplementationFlags; |
| 19 |
Assert.Equal(MethodImplAttributes.Runtime, constructor.MethodImplementationFlags); |
| 20 |
} |
| 21 |
|
| 22 |
[Fact] |
| 23 |
public void MethodImplementationFlags_NotSet() |
| 24 |
{ |
| 25 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); |
| 26 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) }); |
| 27 |
|
| 28 |
Assert.Equal(MethodImplAttributes.IL, constructor.MethodImplementationFlags); |
| 29 |
} |
| 30 |
|
| 31 |
[Fact] |
| 32 |
public void SetImplementationFlags_TypeAlreadyCreated_ThrowsInvalidOperationException() |
| 33 |
{ |
| 34 |
TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); |
| 35 |
ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]); |
| 36 |
constructor.GetILGenerator().Emit(OpCodes.Ret); |
| 37 |
|
| 38 |
Type createdType = type.CreateTypeInfo().AsType(); |
| 39 |
Assert.Throws<InvalidOperationException>(() => constructor.SetImplementationFlags(MethodImplAttributes.Runtime)); |
| 40 |
} |
| 41 |
} |
| 42 |
} |