+++ /dev/null
-// 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 System;
-
-using System.Collections.Generic;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class AssemblyGetModuleTests
- {
- [Fact]
- public void TestCreateDynamicModule()
- {
- AssemblyName myAsmName = new AssemblyName("TestAssembly1");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(myAssembly, "Module1");
- }
-
- [Fact]
- public void TestDefineDynamicModuleDefined()
- {
- AssemblyName myAsmName = new AssemblyName("TestAssembly2");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssembly, "Module1");
- TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld", TypeAttributes.Public);
- ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
- MethodAttributes.Public, CallingConventions.Standard, new Type[] { });
- ILGenerator myILGenerator = myConstructor.GetILGenerator();
- myILGenerator.Emit(OpCodes.Ldarg_1);
- }
-
- [Fact]
- public void TestDefineDynamicModuleWithLargeName()
- {
- AssemblyName myAsmName = new AssemblyName("TestAssembly3");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
- ModuleBuilder mb = myAssembly.DefineDynamicModule(new string('a', 259));
- }
-
- [Fact]
- public void TestThrowsExceptionOnEmptyName()
- {
- AssemblyName myAsmName = new AssemblyName("TestAssembly4");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
- Assert.Throws<ArgumentException>(() => { ModuleBuilder mb = myAssembly.DefineDynamicModule(""); });
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullName()
- {
- AssemblyName myAsmName = new AssemblyName("TestAssembly5");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
- Assert.Throws<ArgumentNullException>(() => { ModuleBuilder mb = myAssembly.DefineDynamicModule(null); });
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullTerminatedString()
- {
- AssemblyName myAsmName = new AssemblyName("TestAssembly6");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
- char[] chararray = new char[] { '\0', 't', 'e', 's', 't' };
- Assert.Throws<ArgumentException>(() => { ModuleBuilder mb = myAssembly.DefineDynamicModule(new string(chararray)); });
- }
-
-
- [Fact]
- public void TestThrowsExceptionOnMultipleModuleDefinition()
- {
- AssemblyName myAsmName = new AssemblyName("TestAssembly7");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);
- ModuleBuilder mb = myAssembly.DefineDynamicModule("module1");
- Assert.Throws<InvalidOperationException>(() => { ModuleBuilder mb2 = myAssembly.DefineDynamicModule("module2"); });
- }
- }
-}
+++ /dev/null
-// 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 System;
-
-using System.Reflection;
-using System.Reflection.Emit;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class AssemblyBuilderGetManifestResourceNames
- {
- [Fact]
- public void TestThrowsExceptionOnDynamicAssembly()
- {
- AssemblyName name = new AssemblyName("NegTest1Assembly");
- AssemblyBuilder builder = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
- Assert.Throws<NotSupportedException>(() => { string[] myName = builder.GetManifestResourceNames(); });
- }
- }
-}
+++ /dev/null
-// 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 System;
-
-using System.Reflection;
-using System.Reflection.Emit;
-using System.IO;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class AssemblyBuilderGetManifestResourceStream1
- {
- [Fact]
- public void TestThrowsExceptionOnMethodNotSupported()
- {
- AssemblyName name = new AssemblyName("NegTest1Assembly");
- AssemblyBuilder builder = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
- Assert.Throws<NotSupportedException>(() => { Stream myStream = builder.GetManifestResourceStream(""); });
- }
- }
-}
+++ /dev/null
-// 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 System;
-
-using System.Reflection;
-using System.Reflection.Emit;
-using System.IO;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class AssemblyBuilderGetManifestResourceStream2
- {
- [Fact]
- public void NegTest1()
- {
- AssemblyName name = new AssemblyName("NegTest1Assembly");
- AssemblyBuilder builder = AssemblyBuilder.DefineDynamicAssembly(name, AssemblyBuilderAccess.Run);
- Assert.Throws<NotSupportedException>(() => { Stream myStream = builder.GetManifestResourceStream(""); });
- }
- }
-}
+++ /dev/null
-// 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 System;
-
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Linq;
-using Xunit;
-using System.Collections.Generic;
-
-namespace System.Reflection.Emit.Tests
-{
- [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
- public class ABAttribute1 : Attribute
- {
- private bool _s;
-
- public ABAttribute1(bool s)
- {
- _s = s;
- }
- }
-
- public class AssemblyBuilderSetCustomAttribute1
- {
- [Fact]
- public void TestSetCustomAttribute()
- {
- AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("PT1"), AssemblyBuilderAccess.Run);
- ConstructorInfo c = typeof(ABAttribute1).GetConstructor(new Type[] { typeof(bool) });
- asmBuilder.SetCustomAttribute(c, new byte[] { 01, 00, 01 });
- IEnumerable<Attribute> attributes = asmBuilder.GetCustomAttributes();
- Assert.Equal("System.Reflection.Emit.Tests.ABAttribute1", attributes.First().ToString());
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullConstructorInfo()
- {
- AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("NT1"), AssemblyBuilderAccess.Run);
- Assert.Throws<ArgumentNullException>(() => { asmBuilder.SetCustomAttribute(null, new byte[] { }); });
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullByteArray()
- {
- AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("NT2"), AssemblyBuilderAccess.Run);
- ConstructorInfo dummyCtor = typeof(DateTime).GetConstructor(new Type[] { });
- Assert.Throws<ArgumentNullException>(() => { asmBuilder.SetCustomAttribute(dummyCtor, null); });
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Threading;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Linq;
-using Xunit;
-using System.Collections.Generic;
-
-namespace System.Reflection.Emit.Tests
-{
- [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
- public class ABAttribute2 : Attribute
- {
- public int i;
-
- public ABAttribute2(int i)
- {
- this.i = i;
- }
- }
-
- [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
- public class ABClassAttribute : Attribute
- {
- public int i;
-
- public ABClassAttribute(int i)
- {
- this.i = i;
- }
- }
-
- public class AssemblyBuilderSetCustomAttribute2
- {
- [Fact]
- public void TestSetCustomAttribute()
- {
- AssemblyName TestAssemblyName = new AssemblyName();
- TestAssemblyName.Name = "TestAssembly";
- AssemblyBuilder TestAssembly = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
- ConstructorInfo infoConstructor = typeof(ABClassAttribute).GetConstructor(new Type[] { typeof(int) });
- CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { 5 });
- TestAssembly.SetCustomAttribute(attributeBuilder);
- IEnumerable<Attribute> attributes = TestAssembly.GetCustomAttributes();
- Assert.Equal("System.Reflection.Emit.Tests.ABClassAttribute", attributes.First().ToString());
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullCustomAttributeBuilder()
- {
- AssemblyName TestAssemblyName = new AssemblyName();
- TestAssemblyName.Name = "TestAssembly";
- AssemblyBuilder TestAssembly = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
- CustomAttributeBuilder attributeBuilder = null;
- Assert.Throws<ArgumentNullException>(() => { TestAssembly.SetCustomAttribute(attributeBuilder); });
- }
- }
-}
--- /dev/null
+// 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 System.Collections.Generic;
+using System.Linq;
+using Xunit;
+
+namespace System.Reflection.Emit.Tests
+{
+ [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
+ public class BoolAllAttribute : Attribute
+ {
+ private bool _s;
+ public BoolAllAttribute(bool s) { _s = s; }
+ }
+
+ [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
+ public class IntClassAttribute : Attribute
+ {
+ public int i;
+ public IntClassAttribute(int i) { this.i = i; }
+ }
+
+ public class AssemblyTests
+ {
+ [Fact]
+ public void DefineDynamicModule()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ ModuleBuilder module = assembly.DefineDynamicModule("Module1");
+ TypeBuilder type = module.DefineType("HelloWorld", TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_1);
+ }
+
+ [Fact]
+ public void DefineDynamicModule_LargeName()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ ModuleBuilder module = assembly.DefineDynamicModule(new string('a', 259));
+ }
+
+ [Theory]
+ [InlineData(null, typeof(ArgumentNullException))]
+ [InlineData("", typeof(ArgumentException))]
+ [InlineData("\0test", typeof(ArgumentException))]
+ public void DefineDyamicModule_InvalidName_ThrowsArgumentException(string name, Type exceptionType)
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ Assert.Throws(exceptionType, () => assembly.DefineDynamicModule(name));
+ }
+
+ [Fact]
+ public void DefineDyamicModule_ModuleAlreadyDefined_ThrowsInvalidOperationException()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ ModuleBuilder mb = assembly.DefineDynamicModule("module1");
+ Assert.Throws<InvalidOperationException>(() => { ModuleBuilder mb2 = assembly.DefineDynamicModule("module2"); });
+ }
+
+ [Fact]
+ public void GetManifestResourceNames_ThrowsNotSupportedException()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ Assert.Throws<NotSupportedException>(() => assembly.GetManifestResourceNames());
+ }
+
+ [Fact]
+ public void GetManifestResourceStream_ThrowsNotSupportedException()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ Assert.Throws<NotSupportedException>(() => assembly.GetManifestResourceStream(""));
+ }
+
+
+ [Fact]
+ public void SetCustomAttribute_ConstructorBuidler_ByteArray()
+ {
+ AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("PT1"), AssemblyBuilderAccess.Run);
+ ConstructorInfo constructor = typeof(BoolAllAttribute).GetConstructor(new Type[] { typeof(bool) });
+ assembly.SetCustomAttribute(constructor, new byte[] { 01, 00, 01 });
+
+ IEnumerable<Attribute> attributes = assembly.GetCustomAttributes();
+ Assert.IsType<BoolAllAttribute>(attributes.First());
+ }
+
+ [Fact]
+ public void SetCustomAttribute_ConstructorBuidler_ByteArray_NullConstructorBuilder_ThrowsArgumentNullException()
+ {
+ AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("NT1"), AssemblyBuilderAccess.Run);
+ Assert.Throws<ArgumentNullException>("con", () => assembly.SetCustomAttribute(null, new byte[0]));
+ }
+
+ [Fact]
+ public void SetCustomAttribute_ConstructorBuidler_ByteArray_NullByteArray_ThrowsArgumentNullException()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ ConstructorInfo constructor = typeof(DateTime).GetConstructor(new Type[0]);
+ Assert.Throws<ArgumentNullException>("con", () => assembly.SetCustomAttribute(constructor, null));
+ }
+
+ [Fact]
+ public void SetCustomAttribute_CustomAttributeBuilder()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ ConstructorInfo constructor = typeof(IntClassAttribute).GetConstructor(new Type[] { typeof(int) });
+ CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(constructor, new object[] { 5 });
+ assembly.SetCustomAttribute(attributeBuilder);
+
+ IEnumerable<Attribute> attributes = assembly.GetCustomAttributes();
+ Assert.IsType<IntClassAttribute>(attributes.First());
+ }
+
+ [Fact]
+ public void SetCustomAttribute_CustomAttributeBuilder_NullAttributeBuilder_ThrowsArgumentNullException()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ Assert.Throws<ArgumentNullException>("customBuilder", () => assembly.SetCustomAttribute(null));
+ }
+ }
+}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
using Xunit;
namespace System.Reflection.Emit.Tests
public class ConstructorBuilderAttributes
{
[Fact]
- public void TestContainsAccessorAttribute()
+ public void CallingConvention_NullRequiredOptionalCustomModifiers()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
-
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb = tb.DefineConstructor(
- MethodAttributes.Public,
- CallingConventions.Standard,
- parameterTypes,
- null,
- null);
-
- Assert.Contains("Public", cb.Attributes.ToString());
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes, null, null);
+ Assert.Contains("Public", constructor.Attributes.ToString());
}
[Fact]
- public void TestContainsAccessorAttributeWithDifferentOverload()
+ public void CallingConvention_NoRequiredOptionalCustomModifiers()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
-
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
-
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
-
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- System.Reflection.Emit.ConstructorBuilder cb =
- tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- Assert.Contains("Public", cb.Attributes.ToString());
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
+ Assert.Contains("Public", constructor.Attributes.ToString());
}
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
+using System.Collections.Generic;
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class ConstructorBuilderCallingConvention
{
- private static CallingConventions[] s_supportedCConvs = new CallingConventions[] { CallingConventions.Any,
- CallingConventions.ExplicitThis, CallingConventions.HasThis, CallingConventions.Standard,
- CallingConventions.VarArgs };
-
- [Fact]
- public void TestCallingConventions()
+ public static IEnumerable<object[]> CallingConventions_TestData()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
-
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
- for (int i = 0; i < s_supportedCConvs.Length; i++)
- {
- TypeBuilder tb = mb.DefineType("DynamicRandomClass" + i.ToString(), TypeAttributes.Public);
-
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb =
- tb.DefineConstructor(MethodAttributes.Public, s_supportedCConvs[i], parameterTypes, null, null);
-
- Assert.Equal(CallingConventions.Standard, cb.CallingConvention);
- }
+ yield return new object[] { CallingConventions.Any };
+ yield return new object[] { CallingConventions.ExplicitThis };
+ yield return new object[] { CallingConventions.HasThis };
+ yield return new object[] { CallingConventions.Standard };
+ yield return new object[] { CallingConventions.VarArgs };
}
- [Fact]
- public void TestCallingConventionWithDifferentOverload()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void CallingConvention_NullRequiredOptionalCustomModifiers(CallingConventions callingConvention)
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
-
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- for (int i = 0; i < s_supportedCConvs.Length; i++)
- {
- TypeBuilder tb = mb.DefineType("DynamicRandomClass" + i.ToString(), TypeAttributes.Public);
- tb.DefineGenericParameters("T");
- Type[] parameterTypes = { typeof(int), typeof(double) };
+ ConstructorBuilder cb = type.DefineConstructor(MethodAttributes.Public, callingConvention, parameterTypes, null, null);
+ Assert.Equal(CallingConventions.Standard, cb.CallingConvention);
+ }
- ConstructorBuilder cb =
- tb.DefineConstructor(MethodAttributes.Public, s_supportedCConvs[i], parameterTypes);
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void CallingConvention_NoRequiredOptionalCustomModifiers(CallingConventions callingConvention)
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- Assert.Equal(CallingConventions.HasThis, cb.CallingConvention);
- }
+ ConstructorBuilder cb = type.DefineConstructor(MethodAttributes.Public, callingConvention, parameterTypes);
+ Assert.Equal(CallingConventions.Standard, cb.CallingConvention);
}
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
using Xunit;
namespace System.Reflection.Emit.Tests
public class ConstructorBuilderDeclaringType
{
[Fact]
- public void TestDeclaringType()
+ public void DeclaringType_NullRequiredOptionalCustomModifiers()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
-
-
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
-
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb = tb.DefineConstructor(
- MethodAttributes.Public,
- CallingConventions.Standard,
- parameterTypes,
- null,
- null);
-
- Assert.Equal(tb.AsType(), cb.DeclaringType);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes, null, null);
+ Assert.Equal(type.AsType(), constructor.DeclaringType);
}
[Fact]
- public void TestDeclaringTypeWithDifferentOverload()
+ public void DeclaringType_NoRequiredOptionalCustomModifiers()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
-
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
-
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
Type[] parameterTypes = { typeof(int), typeof(double) };
- ConstructorBuilder cb =
- tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
-
- Assert.Equal(tb.AsType(), cb.DeclaringType);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
+ Assert.Equal(type.AsType(), constructor.DeclaringType);
}
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class ConstructorBuilderDefineParameter
{
- private const string AssemblyName = "ConstructorBuilderDefineParameter";
- private const string DefaultModuleName = "DynamicModule";
- private const string DefaultTypeName = "DynamicType";
- private const AssemblyBuilderAccess DefaultAssemblyBuilderAccess = AssemblyBuilderAccess.Run;
- private const MethodAttributes DefaultMethodAttribute = MethodAttributes.Public;
- private const CallingConventions DefaultCallingConvention = CallingConventions.Standard;
-
- private TypeBuilder _typeBuilder;
-
- private ParameterAttributes[] _supportedAttributes = new ParameterAttributes[] {
- ParameterAttributes.None,
- ParameterAttributes.HasDefault,
- ParameterAttributes.HasFieldMarshal,
- ParameterAttributes.In,
- ParameterAttributes.None,
- ParameterAttributes.Optional,
- ParameterAttributes.Out,
- ParameterAttributes.Retval };
- private ModuleBuilder _testModuleBuilder;
-
- private ModuleBuilder TestModuleBuilder
+ private static readonly ParameterAttributes[] s_supportedAttributes = new ParameterAttributes[]
{
- get
- {
- AssemblyName name = new AssemblyName(AssemblyName);
- AssemblyBuilder assembly = AssemblyBuilder.DefineDynamicAssembly(name, DefaultAssemblyBuilderAccess);
- _testModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(assembly, "Module1");
-
- return _testModuleBuilder;
- }
- }
+ ParameterAttributes.None,
+ ParameterAttributes.HasDefault,
+ ParameterAttributes.HasFieldMarshal,
+ ParameterAttributes.In,
+ ParameterAttributes.None,
+ ParameterAttributes.Optional,
+ ParameterAttributes.Out,
+ ParameterAttributes.Retval
+ };
[Fact]
- public void TestValidDataWithMultipleParameters()
+ public void DefineParameter_MultipleParameters()
{
- int i = 0;
- Type[] parameterTypes = new Type[]
- {
- typeof(int),
- typeof(string)
- };
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(string) };
- for (i = 1; i < _supportedAttributes.Length; ++i)
+ for (int i = 1; i < s_supportedAttributes.Length; i++)
{
- ConstructorBuilder constructor = CreateConstructorBuilder("PosTest1_Type" + i,
- parameterTypes);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
- constructor.DefineParameter(1, _supportedAttributes[i - 1], "parameter1" + i);
- constructor.DefineParameter(2, _supportedAttributes[i], "parameter2" + i);
+ constructor.DefineParameter(1, s_supportedAttributes[i - 1], "parameter1" + i);
+ constructor.DefineParameter(2, s_supportedAttributes[i], "parameter2" + i);
- ILGenerator ilg = constructor.GetILGenerator();
- ilg.Emit(OpCodes.Ldarg_0);
- ilg.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { }));
- ilg.Emit(OpCodes.Ret);
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_0);
+ ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[0]));
+ ilGenerator.Emit(OpCodes.Ret);
- _typeBuilder.CreateTypeInfo().AsType();
- ParameterInfo[] definedParams = constructor.GetParameters();
+ type.CreateTypeInfo().AsType();
+ ParameterInfo[] definedParams = constructor.GetParameters();
Assert.Equal(2, definedParams.Length);
}
}
[Fact]
- public void TestValidDataWithSingleParameter()
+ public void DefineParameter_SingleParameter()
{
- int i = 0;
+ Type[] parameterTypes = new Type[] { typeof(ConstructorBuilderDefineParameter) };
- Type[] parameterTypes = new Type[]
+ for (int i = 0; i < s_supportedAttributes.Length; i++)
{
- typeof(ConstructorBuilderDefineParameter)
- };
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
- for (; i < _supportedAttributes.Length; ++i)
- {
- ConstructorBuilder constructor = CreateConstructorBuilder("PosTest2_Type" + i,
- parameterTypes);
+ constructor.DefineParameter(1, s_supportedAttributes[i], "parameter1" + i);
- constructor.DefineParameter(1, _supportedAttributes[i], "parameter1" + i);
- ILGenerator ilg = constructor.GetILGenerator();
- ilg.Emit(OpCodes.Ldarg_0);
- ilg.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { }));
- ilg.Emit(OpCodes.Ret);
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_0);
+ ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { }));
+ ilGenerator.Emit(OpCodes.Ret);
- _typeBuilder.CreateTypeInfo().AsType();
- ParameterInfo[] definedParams = constructor.GetParameters();
+ type.CreateTypeInfo().AsType();
+ ParameterInfo[] definedParams = constructor.GetParameters();
Assert.Equal(1, definedParams.Length);
}
}
[Fact]
- public void TestValidDataWithNullName()
+ public void DefineParameter_SingleParameter_NullParameterName()
{
- int i = 0;
+ Type[] parameterTypes = new Type[] { typeof(ConstructorBuilderDefineParameter) };
- Type[] parameterTypes = new Type[]
+ for (int i = 0; i < s_supportedAttributes.Length; i++)
{
- typeof(ConstructorBuilderDefineParameter)
- };
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
- for (; i < _supportedAttributes.Length; ++i)
- {
- ConstructorBuilder constructor = CreateConstructorBuilder("PosTest3_Type" + i,
- parameterTypes);
+ constructor.DefineParameter(1, s_supportedAttributes[i], null);
+
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_0);
+ ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { }));
+ ilGenerator.Emit(OpCodes.Ret);
- constructor.DefineParameter(1, _supportedAttributes[i], null);
- ILGenerator ilg = constructor.GetILGenerator();
- ilg.Emit(OpCodes.Ldarg_0);
- ilg.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { }));
- ilg.Emit(OpCodes.Ret);
+ type.CreateTypeInfo().AsType();
- _typeBuilder.CreateTypeInfo().AsType();
ParameterInfo[] definedParams = constructor.GetParameters();
Assert.Equal(1, definedParams.Length);
}
}
[Fact]
- public void TestValidDataWithMultipleParametersAndNullName()
+ public void DefineParameter_MultipleParameters_NullParameterNames()
{
- int i = 0;
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(string) };
- Type[] parameterTypes = new Type[]
+ for (int i = 1; i < s_supportedAttributes.Length; ++i)
{
- typeof(int),
- typeof(string)
- };
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
- for (i = 1; i < _supportedAttributes.Length; ++i)
- {
- ConstructorBuilder constructor = CreateConstructorBuilder("PosTest4_Type" + i,
- parameterTypes);
+ constructor.DefineParameter(1, s_supportedAttributes[i - 1], null);
+ constructor.DefineParameter(2, s_supportedAttributes[i], null);
+
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_0);
+ ilGenerator.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { }));
+ ilGenerator.Emit(OpCodes.Ret);
- constructor.DefineParameter(1, _supportedAttributes[i - 1], null);
- constructor.DefineParameter(2, _supportedAttributes[i], null);
- ILGenerator ilg = constructor.GetILGenerator();
- ilg.Emit(OpCodes.Ldarg_0);
- ilg.Emit(OpCodes.Call, typeof(object).GetConstructor(new Type[] { }));
- ilg.Emit(OpCodes.Ret);
+ type.CreateTypeInfo().AsType();
- _typeBuilder.CreateTypeInfo().AsType();
ParameterInfo[] definedParams = constructor.GetParameters();
Assert.Equal(2, definedParams.Length);
}
}
[Fact]
- public void TestParameterWithZeroSequence()
+ public void DefineParameter_ZeroSequence_NoParameters()
{
- CreateConstructorBuilder("PosTest5_Type1", new Type[] { }).DefineParameter(0, ParameterAttributes.None, "p");
- CreateConstructorBuilder("PosTest5_Type5", new Type[] { typeof(int) }).DefineParameter(0, ParameterAttributes.None, "p");
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Any, new Type[0]);
+ Assert.NotNull(constructor.DefineParameter(0, ParameterAttributes.None, "p"));
}
[Fact]
- public void TestThrowsExceptionOnIncorrectPosition()
+ public void DefineParameter_ZeroSequence_SingleParameter()
{
- // ArgumentOutOfRangeException should be thrown when position is less than or equal to zero, or it is greater than the number of parameters of the constructor.
- NegTestCaseVerificationHelper(CreateConstructorBuilder("NegTest1_Type2", new Type[] { }),
- -1, ParameterAttributes.None, "p", typeof(ArgumentOutOfRangeException));
- NegTestCaseVerificationHelper(CreateConstructorBuilder("NegTest1_Type3", new Type[] { }),
- 1, ParameterAttributes.None, "p", typeof(ArgumentOutOfRangeException));
- NegTestCaseVerificationHelper(CreateConstructorBuilder("NegTest1_Type4", new Type[] { typeof(int) }),
- 2, ParameterAttributes.None, "p", typeof(ArgumentOutOfRangeException));
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Any, new Type[0]);
+ Assert.NotNull(constructor.DefineParameter(0, ParameterAttributes.None, "p"));
}
[Fact]
- public void TestThrowsExceptionOnCreateTypeCalled()
+ public void DefineParameter_NoParameters_InvalidSequence_ThrowsArgumentOutOfRangeException()
{
- ConstructorBuilder constructor = CreateConstructorBuilder("NegTest2_Type1", new Type[] { typeof(int) });
- constructor.GetILGenerator().Emit(OpCodes.Ret);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
- TypeBuilder type = _typeBuilder;
- type.CreateTypeInfo().AsType();
-
- NegTestCaseVerificationHelper(constructor,
- 1, ParameterAttributes.None, "p", typeof(InvalidOperationException));
+ Assert.Throws<ArgumentOutOfRangeException>(() => constructor.DefineParameter(-1, ParameterAttributes.None, "p"));
+ Assert.Throws<ArgumentOutOfRangeException>(() => constructor.DefineParameter(1, ParameterAttributes.None, "p"));
}
- private ConstructorBuilder CreateConstructorBuilder(string typeName, Type[] parameterTypes)
+ [Fact]
+ public void DefineParameter_SingleParameter_InvalidSequence_ThrowsArgumentOutOfRangeException()
{
- _typeBuilder = TestModuleBuilder.DefineType(typeName);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
- return _typeBuilder.DefineConstructor(
- DefaultMethodAttribute,
- DefaultCallingConvention,
- parameterTypes);
+ Assert.Throws<ArgumentOutOfRangeException>(() => constructor.DefineParameter(2, ParameterAttributes.None, "p"));
}
- private void NegTestCaseVerificationHelper(
- ConstructorBuilder constructor,
- int sequence,
- ParameterAttributes attribute,
- string paramName,
- Type desiredException)
+ [Fact]
+ public void DefineParameter_TypeAlreadyCreated_ThrowsInvalidOperationException()
{
- Assert.Throws(desiredException, () => { constructor.DefineParameter(sequence, attribute, paramName); });
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
+
+ constructor.GetILGenerator().Emit(OpCodes.Ret);
+ type.CreateTypeInfo().AsType();
+
+ Assert.Throws<InvalidOperationException>(() => constructor.DefineParameter(1, ParameterAttributes.None, "p"));
}
}
}
--- /dev/null
+// 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 ConstructorBuilderGetILGenerator1
+ {
+ private static readonly RandomDataGenerator s_randomDataGenerator = new RandomDataGenerator();
+ private static int s_randomInt = s_randomDataGenerator.GetInt16();
+
+ [Theory]
+ [InlineData(MethodAttributes.Assembly)]
+ [InlineData(MethodAttributes.CheckAccessOnOverride)]
+ [InlineData(MethodAttributes.FamANDAssem)]
+ [InlineData(MethodAttributes.Family)]
+ [InlineData(MethodAttributes.FamORAssem)]
+ [InlineData(MethodAttributes.Final)]
+ [InlineData(MethodAttributes.HasSecurity)]
+ [InlineData(MethodAttributes.HideBySig)]
+ [InlineData(MethodAttributes.MemberAccessMask)]
+ [InlineData(MethodAttributes.NewSlot)]
+ [InlineData(MethodAttributes.Private)]
+ [InlineData(MethodAttributes.PrivateScope)]
+ [InlineData(MethodAttributes.Public)]
+ [InlineData(MethodAttributes.RequireSecObject)]
+ [InlineData(MethodAttributes.ReuseSlot)]
+ [InlineData(MethodAttributes.RTSpecialName)]
+ [InlineData(MethodAttributes.SpecialName)]
+ [InlineData(MethodAttributes.Static)]
+ [InlineData(MethodAttributes.UnmanagedExport)]
+ [InlineData(MethodAttributes.Virtual)]
+ [InlineData(MethodAttributes.VtableLayoutMask)]
+ public void GetILGenerator_ReturnsNonNull(MethodAttributes attributes)
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(attributes, CallingConventions.Standard, new Type[0]);
+ Assert.NotNull(constructor.GetILGenerator());
+ Assert.NotNull(constructor.GetILGenerator(s_randomInt));
+ }
+
+ [Fact]
+ public void GetILGenerator_NoMethodBodyAttribute_ThrowsInvalidOperationException()
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.PinvokeImpl, CallingConventions.Standard, new Type[0]);
+ Assert.Throws<InvalidOperationException>(() => constructor.GetILGenerator());
+ Assert.Throws<InvalidOperationException>(() => constructor.GetILGenerator(s_randomInt));
+ }
+
+ [Fact]
+ public void GetILGenerator_DefaultConstructor_ThrowsInvalidOperationException()
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.NotPublic);
+ ConstructorBuilder constructor = type.DefineDefaultConstructor(MethodAttributes.Public);
+ Assert.Throws<InvalidOperationException>(() => constructor.GetILGenerator());
+ Assert.Throws<InvalidOperationException>(() => constructor.GetILGenerator(s_randomInt));
+ }
+ }
+}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class ConstructorBuilderGetILGenerator1
- {
- private const string AssemblyName = "ConstructorBuilderGetILGenerator1";
- private const string DefaultModuleName = "DynamicModule";
- private const string DefaultTypeName = "DynamicType";
- private const AssemblyBuilderAccess DefaultAssemblyBuilderAccess = AssemblyBuilderAccess.Run;
- private const CallingConventions DefaultCallingConvention = CallingConventions.Standard;
-
- private ModuleBuilder TestModuleBuilder
- {
- get
- {
- AssemblyName name = new AssemblyName(AssemblyName);
- AssemblyBuilder assembly =
- AssemblyBuilder.DefineDynamicAssembly(name, DefaultAssemblyBuilderAccess);
- return TestLibrary.Utilities.GetModuleBuilder(assembly, "Module1");
- }
- }
-
- [Fact]
- public void TestILGeneratorOnNonDefaultConstructor()
- {
- int i = 0;
- MethodAttributes[] attributes = new MethodAttributes[] {
- MethodAttributes.Assembly,
- MethodAttributes.CheckAccessOnOverride,
- MethodAttributes.FamANDAssem,
- MethodAttributes.Family,
- MethodAttributes.FamORAssem,
- MethodAttributes.Final,
- MethodAttributes.HasSecurity,
- MethodAttributes.HideBySig,
- MethodAttributes.MemberAccessMask,
- MethodAttributes.NewSlot,
- MethodAttributes.Private,
- MethodAttributes.PrivateScope,
- MethodAttributes.Public,
- MethodAttributes.RequireSecObject,
- MethodAttributes.ReuseSlot,
- MethodAttributes.RTSpecialName,
- MethodAttributes.SpecialName,
- MethodAttributes.Static,
- MethodAttributes.UnmanagedExport,
- MethodAttributes.Virtual,
- MethodAttributes.VtableLayoutMask
- };
-
- for (; i < attributes.Length; ++i)
- {
- ILGenerator generator =
- CreateConstructorBuilder("PosTest1_Type" + i, attributes[i]).GetILGenerator();
- Assert.NotNull(generator);
- }
- }
-
- [Fact]
- public void TestThrowsExceptionWithNoMethodyBody()
- {
- // InvalidOperationException should be thrown when The constructor has MethodAttributes or MethodImplAttributes flags indicating that it should not have a method body.
- Assert.Throws<InvalidOperationException>(() =>
- {
- ILGenerator generator = CreateConstructorBuilder("NegTest1_Type1", MethodAttributes.PinvokeImpl).GetILGenerator();
- });
- }
-
- [Fact]
- public void TestThrowsExceptionOnDefaultConstructor()
- {
- TypeBuilder type = TestModuleBuilder.DefineType("NegTest2_Type1");
-
- ConstructorBuilder constructor = type.DefineDefaultConstructor(MethodAttributes.Public);
- Assert.Throws<InvalidOperationException>(() => { constructor.GetILGenerator(); });
- }
-
- private ConstructorBuilder CreateConstructorBuilder(string typeName, MethodAttributes attribute)
- {
- TypeBuilder type = TestModuleBuilder.DefineType(typeName);
-
- return type.DefineConstructor(
- attribute,
- DefaultCallingConvention,
- new Type[] { });
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class ConstructorBuilderGetILGenerator2
- {
- private const string AssemblyName = "ConstructorBuilderGetILGenerator2";
- private const string DefaultModuleName = "DynamicModule";
- private const string DefaultTypeName = "DynamicType";
- private const AssemblyBuilderAccess DefaultAssemblyBuilderAccess = AssemblyBuilderAccess.Run;
- private const CallingConventions DefaultCallingConvention = CallingConventions.Standard;
- private readonly RandomDataGenerator _generator = new RandomDataGenerator();
-
- private ModuleBuilder TestModuleBuilder
- {
- get
- {
- AssemblyName name = new AssemblyName(AssemblyName);
- AssemblyBuilder assembly =
- AssemblyBuilder.DefineDynamicAssembly(name, DefaultAssemblyBuilderAccess);
- _testModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(assembly, "Module1");
-
-
- return _testModuleBuilder;
- }
- }
-
- private ModuleBuilder _testModuleBuilder;
-
- [Fact]
- public void TestIlGeneratorOnNonDefaultConstructor()
- {
- int i = 0;
- int randValue = 0;
- randValue = _generator.GetInt16();
- MethodAttributes[] attributes = new MethodAttributes[] {
- MethodAttributes.Assembly,
- MethodAttributes.CheckAccessOnOverride,
- MethodAttributes.FamANDAssem,
- MethodAttributes.Family,
- MethodAttributes.FamORAssem,
- MethodAttributes.Final,
- MethodAttributes.HasSecurity,
- MethodAttributes.HideBySig,
- MethodAttributes.MemberAccessMask,
- MethodAttributes.NewSlot,
- MethodAttributes.Private,
- MethodAttributes.PrivateScope,
- MethodAttributes.Public,
- MethodAttributes.RequireSecObject,
- MethodAttributes.ReuseSlot,
- MethodAttributes.RTSpecialName,
- MethodAttributes.SpecialName,
- MethodAttributes.Static,
- MethodAttributes.UnmanagedExport,
- MethodAttributes.Virtual,
- MethodAttributes.VtableLayoutMask
- };
-
- for (; i < attributes.Length; ++i)
- {
- ILGenerator generator =
- CreateConstructorBuilder("PosTest1_Type" + i, attributes[i]).GetILGenerator(randValue);
- Assert.NotNull(generator);
- }
- }
-
- [Fact]
- public void TestThrowsExceptionWithNoMethodyBody()
- {
- int randValue = 0;
- randValue = _generator.GetInt16();
- Assert.Throws<InvalidOperationException>(() =>
- {
- ILGenerator generator = CreateConstructorBuilder("NegTest1_Type1", MethodAttributes.PinvokeImpl).GetILGenerator(randValue);
- });
- }
-
- [Fact]
- public void TestThrowsExceptionOnDefaultConstructor()
- {
- int randValue = 0;
- randValue = _generator.GetInt16();
- TypeBuilder type = TestModuleBuilder.DefineType("NegTest2_Type1");
-
- Assert.Throws<InvalidOperationException>(() =>
- {
- ConstructorBuilder constructor = type.DefineDefaultConstructor(MethodAttributes.Public);
- constructor.GetILGenerator(randValue);
- });
- }
-
- private ConstructorBuilder CreateConstructorBuilder(string typeName, MethodAttributes attribute)
- {
- TypeBuilder type = TestModuleBuilder.DefineType(typeName);
-
- return type.DefineConstructor(
- attribute,
- DefaultCallingConvention,
- new Type[] { });
- }
- }
-}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
using Xunit;
namespace System.Reflection.Emit.Tests
public class ConstructorBuilderInitLocals
{
[Fact]
- public void TestInitLocalsEqualsZero()
+ public void InitLocals()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes, null, null);
+ Assert.True(constructor.InitLocals);
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb = tb.DefineConstructor(
- MethodAttributes.Public,
- CallingConventions.Standard,
- parameterTypes,
- null,
- null);
-
- Assert.True(cb.InitLocals);
- }
-
- [Fact]
- public void TestInitLocalsNonNull()
- {
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
-
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
-
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb = tb.DefineConstructor(
- MethodAttributes.Public,
- CallingConventions.Standard,
- parameterTypes,
- null,
- null);
-
- cb.InitLocals = false;
-
- Assert.False(cb.InitLocals);
+ constructor.InitLocals = false;
+ Assert.False(constructor.InitLocals);
}
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
using Xunit;
namespace System.Reflection.Emit.Tests
public class ConstructorBuilderName
{
[Fact]
- public void TestName()
+ public void Name()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb = tb.DefineConstructor(
- MethodAttributes.Public,
- CallingConventions.Standard,
- parameterTypes,
- null,
- null);
-
- Assert.Equal(".ctor", cb.Name);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes, null, null);
+ Assert.Equal(".ctor", constructor.Name);
}
}
}
--- /dev/null
+// 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 System.Linq;
+using Xunit;
+
+namespace System.Reflection.Emit.Tests
+{
+ [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
+ public class IntAllAttribute : Attribute
+ {
+ public int _i;
+ public IntAllAttribute(int i) { _i = i; }
+ }
+
+ public class ConstructorBuilderSetCustomAttribute
+ {
+ [Fact]
+ public void SetCustomAttribute_ConstructorBuilder_ByteArray()
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
+
+ ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
+ constructor.SetCustomAttribute(attributeConstructor, new byte[] { 01, 00, 05, 00, 00, 00 });
+ }
+
+ [Fact]
+ public void SetCustomAttribute_ConstructorBuilder_ByteArray_NullConstructorBuilder_ThrowsArgumentNullException()
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
+ Assert.Throws<ArgumentNullException>("con", () => constructor.SetCustomAttribute(null, new byte[] { 01, 00, 05, 00, 00, 00 }));
+ }
+
+ [Fact]
+ public void SetCustomAttribute_CustomAttributeBuilder()
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_1);
+
+ ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[1] { typeof(int) });
+ CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[1] { 2 });
+
+ constructor.SetCustomAttribute(attributeBuilder);
+ Type createdType = type.CreateTypeInfo().AsType();
+
+ ConstructorInfo createdConstructor = createdType.GetConstructor(new Type[0]);
+ Attribute[] customAttributes = createdConstructor.GetCustomAttributes(true).ToArray();
+
+ Assert.Equal(1, customAttributes.Length);
+ Assert.Equal(2, ((IntAllAttribute)customAttributes[0])._i);
+ }
+
+ [Fact]
+ public void SetCustomAttribute_NullCustomAtributeBuilder_ThrowsArgumentNullException()
+ {
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_1);
+
+ Assert.Throws<ArgumentNullException>("customBuilder", () => constructor.SetCustomAttribute(null));
+ }
+ }
+}
+++ /dev/null
-// 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 System;
-using System.Threading;
-using System.Reflection;
-using System.Reflection.Emit;
-
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
- public class CBMyAttribute1 : Attribute
- {
- public int i;
-
- public CBMyAttribute1(int i)
- {
- this.i = i;
- }
- }
-
- public class ConstructorBuilderSetCustomAttribute1
- {
- [Fact]
- public void TestSetCustomAttribute()
- {
- AssemblyName TestAssemblyName = new AssemblyName("TestAssembly");
- AssemblyBuilder TestAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
-
- ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssemblyBuilder, "Module1");
-
- TypeBuilder TestTypeBuilder = TestModuleBuilder.DefineType("TestTypeBuilder", TypeAttributes.Public);
- ConstructorBuilder TestConstructorBuilder =
- TestTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
- Type myType = typeof(CBMyAttribute1);
- ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[] { typeof(int) });
- TestConstructorBuilder.SetCustomAttribute(myConstructorInfo, new byte[] { 01, 00, 05, 00, 00, 00 });
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullConstructorInfo()
- {
- AssemblyName TestAssemblyName = new AssemblyName("TestAssembly");
- AssemblyBuilder TestAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
-
- ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssemblyBuilder, "Module1");
-
- TypeBuilder TestTypeBuilder = TestModuleBuilder.DefineType("TestTypeBuilder", TypeAttributes.Public);
- ConstructorBuilder TestConstructorBuilder =
- TestTypeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(int) });
- Assert.Throws<ArgumentNullException>(() => { TestConstructorBuilder.SetCustomAttribute(null, new byte[] { 01, 00, 05, 00, 00, 00 }); });
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Threading;
-using System.Reflection;
-using System.Reflection.Emit;
-using Xunit;
-using System.Linq;
-
-namespace System.Reflection.Emit.Tests
-{
- [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
- public class CBMyAttribute2 : Attribute
- {
- public int m_i;
-
- public CBMyAttribute2(int i)
- {
- m_i = i;
- }
- }
-
- public class ConstructorBuilderSetCustomAttribute2
- {
- [Fact]
- public void TestSetCustomAttribute()
- {
- AssemblyName myAssemblyName = new AssemblyName("EmittedAssembly");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssembly, "Module1");
- TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld", TypeAttributes.Public);
- ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
- MethodAttributes.Public, CallingConventions.Standard, new Type[] { });
- ILGenerator myILGenerator = myConstructor.GetILGenerator();
- myILGenerator.Emit(OpCodes.Ldarg_1);
-
- ConstructorInfo myConstructorInfo = typeof(CBMyAttribute2).GetConstructor(new Type[1] { typeof(int) });
- CustomAttributeBuilder attributeBuilder =
- new CustomAttributeBuilder(myConstructorInfo, new object[1] { 2 });
-
- myConstructor.SetCustomAttribute(attributeBuilder);
- Type myHelloworld = myTypeBuilder.CreateTypeInfo().AsType();
-
- ConstructorInfo myReflectConstructorInfo = myHelloworld.GetConstructor(new Type[] { });
- object[] CBMyAttribute2s = myReflectConstructorInfo.GetCustomAttributes(true).Select(a => (object)a).ToArray();
-
- Assert.Equal(1, CBMyAttribute2s.Length);
- Assert.Equal(2, ((CBMyAttribute2)CBMyAttribute2s[0]).m_i);
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullCustomAttributeBuilder()
- {
- AssemblyName myAssemblyName = new AssemblyName("EmittedAssembly");
- AssemblyBuilder myAssembly = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssembly, "Module1");
-
- TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld", TypeAttributes.Public);
- ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
- MethodAttributes.Public, CallingConventions.Standard, new Type[] { });
- ILGenerator myILGenerator = myConstructor.GetILGenerator();
- myILGenerator.Emit(OpCodes.Ldarg_1);
- Assert.Throws<ArgumentNullException>(() => { myConstructor.SetCustomAttribute(null); });
- }
- }
-}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Threading;
-using System.Reflection;
-using System.Reflection.Emit;
using Xunit;
namespace System.Reflection.Emit.Tests
public class ConstructorBuilderSetImplementationFlags
{
[Fact]
- public void TestImplFlags()
+ public void MethodImplementationFlags_SetToCustomValue()
{
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "TempAssembly";
- AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) });
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
+ constructor.SetImplementationFlags(MethodImplAttributes.Runtime);
+ MethodImplAttributes methodImplementationFlags = constructor.MethodImplementationFlags;
+ int methodImplementationValue = (int)methodImplementationFlags;
- TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("TempClass", TypeAttributes.Public);
- ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
- MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) });
-
- myConstructor.SetImplementationFlags(MethodImplAttributes.Runtime);
-
- MethodImplAttributes myMethodAttributes = myConstructor.MethodImplementationFlags;
-
- int myAttribValue = (int)myMethodAttributes;
-
- FieldInfo[] myFieldInfo = typeof(MethodImplAttributes).GetFields(BindingFlags.Public | BindingFlags.Static);
-
- for (int i = 0; i < myFieldInfo.Length; i++)
+ FieldInfo[] fields = typeof(MethodImplAttributes).GetFields(BindingFlags.Public | BindingFlags.Static);
+ for (int i = 0; i < fields.Length; i++)
{
- if (myFieldInfo[i].Name == "Runtime")
+ if (fields[i].Name == "Runtime")
{
- int myFieldValue = (int)myFieldInfo[i].GetValue(null);
- Assert.Equal(myFieldValue, (myFieldValue & myAttribValue));
+ int fieldValue = (int)fields[i].GetValue(null);
+ Assert.Equal(fieldValue, (fieldValue & methodImplementationValue));
}
}
}
[Fact]
- public void TestImplFlagsNotChanged()
+ public void MethodImplementationFlags_NotSet()
{
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "TempAssembly";
- AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) });
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
+ MethodImplAttributes methodImplementationFlags = constructor.MethodImplementationFlags;
+ int methodImplementationValue = (int)methodImplementationFlags;
- TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("TempClass", TypeAttributes.Public);
- ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
- MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(string) });
-
- MethodImplAttributes myMethodAttributes = myConstructor.MethodImplementationFlags;
-
- int myAttribValue = (int)myMethodAttributes;
-
- FieldInfo[] myFieldInfo = typeof(MethodImplAttributes).GetFields(BindingFlags.Public | BindingFlags.Static);
-
- for (int i = 0; i < myFieldInfo.Length; i++)
+ FieldInfo[] fields = typeof(MethodImplAttributes).GetFields(BindingFlags.Public | BindingFlags.Static);
+ for (int i = 0; i < fields.Length; i++)
{
- if (myFieldInfo[i].Name == "Runtime")
+ if (fields[i].Name == "Runtime")
{
- int myFieldValue = (int)myFieldInfo[i].GetValue(null);
- Assert.NotEqual((myFieldValue & myAttribValue), myFieldValue);
+ int fieldValue = (int)fields[i].GetValue(null);
+ Assert.NotEqual(fieldValue, (fieldValue & methodImplementationValue));
}
}
}
[Fact]
- public void TestThrowsExceptionOnCreateTypeCalled()
+ public void SetImplementationFlags_TypeAlreadyCreated_ThrowsInvalidOperationException()
{
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "TempAssembly";
- AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
-
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[0]);
- TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("TempClass", TypeAttributes.Public);
+ ILGenerator ilGenerator = constructor.GetILGenerator();
+ ilGenerator.Emit(OpCodes.Ldarg_1);
- ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
- MethodAttributes.Public, CallingConventions.Standard, new Type[] { });
- ILGenerator myILGenerator = myConstructor.GetILGenerator();
- myILGenerator.Emit(OpCodes.Ldarg_1);
- Type myType = myTypeBuilder.CreateTypeInfo().AsType();
- Assert.Throws<InvalidOperationException>(() => { myConstructor.SetImplementationFlags(MethodImplAttributes.Runtime); });
+ Type createdType = type.CreateTypeInfo().AsType();
+ Assert.Throws<InvalidOperationException>(() => constructor.SetImplementationFlags(MethodImplAttributes.Runtime));
}
}
}
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Reflection;
-using System.Reflection.Emit;
using Xunit;
namespace System.Reflection.Emit.Tests
public class ConstructorBuilderToString
{
[Fact]
- public void TestToString()
+ public void ToString_NullRequiredOptionalCustomModifiers()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
-
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb =
- tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes, null, null);
-
- Assert.StartsWith("Name: .ctor", cb.ToString());
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes, null, null);
+ Assert.StartsWith("Name: .ctor", constructor.ToString());
}
[Fact]
- public void TestToStringWithDifferentOverload()
+ public void ToString_NoRequiredOptionalCustomModifiers()
{
- AssemblyName an = new AssemblyName();
- an.Name = "DynamicRandomAssembly";
- AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
-
- ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1");
- TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public);
-
- Type[] parameterTypes = { typeof(int), typeof(double) };
-
- ConstructorBuilder cb =
- tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
+ TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public);
+ Type[] parameterTypes = new Type[] { typeof(int), typeof(double) };
- Assert.StartsWith("Name: .ctor", cb.ToString());
+ ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes);
+ Assert.StartsWith("Name: .ctor", constructor.ToString());
}
}
}
--- /dev/null
+// 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 System.Linq;
+using Xunit;
+
+namespace System.Reflection.Emit.Tests
+{
+ public class EnumBuilderMethodTests
+ {
+ [Fact]
+ public void DefineLiteral()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ FieldBuilder field = enumBuilder.DefineLiteral("FieldOne", 1);
+ enumBuilder.AsType();
+ Assert.True(field.IsLiteral);
+ Assert.True(field.IsPublic);
+ Assert.True(field.IsStatic);
+ }
+
+ [Fact]
+ public void GetElementType_ThrowsNotSupportedException()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ Assert.Throws<NotSupportedException>(() => enumBuilder.GetElementType());
+ }
+
+ [Fact]
+ public void MakeArrayType()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
+ Type arrayType = enumBuilder.MakeArrayType();
+ Assert.Equal(typeof(Array), arrayType.GetTypeInfo().BaseType);
+ Assert.Equal("TestEnum[]", arrayType.Name);
+ }
+
+ [Theory]
+ [InlineData(1)]
+ [InlineData(2)]
+ [InlineData(260)]
+ public void MakeArrayType_Int(int rank)
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
+ Type arrayType = enumBuilder.MakeArrayType(rank);
+
+ string ranks = rank == 1 ? "*" : string.Empty;
+ for (int i = 1; i < rank; i++)
+ {
+ ranks += ",";
+ }
+
+ Assert.Equal(typeof(Array), arrayType.GetTypeInfo().BaseType);
+ Assert.Equal($"TestEnum[{ranks}]", arrayType.Name);
+ }
+
+ [Theory]
+ [InlineData(0)]
+ [InlineData(-1)]
+ public void MakeArrayType_Int_RankLessThanOne_ThrowsIndexOutOfRange(int rank)
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
+ Assert.Throws<IndexOutOfRangeException>(() => enumBuilder.MakeArrayType(rank));
+ }
+
+ [Fact]
+ public void MakeByRefType()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
+ Type arrayType = enumBuilder.MakeByRefType();
+ Assert.Equal(typeof(Array), arrayType.GetTypeInfo().BaseType);
+ Assert.Equal("TestEnum&", arrayType.Name);
+ }
+
+ [Fact]
+ public void MakePointerType()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
+ Type arrayType = enumBuilder.MakePointerType();
+ Assert.Equal(typeof(Array), arrayType.GetTypeInfo().BaseType);
+ Assert.Equal("TestEnum*", arrayType.Name);
+ }
+
+ [Fact]
+ public void SetCustomAttribute_ConstructorInfo_ByteArray()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ enumBuilder.CreateTypeInfo().AsType();
+
+ ConstructorInfo attributeConstructor = typeof(BoolAttribute).GetConstructor(new Type[] { typeof(bool) });
+ enumBuilder.SetCustomAttribute(attributeConstructor, new byte[] { 01, 00, 01 });
+
+ Attribute[] objVals = enumBuilder.GetCustomAttributes(true).ToArray();
+ Assert.Equal(1, objVals.Length);
+ Assert.Equal(new BoolAttribute(true), objVals[0]);
+ }
+
+
+ [Fact]
+ public void SetCustomAttribute_CustomAttributeBuilder()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ enumBuilder.CreateTypeInfo().AsType();
+
+ ConstructorInfo attributeConstructor = typeof(BoolAttribute).GetConstructor(new Type[] { typeof(bool) });
+ CustomAttributeBuilder myCustomAttribute = new CustomAttributeBuilder(attributeConstructor, new object[] { true });
+ enumBuilder.SetCustomAttribute(myCustomAttribute);
+
+ object[] objVals = enumBuilder.GetCustomAttributes(true).ToArray();
+ Assert.Equal(1, objVals.Length);
+ Assert.True(objVals[0].Equals(new BoolAttribute(true)));
+ }
+
+ public class BoolAttribute : Attribute
+ {
+ private bool _b;
+ public BoolAttribute(bool myBool) { _b = myBool; }
+ }
+ }
+}
--- /dev/null
+// 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 EnumBuilderPropertyTests
+ {
+ [Fact]
+ public void Assembly()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ ModuleBuilder module = assembly.DefineDynamicModule("TestModule");
+ EnumBuilder enumBuilder = module.DefineEnum("TestEnum", TypeAttributes.Public, typeof(int));
+ Assert.Equal(assembly, enumBuilder.Assembly);
+ }
+
+ [Fact]
+ public void AssemblyQualifiedName()
+ {
+ AssemblyBuilder assembly = Helpers.DynamicAssembly();
+ ModuleBuilder module = assembly.DefineDynamicModule("TestModule");
+ EnumBuilder enumBuilder = module.DefineEnum("TestEnum", TypeAttributes.Public, typeof(int));
+ Assert.Equal("TestEnum, " + assembly.FullName, enumBuilder.AssemblyQualifiedName);
+ }
+
+ [Fact]
+ public void BaseType()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ Assert.Equal(typeof(Enum), enumBuilder.BaseType);
+ }
+
+ [Fact]
+ public void DeclaringType()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ Assert.Null(enumBuilder.DeclaringType);
+ }
+
+ [Fact]
+ public void FullName()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
+ enumBuilder.AsType();
+ Assert.Equal("TestEnum", enumBuilder.FullName);
+ }
+
+ [Fact]
+ public void Guid_TypeCreated()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ enumBuilder.CreateTypeInfo().AsType();
+ Assert.NotEqual(Guid.Empty, enumBuilder.GUID);
+ }
+
+ [Fact]
+ public void Guid_TypeNotCreated_ThrowsNotSupportedException()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ Assert.Throws<NotSupportedException>(() => enumBuilder.GUID);
+ }
+
+ [Fact]
+ public void Module()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ EnumBuilder enumBuilder = module.DefineEnum("TestEnum", TypeAttributes.Public, typeof(int));
+ Assert.Equal(module, enumBuilder.Module);
+ }
+
+ [Fact]
+ public void Name()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int), enumName: "TestEnum");
+ enumBuilder.AsType();
+ Assert.Equal("TestEnum", enumBuilder.Name);
+ }
+
+ [Fact]
+ public void Namespace()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ enumBuilder.AsType();
+ Assert.Empty(enumBuilder.Namespace);
+ }
+
+ [Fact]
+ public void UnderlyingField_TypeCreated()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ enumBuilder.AsType();
+ Assert.Equal(typeof(int), enumBuilder.UnderlyingField.FieldType);
+ }
+
+ [Fact]
+ public void UnderlyingField_TypeNotCreated()
+ {
+ EnumBuilder enumBuilder = Helpers.DynamicEnum(TypeAttributes.Public, typeof(int));
+ Assert.Equal(typeof(int), enumBuilder.UnderlyingField.FieldType);
+ }
+ }
+}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderAssembly
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestAssemblyProperty()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.AsType();
- Assembly myAssem = _myEnumBuilder.Assembly;
- Assert.Equal(_myAssemblyBuilder.FullName, myAssem.FullName);
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderAssemblyQualifiedName
- {
- private AssemblyBuilder _myAssemblyBuilder;
-
- private ModuleBuilder CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- return TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestAssemblyQualifiedNameProperty()
- {
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- string myAssemQualified = myEnumBuilder.AssemblyQualifiedName;
- Assert.Equal("myEnum, " + _myAssemblyBuilder.FullName, myAssemQualified);
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderBaseType
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestBaseTypeProperty()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.AsType();
- Type myBaseType = _myEnumBuilder.BaseType;
- Assert.Equal(typeof(Enum), myBaseType);
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderDeclaringType
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestDeclaringTypeProperty()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.AsType();
- Type myDeclaringType = _myEnumBuilder.DeclaringType;
- Assert.Null(myDeclaringType);
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderDefineLiteral
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
-
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestDefineLiteral()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- FieldBuilder myField = _myEnumBuilder.DefineLiteral("FieldOne", 1);
- _myEnumBuilder.AsType();
- Assert.True(myField.IsLiteral);
- Assert.True(myField.IsPublic);
- Assert.True(myField.IsStatic);
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderFullName
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestFullNameProperty()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.AsType();
- string myFullName = _myEnumBuilder.FullName;
- Assert.Equal(_myEnumBuilder.FullName, myFullName);
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderGUID
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- return TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestGuidProperty()
- {
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- myEnumBuilder.CreateTypeInfo().AsType();
- Guid myGUID = myEnumBuilder.GUID;
- Assert.NotEqual(myGUID, Guid.Empty);
- }
-
- [Fact]
- public void TestThrowsExceptionForNotCreatedTypes()
- {
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- Assert.Throws<NotSupportedException>(() => { Guid myGUID = myEnumBuilder.GUID; });
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderGetElementType
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
-
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestThrowsExceptionForNotSupportedMethod()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- Assert.Throws<NotSupportedException>(() => { Type typeVal = _myEnumBuilder.GetElementType(); });
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderMakeArrayType1
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
-
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestMakeArrayType()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- Type myType = _myEnumBuilder.MakeArrayType();
- Assert.True(myType.GetTypeInfo().BaseType.Equals(typeof(Array)));
- Assert.Equal(myType.Name, "myEnum[]");
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using System.Globalization;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderMakeArrayType2
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private readonly RandomDataGenerator _generator = new RandomDataGenerator();
-
- private ModuleBuilder CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- return TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestMakeArrayTypeWithRank1()
- {
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- int rank = 1;
- Type myType = myEnumBuilder.MakeArrayType(rank);
- Assert.True(myType.GetTypeInfo().BaseType.Equals(typeof(Array)));
- Assert.Equal(myType.Name, "myEnum[*]");
- }
-
- [Fact]
- public void TestMakeArrayTypeWithRank2()
- {
- string szranks = null;
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- int rank = GetInt32(2, 256);
- for (int i = 1; i < rank; i++)
- szranks += ",";
- string s = string.Format("[{0}]", szranks);
- Type myType = myEnumBuilder.MakeArrayType(rank);
- Assert.True(myType.GetTypeInfo().BaseType.Equals(typeof(Array)));
- Assert.Equal(myType.Name, "myEnum" + s);
- }
-
- [Fact]
- public void TestThrowsExceptionOnInvalidRank()
- {
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- int rank = GetInt32(0, int.MaxValue) * (-1);
- Assert.Throws<IndexOutOfRangeException>(() => { Type myType = myEnumBuilder.MakeArrayType(rank); });
- }
-
- private int GetInt32(int minValue, int maxValue)
- {
- try
- {
- if (minValue == maxValue)
- {
- return minValue;
- }
- if (minValue < maxValue)
- {
- return minValue + _generator.GetInt32() % (maxValue - minValue);
- }
- }
- catch
- {
- throw;
- }
-
- return minValue;
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderMakeByRefType
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
-
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestMakeByRefType()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- Type myType = _myEnumBuilder.MakeByRefType();
- Assert.True(myType.GetTypeInfo().BaseType.Equals(typeof(Array)));
- Assert.Equal(myType.Name, "myEnum&");
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderMakePointerType
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
-
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestMakePointerType()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- Type myType = _myEnumBuilder.MakePointerType();
- Assert.True(myType.GetTypeInfo().BaseType.Equals(typeof(Array)));
- Assert.Equal(myType.Name, "myEnum*");
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderModule
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestModuleProperty()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.AsType();
- Module myModule = _myEnumBuilder.Module;
- Assert.True(myModule.Equals(_myModuleBuilder));
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderName
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestModuleName()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.AsType();
- string myEnumName = _myEnumBuilder.Name;
- Assert.Equal("myEnum", myEnumName);
- }
- }
-}
-
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderNamespace
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestNamespaceProperty()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.AsType();
- string myEnumNamespace = _myEnumBuilder.Namespace;
- Assert.Equal("", myEnumNamespace);
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using System.Linq;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderSetCustomAttribute1
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private ConstructorInfo _myInfo;
-
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- _myInfo = typeof(EBMyAttribute).GetConstructor(new Type[] { typeof(bool) });
- }
-
- [Fact]
- public void TestSetCustomAttribute()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.CreateTypeInfo().AsType();
- _myEnumBuilder.SetCustomAttribute(_myInfo, new byte[] { 01, 00, 01 });
- object[] objVals = _myEnumBuilder.GetCustomAttributes(true).Select(a => (object)a).ToArray().Select(a => (object)a).ToArray();
- Assert.Equal(1, objVals.Length);
- Assert.True(objVals[0].Equals(new EBMyAttribute(true)));
- }
-
- public class EBMyAttribute : Attribute
- {
- private bool _myBoolValue;
-
- public EBMyAttribute(bool myBool)
- {
- _myBoolValue = myBool;
- }
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using System.Linq;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderSetCustomAttribute2
- {
- private AssemblyBuilder _myAssemblyBuilder;
- private ModuleBuilder _myModuleBuilder;
- private EnumBuilder _myEnumBuilder;
- private ConstructorInfo _myInfo;
-
- private void CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- _myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- _myInfo = typeof(EBMyAttribute2).GetConstructor(new Type[] { typeof(bool) });
- }
-
- [Fact]
- public void TestSetCustomAttribute()
- {
- CreateCallee();
- _myEnumBuilder = _myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- _myEnumBuilder.CreateTypeInfo().AsType();
- CustomAttributeBuilder myCustomAttribute = new CustomAttributeBuilder(_myInfo, new object[] { true });
- _myEnumBuilder.SetCustomAttribute(myCustomAttribute);
- object[] objVals = _myEnumBuilder.GetCustomAttributes(true).Select(a => (object)a).ToArray();
- Assert.Equal(1, objVals.Length);
- Assert.True(objVals[0].Equals(new EBMyAttribute2(true)));
- }
-
- public class EBMyAttribute2 : Attribute
- {
- private bool _myBoolValue;
-
- public EBMyAttribute2(bool myBool)
- {
- _myBoolValue = myBool;
- }
- }
- }
-}
+++ /dev/null
-// 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 System;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.Threading;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class EnumBuilderUnderlyingField
- {
- private AssemblyBuilder _myAssemblyBuilder;
-
- private ModuleBuilder CreateCallee()
- {
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "EnumAssembly";
- _myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
- return TestLibrary.Utilities.GetModuleBuilder(_myAssemblyBuilder, "EnumModule.mod");
- }
-
- [Fact]
- public void TestUnderlyingFieldProperty()
- {
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- FieldBuilder fieldBuilder1 = myEnumBuilder.DefineLiteral("field1", 1);
- myEnumBuilder.AsType();
- FieldBuilder myUnderlyingField = myEnumBuilder.UnderlyingField;
- Assert.NotNull(myUnderlyingField);
- }
-
- [Fact]
- public void TestUnderlyingFieldWithNoLiteralDefined()
- {
- var myModuleBuilder = CreateCallee();
- var myEnumBuilder = myModuleBuilder.DefineEnum("myEnum", TypeAttributes.Public, typeof(int));
- FieldBuilder myUnderlyingField = myEnumBuilder.UnderlyingField;
- Assert.NotNull(myUnderlyingField);
- }
- }
-}
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
- <Compile Include="AssemblyBuilder\AssemblyBuilderDefineDynamicModule1.cs" />
- <Compile Include="AssemblyBuilder\AssemblyBuilderGetManifestResourceNames.cs" />
- <Compile Include="AssemblyBuilder\AssemblyBuilderGetManifestResourceStream1.cs" />
- <Compile Include="AssemblyBuilder\AssemblyBuilderSetCustomAttribute.cs" />
- <Compile Include="AssemblyBuilder\AssemblyBuilderSetCustomAttribute2.cs" />
+ <Compile Include="AssemblyBuilderTests.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderAttributes.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderCallingConvention.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderDeclaringType.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderDefineParameter.cs" />
- <Compile Include="ConstructorBuilder\ConstructorBuilderGetILGenerator1.cs" />
- <Compile Include="ConstructorBuilder\ConstructorBuilderGetILGenerator2.cs" />
+ <Compile Include="ConstructorBuilder\ConstructorBuilderGetILGenerator.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderInitLocals.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderName.cs" />
- <Compile Include="ConstructorBuilder\ConstructorBuilderSetCustomAttribute1.cs" />
- <Compile Include="ConstructorBuilder\ConstructorBuilderSetCustomAttribute2.cs" />
+ <Compile Include="ConstructorBuilder\ConstructorBuilderSetCustomAttribute.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderSetImplementationFlags.cs" />
<Compile Include="ConstructorBuilder\ConstructorBuilderToString.cs" />
- <Compile Include="EnumBuilder\EnumBuilderAssembly.cs" />
- <Compile Include="EnumBuilder\EnumBuilderAssemblyQualifiedName.cs" />
- <Compile Include="EnumBuilder\EnumBuilderBaseType.cs" />
- <Compile Include="EnumBuilder\EnumBuilderDeclaringType.cs" />
- <Compile Include="EnumBuilder\EnumBuilderDefineLiteral.cs" />
- <Compile Include="EnumBuilder\EnumBuilderFullName.cs" />
- <Compile Include="EnumBuilder\EnumBuilderGetElementType.cs" />
- <Compile Include="EnumBuilder\EnumBuilderGUID.cs" />
- <Compile Include="EnumBuilder\EnumBuilderMakeArrayType1.cs" />
- <Compile Include="EnumBuilder\EnumBuilderMakeArrayType2.cs" />
- <Compile Include="EnumBuilder\EnumBuilderMakeByRefType.cs" />
- <Compile Include="EnumBuilder\EnumBuilderMakePointerType.cs" />
- <Compile Include="EnumBuilder\EnumBuilderModule.cs" />
- <Compile Include="EnumBuilder\EnumBuilderName.cs" />
- <Compile Include="EnumBuilder\EnumBuilderNamespace.cs" />
- <Compile Include="EnumBuilder\EnumBuilderSetCustomAttribute1.cs" />
- <Compile Include="EnumBuilder\EnumBuilderSetCustomAttribute2.cs" />
- <Compile Include="EnumBuilder\EnumBuilderUnderlyingField.cs" />
+ <Compile Include="EnumBuilder\EnumBuilder.Methods.Tests.cs" />
+ <Compile Include="EnumBuilder\EnumBuilder.Properties.Tests.cs" />
<Compile Include="EventBuilder\EventBuilderAddOtherMethod.cs" />
<Compile Include="EventBuilder\EventBuilderSetAddOnMethod.cs" />
<Compile Include="EventBuilder\EventBuilderSetCustomAttribute1.cs" />
using System.Reflection.Emit;
+namespace System.Reflection.Emit.Tests
+{
+ public static class Helpers
+ {
+ public static AssemblyBuilder DynamicAssembly(string name = "TestAssembly")
+ {
+ AssemblyName assemblyName = new AssemblyName(name);
+ return AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
+ }
+
+ public static ModuleBuilder DynamicModule(string assemblyName = "TestAssembly", string moduleName = "TestModule")
+ {
+ return DynamicAssembly(assemblyName).DefineDynamicModule(moduleName);
+ }
+
+ public static TypeBuilder DynamicType(TypeAttributes attributes, string assemblyName = "TestAssembly", string moduleName = "TestModule", string typeName = "TestType")
+ {
+ return DynamicModule(assemblyName, moduleName).DefineType(typeName, attributes);
+ }
+
+ public static EnumBuilder DynamicEnum(TypeAttributes visibility, Type underlyingType, string enumName = "TestEnum", string assemblyName = "TestAssembly", string moduleName = "TestModule")
+ {
+ return DynamicModule(assemblyName, moduleName).DefineEnum(enumName, visibility, underlyingType);
+ }
+ }
+}
+
namespace TestLibrary
{
public static class Utilities