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]
// 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 Xunit;
namespace System.Reflection.Emit.Tests
// 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 ModuleBuilderCreateGlobalFunctions
{
[Fact]
- public void TestWithSingleGlobalMethod()
+ public void CreateGlobalFunctions_SingleGlobalMethod()
{
- ModuleBuilder m1 = CreateModule("Foo", "Bar");
- MethodBuilder mb = m1.DefineGlobalMethod("MyMethod1", MethodAttributes.Static | MethodAttributes.Public,
- null, null);
- ILGenerator ilg = mb.GetILGenerator();
- ilg.EmitWriteLine("Hello World from global method.");
- ilg.Emit(OpCodes.Ret);
- m1.CreateGlobalFunctions();
+ ModuleBuilder module = Helpers.DynamicModule();
+ MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null);
+ ILGenerator ilGenerator = method.GetILGenerator();
+ ilGenerator.EmitWriteLine("Hello World from global method.");
+ ilGenerator.Emit(OpCodes.Ret);
+
+ module.CreateGlobalFunctions();
}
[Fact]
- public void TestWithMultipleGlobalMethods()
+ public void CreateGlobalFunctions_MultipleGlobalMethods()
{
- ModuleBuilder m1 = CreateModule("Baz", "Quux");
- MethodBuilder mb = m1.DefineGlobalMethod("MyMethod1", MethodAttributes.Static | MethodAttributes.Public,
- null, null);
- ILGenerator ilg = mb.GetILGenerator();
- ilg.EmitWriteLine("Hello World from global method.");
- ilg.Emit(OpCodes.Ret);
- mb = m1.DefineGlobalMethod("MyMethod2", MethodAttributes.Static | MethodAttributes.Public,
+ ModuleBuilder module = Helpers.DynamicModule();
+ MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null);
+ ILGenerator ilGenerator = method.GetILGenerator();
+ ilGenerator.EmitWriteLine("Hello World from global method.");
+ ilGenerator.Emit(OpCodes.Ret);
+
+ method = module.DefineGlobalMethod("MyMethod2", MethodAttributes.Static | MethodAttributes.Public,
null, null);
- ilg = mb.GetILGenerator();
- ilg.EmitWriteLine("Hello World from global method again!");
- m1.CreateGlobalFunctions();
- }
+ ilGenerator = method.GetILGenerator();
+ ilGenerator.EmitWriteLine("Hello World from global method again!");
- [Fact]
- public void TestThrowsExceptionOnMultipleCallsToCreateGlobalFunctions()
- {
- ModuleBuilder m1 = CreateModule("Grip", "Fang");
- MethodBuilder mb = m1.DefineGlobalMethod("MyMethod1", MethodAttributes.Static | MethodAttributes.Public,
- null, null);
- ILGenerator ilg = mb.GetILGenerator();
- ilg.EmitWriteLine("Hello World from global method.");
- ilg.Emit(OpCodes.Ret);
- m1.CreateGlobalFunctions();
- Assert.Throws<InvalidOperationException>(() => { m1.CreateGlobalFunctions(); });
+ module.CreateGlobalFunctions();
}
- public ModuleBuilder CreateModule(string assemblyName, string modName)
+ [Fact]
+ public void CreateGlobalFunctions_CalledMultipleTimes_ThrowsInvalidOperationException()
{
- AssemblyName asmName;
- AssemblyBuilder asmBuilder;
- ModuleBuilder modBuilder;
-
- // create the dynamic module
- asmName = new AssemblyName(assemblyName);
- asmBuilder = AssemblyBuilder.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);
- modBuilder = TestLibrary.Utilities.GetModuleBuilder(asmBuilder, "Module1");
-
- return modBuilder;
+ ModuleBuilder module = Helpers.DynamicModule();
+ MethodBuilder method = module.DefineGlobalMethod("TestMethod", MethodAttributes.Static | MethodAttributes.Public, null, null);
+ ILGenerator ilGenerator = method.GetILGenerator();
+ ilGenerator.EmitWriteLine("Hello World from global method.");
+ ilGenerator.Emit(OpCodes.Ret);
+
+ module.CreateGlobalFunctions();
+ Assert.Throws<InvalidOperationException>(() => module.CreateGlobalFunctions());
}
}
}
// 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;
using System.Collections.Generic;
using Xunit;
{
public class ModuleBuilderDefineEnum
{
- private static Type[] s_builtInIntegerTypes = new Type[] { typeof(byte), typeof(SByte), typeof(Int16), typeof(ushort),
+ private static Type[] s_builtInIntegerTypes = new Type[] { typeof(byte), typeof(sbyte), typeof(short), typeof(ushort),
typeof(int), typeof(uint), typeof(long), typeof(ulong) };
- [Fact]
- public void TestWithValueType()
+ [Theory]
+ [MemberData(nameof(VisibilityAttributes), true)]
+ public void DefineEnum_ValueType(TypeAttributes visibility)
{
- List<object> myArray = new List<object>();
- myArray = GetVisibilityAttr(true);
- foreach (TypeAttributes current in myArray)
+ foreach (Type integerType in s_builtInIntegerTypes)
{
- foreach (Type integerType in s_builtInIntegerTypes)
- {
- VerificationHelper(current, integerType);
- }
- }
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ EnumBuilder enumBuilder = module.DefineEnum("MyEnum", visibility, integerType);
+ Assert.True(enumBuilder.IsEnum);
+ Assert.Equal("MyEnum", enumBuilder.FullName);
- [Fact]
- public void TestForNonVisibilityAttributes()
- {
- List<object> myArray = new List<object>();
- myArray = GetVisibilityAttr(false);
- foreach (TypeAttributes current in myArray)
- {
- string name = "MyEnum";
- VerificationHelperNegative(name, current, typeof(int), true);
+ enumBuilder.CreateTypeInfo().AsType();
}
}
- [Fact]
- public void TestForAlreadyExistingEnumWithSameName()
+ [Theory]
+ [MemberData(nameof(VisibilityAttributes), false)]
+ public void DefineEnum_NonVisibilityAttributes_ThrowsArgumentException(TypeAttributes visibility)
{
- List<object> myArray = new List<object>();
-
- myArray = GetVisibilityAttr(true);
-
- foreach (TypeAttributes current in myArray)
- {
- string name = "MyEnum";
- VerificationHelperNegative(name, current, typeof(object), false);
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentException>("name", () => module.DefineEnum("MyEnum", visibility, typeof(int)));
}
- [Fact]
- public void TestWithNullName()
+ [Theory]
+ [MemberData(nameof(VisibilityAttributes), true)]
+ public void DefineEnum_EnumWithSameNameExists_ThrowsArgumentException(TypeAttributes visibility)
{
- List<object> myArray = new List<object>();
-
- myArray = GetVisibilityAttr(true);
-
- foreach (TypeAttributes current in myArray)
- {
- string name = null;
- VerificationHelperNegative(name, current, typeof(object), typeof(ArgumentNullException));
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ module.DefineEnum("MyEnum", visibility, typeof(int));
+ Assert.Throws<ArgumentException>(null, () => module.DefineEnum("MyEnum", visibility, typeof(int)));
}
- [Fact]
- public void TestWithEmptyName()
+ [Theory]
+ [MemberData(nameof(VisibilityAttributes), true)]
+ public void DefineEnum_NullName_ThrowsArgumentNullException(TypeAttributes visibility)
{
- List<object> myArray = new List<object>();
- myArray = GetVisibilityAttr(true);
-
- foreach (TypeAttributes current in myArray)
- {
- string name = string.Empty;
- VerificationHelperNegative(name, current, typeof(object), typeof(ArgumentException));
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentNullException>("fullname", () => module.DefineEnum(null, visibility, typeof(object)));
}
- [Fact]
- public void TestWithIncorrectVisibilityAttributes()
+ [Theory]
+ [MemberData(nameof(VisibilityAttributes), true)]
+ public void DefineEnum_EmptyName_ThrowsArgumentNullException(TypeAttributes visibility)
{
- List<object> myArray = new List<object>();
- myArray = GetNestVisibilityAttr(true);
- foreach (TypeAttributes current in myArray)
- {
- string name = "MyEnum";
- VerificationHelperNegative(name, current, typeof(object), true);
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentException>("fullname", () => module.DefineEnum("", visibility, typeof(object)));
}
- [Fact]
- public void TestWithReferenceType()
+ [Theory]
+ [MemberData(nameof(NestedVisibilityAttributes), true)]
+ public void DefineEnum_IncorrectVisibilityAttributes_ThrowsArgumentException(TypeAttributes visibility)
{
- List<object> myArray = new List<object>();
- myArray = GetVisibilityAttr(true);
- foreach (TypeAttributes current in myArray)
- {
- VerificationHelperNegative("MyEnum", current, typeof(string), typeof(TypeLoadException));
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentException>(null, () => module.DefineEnum("MyEnum", visibility, typeof(object)));
}
- private ModuleBuilder GetModuleBuilder()
+ [Theory]
+ [MemberData(nameof(VisibilityAttributes), true)]
+ public void DefineEnum_ReferecnceType_ThrowsTypeLoadException(TypeAttributes visibility)
{
- ModuleBuilder myModuleBuilder;
- AssemblyBuilder myAssemblyBuilder;
- // Get the current application domain for the current thread.
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "TempAssembly";
-
- // Define a dynamic assembly in the current domain.
- myAssemblyBuilder =
- AssemblyBuilder.DefineDynamicAssembly
- (myAssemblyName, AssemblyBuilderAccess.Run);
- // Define a dynamic module in "TempAssembly" assembly.
- myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
-
- return myModuleBuilder;
+ ModuleBuilder module = Helpers.DynamicModule();
+ EnumBuilder enumBuilder = module.DefineEnum("MyEnum", visibility, typeof(string));
+ Assert.Throws<TypeLoadException>(() => enumBuilder.CreateTypeInfo().AsType());
}
- private List<object> GetNestVisibilityAttr(bool flag)
+ public static IEnumerable<object[]> NestedVisibilityAttributes(bool flag)
{
- List<object> myArray = new List<object>();
if (JudgeVisibilityMaskAttributes(TypeAttributes.NestedAssembly, flag))
- myArray.Add(TypeAttributes.NestedAssembly);
+ yield return new object[] { TypeAttributes.NestedAssembly };
if (JudgeVisibilityMaskAttributes(TypeAttributes.NestedFamANDAssem, flag))
- myArray.Add(TypeAttributes.NestedFamANDAssem);
+ yield return new object[] { TypeAttributes.NestedFamANDAssem };
if (JudgeVisibilityMaskAttributes(TypeAttributes.NestedFamily, flag))
- myArray.Add(TypeAttributes.NestedFamily);
+ yield return new object[] { TypeAttributes.NestedFamily };
if (JudgeVisibilityMaskAttributes(TypeAttributes.NestedFamANDAssem, flag))
- myArray.Add(TypeAttributes.NestedFamANDAssem);
+ yield return new object[] { TypeAttributes.NestedFamANDAssem };
if (JudgeVisibilityMaskAttributes(TypeAttributes.NestedFamORAssem, flag))
- myArray.Add(TypeAttributes.NestedFamORAssem);
+ yield return new object[] { TypeAttributes.NestedFamORAssem };
if (JudgeVisibilityMaskAttributes(TypeAttributes.NestedPrivate, flag))
- myArray.Add(TypeAttributes.NestedPrivate);
+ yield return new object[] { TypeAttributes.NestedPrivate };
if (JudgeVisibilityMaskAttributes(TypeAttributes.NestedPublic, flag))
- myArray.Add(TypeAttributes.NestedPublic);
- return myArray;
+ yield return new object[] { TypeAttributes.NestedPublic };
}
- private List<object> GetVisibilityAttr(bool flag)
+ public static IEnumerable<object[]> VisibilityAttributes(bool flag)
{
- List<object> myArray = new List<object>();
if (JudgeVisibilityMaskAttributes(TypeAttributes.Abstract, flag))
- myArray.Add(TypeAttributes.Abstract);
+ yield return new object[] { TypeAttributes.Abstract };
if (JudgeVisibilityMaskAttributes(TypeAttributes.AnsiClass, flag))
- myArray.Add(TypeAttributes.AnsiClass);
+ yield return new object[] { TypeAttributes.AnsiClass };
if (JudgeVisibilityMaskAttributes(TypeAttributes.AutoClass, flag))
- myArray.Add(TypeAttributes.AutoClass);
+ yield return new object[] { TypeAttributes.AutoClass };
if (JudgeVisibilityMaskAttributes(TypeAttributes.AutoLayout, flag))
- myArray.Add(TypeAttributes.AutoLayout);
+ yield return new object[] { TypeAttributes.AutoLayout };
if (JudgeVisibilityMaskAttributes(TypeAttributes.BeforeFieldInit, flag))
- myArray.Add(TypeAttributes.BeforeFieldInit);
+ yield return new object[] { TypeAttributes.BeforeFieldInit };
if (JudgeVisibilityMaskAttributes(TypeAttributes.Class, flag))
- myArray.Add(TypeAttributes.Class);
+ yield return new object[] { TypeAttributes.Class };
if (JudgeVisibilityMaskAttributes(TypeAttributes.ClassSemanticsMask, flag))
- myArray.Add(TypeAttributes.ClassSemanticsMask);
+ yield return new object[] { TypeAttributes.ClassSemanticsMask };
if (JudgeVisibilityMaskAttributes(TypeAttributes.CustomFormatClass, flag))
- myArray.Add(TypeAttributes.CustomFormatClass);
+ yield return new object[] { TypeAttributes.CustomFormatClass };
if (JudgeVisibilityMaskAttributes(TypeAttributes.CustomFormatMask, flag))
- myArray.Add(TypeAttributes.CustomFormatMask);
+ yield return new object[] { TypeAttributes.CustomFormatMask };
if (JudgeVisibilityMaskAttributes(TypeAttributes.ExplicitLayout, flag))
- myArray.Add(TypeAttributes.ExplicitLayout);
+ yield return new object[] { TypeAttributes.ExplicitLayout };
if (JudgeVisibilityMaskAttributes(TypeAttributes.HasSecurity, flag))
- myArray.Add(TypeAttributes.HasSecurity);
+ yield return new object[] { TypeAttributes.HasSecurity };
if (JudgeVisibilityMaskAttributes(TypeAttributes.Import, flag))
- myArray.Add(TypeAttributes.Import);
+ yield return new object[] { TypeAttributes.Import };
if (JudgeVisibilityMaskAttributes(TypeAttributes.Interface, flag))
- myArray.Add(TypeAttributes.Interface);
+ yield return new object[] { TypeAttributes.Interface };
if (JudgeVisibilityMaskAttributes(TypeAttributes.LayoutMask, flag))
- myArray.Add(TypeAttributes.LayoutMask);
-
+ yield return new object[] { TypeAttributes.LayoutMask };
if (JudgeVisibilityMaskAttributes(TypeAttributes.NotPublic, flag))
- myArray.Add(TypeAttributes.NotPublic);
+ yield return new object[] { TypeAttributes.NotPublic };
if (JudgeVisibilityMaskAttributes(TypeAttributes.Public, flag))
- myArray.Add(TypeAttributes.Public);
+ yield return new object[] { TypeAttributes.Public };
if (JudgeVisibilityMaskAttributes(TypeAttributes.RTSpecialName, flag))
- myArray.Add(TypeAttributes.RTSpecialName);
+ yield return new object[] { TypeAttributes.RTSpecialName };
if (JudgeVisibilityMaskAttributes(TypeAttributes.Sealed, flag))
- myArray.Add(TypeAttributes.Sealed);
+ yield return new object[] { TypeAttributes.Sealed };
if (JudgeVisibilityMaskAttributes(TypeAttributes.SequentialLayout, flag))
- myArray.Add(TypeAttributes.SequentialLayout);
+ yield return new object[] { TypeAttributes.SequentialLayout };
if (JudgeVisibilityMaskAttributes(TypeAttributes.Serializable, flag))
- myArray.Add(TypeAttributes.Serializable);
+ yield return new object[] { TypeAttributes.Serializable };
if (JudgeVisibilityMaskAttributes(TypeAttributes.SpecialName, flag))
- myArray.Add(TypeAttributes.SpecialName);
+ yield return new object[] { TypeAttributes.SpecialName };
if (JudgeVisibilityMaskAttributes(TypeAttributes.StringFormatMask, flag))
- myArray.Add(TypeAttributes.StringFormatMask);
+ yield return new object[] { TypeAttributes.StringFormatMask };
if (JudgeVisibilityMaskAttributes(TypeAttributes.UnicodeClass, flag))
- myArray.Add(TypeAttributes.UnicodeClass);
-
-
- return myArray;
+ yield return new object[] { TypeAttributes.UnicodeClass };
}
- private bool JudgeVisibilityMaskAttributes(TypeAttributes visibility, bool flag)
+ private static bool JudgeVisibilityMaskAttributes(TypeAttributes visibility, bool flag)
{
if (flag)
{
- if ((visibility & ~TypeAttributes.VisibilityMask) == 0)
- return true;
- else
- return false;
+ return (visibility & ~TypeAttributes.VisibilityMask) == 0;
}
else
{
- if ((visibility & ~TypeAttributes.VisibilityMask) != 0)
- return true;
- else
- return false;
+ return (visibility & ~TypeAttributes.VisibilityMask) != 0;
}
}
- private void VerificationHelper(TypeAttributes myTypeAttribute, Type mytype)
- {
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define a enumeration type with name 'MyEnum' in the 'TempModule'.
- EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum("MyEnum",
- myTypeAttribute, mytype);
- Assert.True(myEnumBuilder.IsEnum);
- Assert.Equal(myEnumBuilder.FullName, "MyEnum");
-
- myEnumBuilder.CreateTypeInfo().AsType();
- }
-
private void VerificationHelperNegative(string name, TypeAttributes myTypeAttribute, Type mytype, bool flag)
{
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define a enumeration type with name 'MyEnum' in the 'TempModule'.
-
+ ModuleBuilder myModuleBuilder = Helpers.DynamicModule();
Assert.Throws<ArgumentException>(() =>
{
EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum(name, myTypeAttribute, mytype);
}
});
}
-
- private void VerificationHelperNegative(string name, TypeAttributes myTypeAttribute, Type mytype, Type expectedException)
- {
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define a enumeration type with name 'MyEnum' in the 'TempModule'.
- Action test = () =>
- {
- EnumBuilder myEnumBuilder = myModuleBuilder.DefineEnum(name, myTypeAttribute, mytype);
- myEnumBuilder.CreateTypeInfo().AsType();
- };
-
- Assert.Throws(expectedException, test);
- }
- }
-
- public class Container1
- {
- public class Nested
- {
- private Container1 _parent;
-
- public Nested()
- {
- }
- public Nested(Container1 parent)
- {
- _parent = parent;
- }
- }
}
}
// 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 ModuleBuilderDefineInitializedData
{
- [Fact]
- public void TestWithStaticAndPublic()
- {
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define the initialized data field in the .sdata section of the PE file.
- FieldBuilder myFieldBuilder =
- myModuleBuilder.DefineInitializedData("MyField", new byte[] { 01, 00, 01 },
- FieldAttributes.Static | FieldAttributes.Public);
- Assert.True(myFieldBuilder.IsStatic);
- Assert.True(myFieldBuilder.IsPublic);
- Assert.Equal(myFieldBuilder.Name, "MyField");
- }
-
- [Fact]
- public void TestWithStaticAndPrivate()
- {
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define the initialized data field in the .sdata section of the PE file.
- FieldBuilder myFieldBuilder =
- myModuleBuilder.DefineInitializedData("MyField", new byte[] { 01, 00, 01 },
- FieldAttributes.Static | FieldAttributes.Private);
- Assert.True(myFieldBuilder.IsStatic);
- Assert.True(myFieldBuilder.IsPrivate);
- Assert.Equal(myFieldBuilder.Name, "MyField");
- }
-
- [Fact]
- public void TestIncludeStaticWithDefault()
+ [Theory]
+ [InlineData(FieldAttributes.Static | FieldAttributes.Public)]
+ [InlineData(FieldAttributes.Static | FieldAttributes.Private)]
+ [InlineData( FieldAttributes.Private)]
+ public void TestWithStaticAndPublic(FieldAttributes attributes)
{
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define the initialized data field in the .sdata section of the PE file.
- FieldBuilder myFieldBuilder =
- myModuleBuilder.DefineInitializedData("MyField", new byte[] { 01, 00, 01 },
- FieldAttributes.Private);
- Assert.True(myFieldBuilder.IsStatic);
- Assert.True(myFieldBuilder.IsPrivate);
- Assert.Equal(myFieldBuilder.Name, "MyField");
+ ModuleBuilder module = Helpers.DynamicModule();
+ FieldBuilder field = module.DefineInitializedData("MyField", new byte[] { 01, 00, 01 }, attributes);
+ Assert.True(field.IsStatic);
+ Assert.Equal((attributes & FieldAttributes.Public) != 0 , field.IsPublic);
+ Assert.Equal((attributes & FieldAttributes.Private) != 0, field.IsPrivate);
+ Assert.Equal(field.Name, "MyField");
}
[Fact]
- public void TestThrowsExceptionOnEmptyName()
+ public void DefineInitializedData_EmptyName_ThrowsArgumentException()
{
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define the initialized data field in the .sdata section of the PE file.
- Assert.Throws<ArgumentException>(() =>
- {
- FieldBuilder myFieldBuilder = myModuleBuilder.DefineInitializedData("", new byte[] { 01, 00, 01 }, FieldAttributes.Private);
- });
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentException>("name", () => module.DefineInitializedData("", new byte[] { 1, 0, 1 }, FieldAttributes.Private));
}
- [Fact]
- public void TestThrowsExceptionWithSizeOfDataLessThanZero()
- {
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define the initialized data field in the .sdata section of the PE file.
- string fieldname = "myField";
- Assert.Throws<ArgumentException>(() =>
- {
- FieldBuilder myFieldBuilder = myModuleBuilder.DefineInitializedData(fieldname, new byte[] { }, FieldAttributes.Private);
- });
- }
-
- [Fact]
- public void TestThrowsExceptionWithSizeOfDateGreaterThan0x3f0000()
+ [Theory]
+ [InlineData(0)]
+ [InlineData(0x3f0000)]
+ public void DefineInitializedData_InvalidDataLength_ThrowsArgumentException(int length)
{
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
- // Define the initialized data field in the .sdata section of the PE file.
- string fieldname = "myField";
- byte[] myByte = new byte[0x3f0000];
- Assert.Throws<ArgumentException>(() =>
- {
- FieldBuilder myFieldBuilder = myModuleBuilder.DefineInitializedData(fieldname, myByte, FieldAttributes.Public);
- });
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentException>(null, () => module.DefineInitializedData("MyField", new byte[length], FieldAttributes.Public));
}
[Fact]
- public void TestThrowsExceptionWithNullName()
+ public void DefineInitializedData_NullName_ThrowsArgumentNullException()
{
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
-
- string fieldname = null;
- byte[] myByte = new byte[] { 01, 00, 01 };
- Assert.Throws<ArgumentNullException>(() =>
- {
- FieldBuilder myFieldBuilder = myModuleBuilder.DefineInitializedData(fieldname, myByte, FieldAttributes.Public);
- });
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentNullException>("name", () => module.DefineInitializedData(null, new byte[] { 1, 0, 1 }, FieldAttributes.Public));
}
[Fact]
- public void TestThrowsExceptionWithNullData()
+ public void DefineInitializedData_NullData_ThrowsArgumentNullException()
{
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
-
- string fieldname = "MyField";
- byte[] myByte = null;
- Assert.Throws<ArgumentNullException>(() =>
- {
- FieldBuilder myFieldBuilder = myModuleBuilder.DefineInitializedData(fieldname, myByte, FieldAttributes.Public);
- });
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentNullException>("data", () => module.DefineInitializedData("MyField", null, FieldAttributes.Public));
}
[Fact]
- public void TestThrowsExceptionWhenCreateGlobalFunctionsPreviouslyCalled()
- {
- ModuleBuilder myModuleBuilder = GetModuleBuilder();
-
- string fieldname = "MyField";
- byte[] myByte = new byte[] { 01, 00, 01 };
- FieldBuilder myFieldBuilder =
- myModuleBuilder.DefineInitializedData(fieldname, myByte, FieldAttributes.Public);
- myModuleBuilder.CreateGlobalFunctions();
- Assert.Throws<InvalidOperationException>(() =>
- {
- myFieldBuilder = myModuleBuilder.DefineInitializedData(fieldname, myByte, FieldAttributes.Public);
- });
- }
-
- private ModuleBuilder GetModuleBuilder()
- {
- ModuleBuilder myModuleBuilder;
- AssemblyBuilder myAssemblyBuilder;
- // Get the current application domain for the current thread.
- AssemblyName myAssemblyName = new AssemblyName();
- myAssemblyName.Name = "TempAssembly";
-
- // Define a dynamic assembly in the current domain.
- myAssemblyBuilder =
- AssemblyBuilder.DefineDynamicAssembly
- (myAssemblyName, AssemblyBuilderAccess.Run);
- // Define a dynamic module in "TempAssembly" assembly.
- myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
-
- return myModuleBuilder;
- }
- }
-
- public class Container2
- {
- public class Nested
+ public void DefineInitializedData_CreateGlobalFunctionsCalled_ThrowsInvalidOperationException()
{
- private Container2 _parent;
-
- public Nested()
- {
- }
- public Nested(Container2 parent)
- {
- _parent = parent;
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ FieldBuilder field = module.DefineInitializedData("MyField", new byte[] { 1, 0, 1 }, FieldAttributes.Public);
+ module.CreateGlobalFunctions();
+ Assert.Throws<InvalidOperationException>(() => module.DefineInitializedData("MyField2", new byte[] { 1, 0, 1 }, FieldAttributes.Public));
}
}
}
--- /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 ModuleBuilderDefineType
+ {
+ [Fact]
+ public void DefineType_String()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ TypeBuilder type = module.DefineType("TestType");
+ Type createdType = type.CreateTypeInfo().AsType();
+ Assert.Equal("TestType", createdType.Name);
+ }
+
+ [Theory]
+ [InlineData(TypeAttributes.NotPublic)]
+ [InlineData(TypeAttributes.Interface | TypeAttributes.Abstract)]
+ [InlineData(TypeAttributes.Class)]
+ public void DefineType_String_TypeAttributes(TypeAttributes attributes)
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ TypeBuilder type = module.DefineType("TestType", attributes);
+
+ Type createdType = type.CreateTypeInfo().AsType();
+ Assert.Equal("TestType", createdType.Name);
+ Assert.Equal(attributes, createdType.GetTypeInfo().Attributes);
+ }
+
+ [Theory]
+ [InlineData(TypeAttributes.NotPublic)]
+ [InlineData(TypeAttributes.Class)]
+ public void DefineType_String_TypeAttributes_Type(TypeAttributes attributes)
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ TypeBuilder type = module.DefineType("TestType", attributes, typeof(ModuleBuilderDefineType));
+
+ Type createdType = type.CreateTypeInfo().AsType();
+ Assert.Equal("TestType", createdType.Name);
+ Assert.Equal(attributes, createdType.GetTypeInfo().Attributes);
+ Assert.Equal(typeof(ModuleBuilderDefineType), createdType.GetTypeInfo().BaseType);
+ }
+
+ [Fact]
+ public void DefineType_String_TypeAttributes_Type_TypeCreatedInModule()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ TypeBuilder type1 = module.DefineType("TestType1");
+ Type parent = type1.CreateTypeInfo().AsType();
+
+ TypeBuilder type2 = module.DefineType("TestType2", TypeAttributes.NotPublic, parent);
+ Type createdType = type2.CreateTypeInfo().AsType();
+ Assert.Equal("TestType2", createdType.Name);
+ Assert.Equal(TypeAttributes.NotPublic, createdType.GetTypeInfo().Attributes);
+ Assert.Equal(parent, createdType.GetTypeInfo().BaseType);
+ }
+
+ [Fact]
+ public void DefineType_NullName_ThrowsArgumentNullException()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentNullException>("fullname", () => module.DefineType(null));
+ Assert.Throws<ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic));
+ Assert.Throws<ArgumentNullException>("fullname", () => module.DefineType(null, TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType)));
+ }
+
+ [Fact]
+ public void DefineType_TypeAlreadyExists_ThrowsArgumentException()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ module.DefineType("TestType");
+ Assert.Throws<ArgumentException>(null, () => module.DefineType("TestType"));
+ Assert.Throws<ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic));
+ Assert.Throws<ArgumentException>(null, () => module.DefineType("TestType", TypeAttributes.NotPublic, typeof(ModuleBuilderDefineType)));
+ }
+ }
+}
+++ /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 Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class ModuleBuilderDefineType1
- {
- [Fact]
- public void TestDefineType()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateTypeBuilder();
- testTypeBuilder = testModuleBuilder.DefineType(typeName);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Equal(typeName, tpA.Name);
- }
-
- [Fact]
- public void TestWithNullName()
- {
- string typeName = null;
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateTypeBuilder();
- Assert.Throws<ArgumentNullException>(() => { testTypeBuilder = testModuleBuilder.DefineType(typeName); });
- }
-
- [Fact]
- public void TestWithTypeWithSameNameAlreadyExists()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateTypeBuilder();
- testTypeBuilder = testModuleBuilder.DefineType(typeName);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Throws<ArgumentException>(() => { TypeBuilder testTypeBuilder2 = testModuleBuilder.DefineType(typeName); });
- }
-
- private ModuleBuilder CreateTypeBuilder()
- {
- AssemblyName assemName = new AssemblyName();
- assemName.Name = "testAssembly.exe";
- AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run);
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
-
- return 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.Collections.Generic;
-using System.Reflection;
-using System.Reflection.Emit;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class ModuleBuilderDefineType2
- {
- [Fact]
- public void TestWithNonPublicAttributeInModule()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- testTypeBuilder = testModuleBuilder.DefineType(typeName, TypeAttributes.NotPublic);
-
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Equal(typeName, tpA.Name);
- Assert.Equal(TypeAttributes.NotPublic, tpA.GetTypeInfo().Attributes);
- }
-
- [Fact]
- public void TestWithInterfaceAttributeInModule()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- TypeAttributes typeAttr = TypeAttributes.Interface;
- testTypeBuilder = testModuleBuilder.DefineType(typeName, typeAttr | TypeAttributes.Abstract);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Equal(typeName, tpA.Name);
- Assert.Equal((TypeAttributes.Interface | TypeAttributes.Abstract), tpA.GetTypeInfo().Attributes);
- }
-
- [Fact]
- public void TestWithClassAttributeInModule()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- testTypeBuilder = testModuleBuilder.DefineType(typeName, TypeAttributes.Class);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Equal(typeName, tpA.Name);
- Assert.Equal(TypeAttributes.Class, tpA.GetTypeInfo().Attributes);
- }
-
- [Fact]
- public void TestWithNullName()
- {
- string typeName = null;
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- Assert.Throws<ArgumentNullException>(() => { testTypeBuilder = testModuleBuilder.DefineType(typeName, TypeAttributes.NotPublic); });
- }
-
- [Fact]
- public void TestWithSameNameTypeAlreadyExists()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- testTypeBuilder = testModuleBuilder.DefineType(typeName);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Throws<ArgumentException>(() => { TypeBuilder testTypeBuilder2 = testModuleBuilder.DefineType(typeName, TypeAttributes.NotPublic); });
- }
-
- private ModuleBuilder CreateModuleBuilder()
- {
- AssemblyName assemName = new AssemblyName();
- assemName.Name = "testAssembly.dll";
- AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run);
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
- return 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.Collections.Generic;
-using System.Reflection;
-using System.Reflection.Emit;
-using System.IO;
-using Xunit;
-
-namespace System.Reflection.Emit.Tests
-{
- public class ModuleBuilderDefineType3
- {
- [Fact]
- public void TestTypeWithNonPublicAttributeAndBaseTypeClassInModule()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- Type parent = typeof(MBTestClass);
- testTypeBuilder = testModuleBuilder.DefineType(typeName, TypeAttributes.NotPublic, parent);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Equal(typeName, tpA.Name);
- Assert.Equal(TypeAttributes.NotPublic, tpA.GetTypeInfo().Attributes);
- Assert.Equal(parent, tpA.GetTypeInfo().BaseType);
- }
-
- [Fact]
- public void TestTypeWithClassAttributeAndBaseTypeClassInModule()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- Type parent = typeof(MBTestClass);
- testTypeBuilder = testModuleBuilder.DefineType(typeName, TypeAttributes.Class, parent);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Equal(typeName, tpA.Name);
- Assert.Equal(TypeAttributes.Class, tpA.GetTypeInfo().Attributes);
- Assert.Equal(parent, tpA.GetTypeInfo().BaseType);
- }
-
- [Fact]
- public void TestTypeWithNonPublicAttributeAndBaseTypeCreateTypeInModule()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- TypeBuilder myTypeBuilder = testModuleBuilder.DefineType("myType");
- Type parent = myTypeBuilder.CreateTypeInfo().AsType();
- testTypeBuilder = testModuleBuilder.DefineType(typeName, TypeAttributes.NotPublic, parent);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Equal(typeName, tpA.Name);
- Assert.Equal(TypeAttributes.NotPublic, tpA.GetTypeInfo().Attributes);
- Assert.Equal(parent, tpA.GetTypeInfo().BaseType);
- }
-
- [Fact]
- public void NegTest1()
- {
- string typeName = null;
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- Assert.Throws<ArgumentNullException>(() => { testTypeBuilder = testModuleBuilder.DefineType(typeName, TypeAttributes.NotPublic, typeof(MBTestClass)); });
- }
-
- [Fact]
- public void NegTest2()
- {
- string typeName = "testType";
- ModuleBuilder testModuleBuilder;
- TypeBuilder testTypeBuilder;
- testModuleBuilder = CreateModuleBuilder();
- testTypeBuilder = testModuleBuilder.DefineType(typeName);
- Type tpA = testTypeBuilder.CreateTypeInfo().AsType();
- Assert.Throws<ArgumentException>(() => { TypeBuilder testTypeBuilder2 = testModuleBuilder.DefineType(typeName, TypeAttributes.NotPublic, typeof(MBTestClass)); });
- }
-
- private ModuleBuilder CreateModuleBuilder()
- {
- AssemblyName assemName = new AssemblyName();
- assemName.Name = "testAssembly.dll";
- AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemName, AssemblyBuilderAccess.Run);
- ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, "Module1");
- return myModuleBuilder;
- }
- }
-
- public class MBTestClass { }
- public interface MBTestInterface { }
-}
// 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 ModuleBuilderDefineUninitializedData
{
- private const string DefaultAssemblyName = "ModuleBuilderDefineUninitializedData";
- private const AssemblyBuilderAccess DefaultAssemblyBuilderAccess = AssemblyBuilderAccess.Run;
- private const string DefaultModuleName = "DynamicModule";
- private const int MinStringLength = 8;
- private const int MaxStringLength = 256;
private const int ReservedMaskFieldAttribute = 0x9500; // This constant maps to FieldAttributes.ReservedMask that is not available in the contract.
- private readonly RandomDataGenerator _generator = new RandomDataGenerator();
-
- private ModuleBuilder GetModuleBuilder()
- {
- AssemblyName name = new AssemblyName(DefaultAssemblyName);
- AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(name, DefaultAssemblyBuilderAccess);
- return TestLibrary.Utilities.GetModuleBuilder(asmBuilder, DefaultModuleName);
- }
- [Fact]
- public void TestWithValidData()
+ public static IEnumerable<object[]> Attributes_TestData()
{
- ModuleBuilder builder = GetModuleBuilder();
- string fieldName = "PosTest1_";
- int size = _generator.GetByte();
- if (size == 0)
- size++;
-
- FieldAttributes[] attributes = new FieldAttributes[] {
- FieldAttributes.Assembly,
- FieldAttributes.FamANDAssem,
- FieldAttributes.Family,
- FieldAttributes.FamORAssem,
- FieldAttributes.FieldAccessMask,
- FieldAttributes.HasDefault,
- FieldAttributes.HasFieldMarshal,
- FieldAttributes.HasFieldRVA,
- FieldAttributes.InitOnly,
- FieldAttributes.Literal,
- FieldAttributes.NotSerialized,
- FieldAttributes.PinvokeImpl,
- FieldAttributes.Private,
- FieldAttributes.PrivateScope,
- FieldAttributes.Public,
- FieldAttributes.RTSpecialName,
- FieldAttributes.SpecialName,
- FieldAttributes.Static
- };
-
- for (int i = 0; i < attributes.Length; ++i)
- {
- FieldAttributes attribute = attributes[i];
- string desiredFieldName = fieldName + i.ToString();
- FieldBuilder fb = builder.DefineUninitializedData(desiredFieldName, size, attribute);
- int desiredAttribute = ((int)attribute | (int)FieldAttributes.Static) & ~ReservedMaskFieldAttribute;
- VerificationHelper(fb, desiredFieldName, (FieldAttributes)desiredAttribute);
- }
+ yield return new object[] { FieldAttributes.Assembly };
+ yield return new object[] { FieldAttributes.FamANDAssem };
+ yield return new object[] { FieldAttributes.Family };
+ yield return new object[] { FieldAttributes.FamORAssem };
+ yield return new object[] { FieldAttributes.FieldAccessMask };
+ yield return new object[] { FieldAttributes.HasDefault };
+ yield return new object[] { FieldAttributes.HasFieldMarshal };
+ yield return new object[] { FieldAttributes.HasFieldRVA };
+ yield return new object[] { FieldAttributes.InitOnly };
+ yield return new object[] { FieldAttributes.Literal };
+ yield return new object[] { FieldAttributes.NotSerialized };
+ yield return new object[] { FieldAttributes.PinvokeImpl };
+ yield return new object[] { FieldAttributes.Private };
+ yield return new object[] { FieldAttributes.PrivateScope };
+ yield return new object[] { FieldAttributes.Public };
+ yield return new object[] { FieldAttributes.RTSpecialName };
+ yield return new object[] { FieldAttributes.SpecialName };
+ yield return new object[] { FieldAttributes.Static };
}
- [Fact]
- public void TestWithBoundaryData()
+ [Theory]
+ [MemberData(nameof(Attributes_TestData))]
+ public void DefineUnitializedData(FieldAttributes attributes)
{
- ModuleBuilder builder = GetModuleBuilder();
- string fieldName = "PosTest2_";
- int[] sizeValues = new int[] {
- 1,
- 0x003f0000 - 1
- };
- FieldAttributes[] attributes = new FieldAttributes[] {
- FieldAttributes.Assembly,
- FieldAttributes.FamANDAssem,
- FieldAttributes.Family,
- FieldAttributes.FamORAssem,
- FieldAttributes.FieldAccessMask,
- FieldAttributes.HasDefault,
- FieldAttributes.HasFieldMarshal,
- FieldAttributes.HasFieldRVA,
- FieldAttributes.InitOnly,
- FieldAttributes.Literal,
- FieldAttributes.NotSerialized,
- FieldAttributes.PinvokeImpl,
- FieldAttributes.Private,
- FieldAttributes.PrivateScope,
- FieldAttributes.Public,
- FieldAttributes.RTSpecialName,
- FieldAttributes.SpecialName,
- FieldAttributes.Static
- };
-
- for (int i = 0; i < sizeValues.Length; ++i)
+ ModuleBuilder module = Helpers.DynamicModule();
+ foreach (int size in new int[] { 1, 2, 0x003f0000 - 1 })
{
- for (int j = 0; j < attributes.Length; ++j)
- {
- FieldAttributes attribute = attributes[j];
- string desiredFieldName = fieldName + i.ToString() + "_" + j.ToString();
- FieldBuilder fb = builder.DefineUninitializedData(desiredFieldName, sizeValues[i], attribute);
-
- int desiredAttribute = ((int)attribute | (int)FieldAttributes.Static) & (~ReservedMaskFieldAttribute);
- VerificationHelper(fb, desiredFieldName, (FieldAttributes)desiredAttribute);
- }
- }
- }
-
- [Fact]
- public void TestThrowsExceptionWithZeroLengthName()
- {
- ModuleBuilder builder = GetModuleBuilder();
- FieldAttributes[] attributes = new FieldAttributes[] {
- FieldAttributes.Assembly,
- FieldAttributes.FamANDAssem,
- FieldAttributes.Family,
- FieldAttributes.FamORAssem,
- FieldAttributes.FieldAccessMask,
- FieldAttributes.HasDefault,
- FieldAttributes.HasFieldMarshal,
- FieldAttributes.HasFieldRVA,
- FieldAttributes.InitOnly,
- FieldAttributes.Literal,
- FieldAttributes.NotSerialized,
- FieldAttributes.PinvokeImpl,
- FieldAttributes.Private,
- FieldAttributes.PrivateScope,
- FieldAttributes.Public,
- FieldAttributes.RTSpecialName,
- FieldAttributes.SpecialName,
- FieldAttributes.Static
- };
- int size = _generator.GetByte();
+ FieldBuilder field = module.DefineUninitializedData(size.ToString(), size, attributes);
- for (int i = 0; i < attributes.Length; ++i)
- {
- VerificationHelper(builder, "", size, attributes[i], typeof(ArgumentException));
+ int expectedAttributes = ((int)attributes | (int)FieldAttributes.Static) & ~ReservedMaskFieldAttribute;
+ Assert.Equal(size.ToString(), field.Name);
+ Assert.Equal((FieldAttributes)expectedAttributes, field.Attributes);
}
}
- [Fact]
- public void TestThrowsExceptionWithInvalidSizeData()
+ [Theory]
+ [MemberData(nameof(Attributes_TestData))]
+ public void DefineUnitializedData_EmptyName_ThrowsArgumentException(FieldAttributes attributes)
{
- int[] sizeValues = new int[] {
- 0,
- -1,
- 0x003f0000,
- 0x003f0000 + 1
- };
-
- ModuleBuilder builder = GetModuleBuilder();
-
- FieldAttributes[] attributes = new FieldAttributes[] {
- FieldAttributes.Assembly,
- FieldAttributes.FamANDAssem,
- FieldAttributes.Family,
- FieldAttributes.FamORAssem,
- FieldAttributes.FieldAccessMask,
- FieldAttributes.HasDefault,
- FieldAttributes.HasFieldMarshal,
- FieldAttributes.HasFieldRVA,
- FieldAttributes.InitOnly,
- FieldAttributes.Literal,
- FieldAttributes.NotSerialized,
- FieldAttributes.PinvokeImpl,
- FieldAttributes.Private,
- FieldAttributes.PrivateScope,
- FieldAttributes.Public,
- FieldAttributes.RTSpecialName,
- FieldAttributes.SpecialName,
- FieldAttributes.Static
- };
-
- for (int i = 0; i < sizeValues.Length; ++i)
- {
- for (int j = 0; j < attributes.Length; ++j)
- {
- FieldAttributes attribute = attributes[j];
- VerificationHelper(builder, "", sizeValues[i], attribute, typeof(ArgumentException));
- }
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentException>("name", () => module.DefineUninitializedData("", 1, attributes));
}
- [Fact]
- public void TestThrowsExceptionWithNullName()
+ [Theory]
+ [MemberData(nameof(Attributes_TestData))]
+ public void DefineUnitializedData_InvalidSize_ThrowsArgumentException(FieldAttributes attributes)
{
- ModuleBuilder builder = GetModuleBuilder();
- FieldAttributes[] attributes = new FieldAttributes[] {
- FieldAttributes.Assembly,
- FieldAttributes.FamANDAssem,
- FieldAttributes.Family,
- FieldAttributes.FamORAssem,
- FieldAttributes.FieldAccessMask,
- FieldAttributes.HasDefault,
- FieldAttributes.HasFieldMarshal,
- FieldAttributes.HasFieldRVA,
- FieldAttributes.InitOnly,
- FieldAttributes.Literal,
- FieldAttributes.NotSerialized,
- FieldAttributes.PinvokeImpl,
- FieldAttributes.Private,
- FieldAttributes.PrivateScope,
- FieldAttributes.Public,
- FieldAttributes.RTSpecialName,
- FieldAttributes.SpecialName,
- FieldAttributes.Static
- };
- int size = _generator.GetByte();
-
- for (int i = 0; i < attributes.Length; ++i)
+ ModuleBuilder module = Helpers.DynamicModule();
+ foreach (int size in new int[] { -1, 0, 0x003f0000, 0x003f0000 + 1 })
{
- VerificationHelper(builder, null, size, attributes[i], typeof(ArgumentNullException));
+ Assert.Throws<ArgumentException>(null, () => module.DefineUninitializedData("TestField", size, attributes));
}
}
- [Fact]
- public void TestThrowsExceptionOnCreateGlobalFunctionCalledPreviously()
+ [Theory]
+ [MemberData(nameof(Attributes_TestData))]
+ public void DefineUnitializedData_NullName_ThrowsArgumentNullException(FieldAttributes attributes)
{
- FieldAttributes[] attributes = new FieldAttributes[] {
- FieldAttributes.Assembly,
- FieldAttributes.FamANDAssem,
- FieldAttributes.Family,
- FieldAttributes.FamORAssem,
- FieldAttributes.FieldAccessMask,
- FieldAttributes.HasDefault,
- FieldAttributes.HasFieldMarshal,
- FieldAttributes.HasFieldRVA,
- FieldAttributes.InitOnly,
- FieldAttributes.Literal,
- FieldAttributes.NotSerialized,
- FieldAttributes.PinvokeImpl,
- FieldAttributes.Private,
- FieldAttributes.PrivateScope,
- FieldAttributes.Public,
- FieldAttributes.RTSpecialName,
- FieldAttributes.SpecialName,
- FieldAttributes.Static
- };
- int size = _generator.GetByte();
- ModuleBuilder testModuleBuilder = GetModuleBuilder();
-
- testModuleBuilder.CreateGlobalFunctions();
- string fieldName = "NegTest4_";
-
- for (int i = 0; i < attributes.Length; ++i)
- {
- VerificationHelper(testModuleBuilder, fieldName + i.ToString(), size, attributes[i], typeof(InvalidOperationException));
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentNullException>("name", () => module.DefineUninitializedData(null, 1, attributes));
}
-
- private void VerificationHelper(FieldBuilder fb, string desiredName, FieldAttributes desiredAttribute)
+ [Theory]
+ [MemberData(nameof(Attributes_TestData))]
+ public void DefineUninitalizedData_CreateGlobalFunctionsAlreadyCalled_ThrowsInvalidOperationException(FieldAttributes attributes)
{
- Assert.Equal(desiredName, fb.Name);
- Assert.Equal(desiredAttribute, fb.Attributes);
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ module.CreateGlobalFunctions();
- private void VerificationHelper(ModuleBuilder builder, string name, int size, FieldAttributes attribute, Type desiredException)
- {
- Assert.Throws(desiredException, () => { FieldBuilder fieldBuilder = builder.DefineUninitializedData(name, size, attribute); });
+ Assert.Throws<InvalidOperationException>(() => module.DefineUninitializedData("TestField", 1, attributes));
}
}
}
// 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 ModuleBuilderGetArrayMethod
{
- private const string DefaultAssemblyName = "ModuleBuilderGetArrayMethod";
- private const AssemblyBuilderAccess DefaultAssemblyBuilderAccess = AssemblyBuilderAccess.Run;
- private const string DefaultModuleName = "DynamicModule";
-
- private ModuleBuilder TestModuleBuilder
+ public static IEnumerable<object[]> CallingConventions_TestData()
{
- get
- {
- AssemblyName name = new AssemblyName(DefaultAssemblyName);
- AssemblyBuilder asmBuilder = AssemblyBuilder.DefineDynamicAssembly(name, DefaultAssemblyBuilderAccess);
- _moduleBuilder = TestLibrary.Utilities.GetModuleBuilder(asmBuilder, "Module1");
- return _moduleBuilder;
- }
+ 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 };
}
- private ModuleBuilder _moduleBuilder;
-
- [Fact]
- public void TestWithValidArrayValuesAndVoidReturnTypeMethod()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_ValidArrayValues_VoidReturnType(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(ModuleBuilderGetArrayMethod[]);
- string methodName = "PosTest1_";
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(void),
- new Type[] { });
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString(), callingConvention, typeof(void), new Type[0]);
}
- [Fact]
- public void TestWithValidArrayValuesAndValueReturnTypeMethod()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_ValidArrayValues_ValueReturnType(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(int[]);
- string methodName = "PosTest2_";
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- new Type[] { });
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(int[]), callingConvention.ToString(), callingConvention, typeof(int), new Type[0]);
}
- [Fact]
- public void TestWithValidArrayValuesAndReferenceReturnTypeMethod()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_ValidArrayValues_ReferenceReturnType(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(object[]);
- string methodName = "PosTest3_";
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(object),
- new Type[] { });
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(object[]), callingConvention.ToString(), callingConvention, typeof(object), new Type[0]);
}
- [Fact]
- public void TestWithValidArrayValuesAndWithValueTypeParameterMethod()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_ValidArrayValues_ValueParameterType(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(object[]);
- string methodName = "PosTest4_";
- Type[] parametersType = new Type[] {
- typeof(int)
- };
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(object[]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(int) });
- parametersType = new Type[] {
- typeof(int),
- typeof(MBTestStruct)
- };
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- }
+ VerifyGetArrayMethod(module, typeof(object[]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(int), typeof(MBTestStruct) });
}
- [Fact]
- public void TestWithValidArrayValuesAndReferenceTypeParameterMethod()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_ValidArrayValues_ReferenceParameterType(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(ModuleBuilderGetArrayMethod[]);
- string methodName = "PosTest5_";
- Type[] parametersType = new Type[] {
- typeof(object)
- };
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(object) });
- parametersType = new Type[] {
- typeof(object),
- typeof(string),
- typeof(ModuleBuilderGetArrayMethod)
- };
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- }
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(object), typeof(string), typeof(ModuleBuilderGetArrayMethod) });
}
- [Fact]
- public void TestWithValidValuesJaggedDimensionArray()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_JaggedArray(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(ModuleBuilderGetArrayMethod[][]);
- string methodName = "PosTest6_";
- int errorNo = 1;
- Type[] parametersType = new Type[] {
- typeof(object)
- };
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- errorNo++;
- }
-
- parametersType = new Type[] {
- typeof(object),
- typeof(int),
- typeof(ModuleBuilderGetArrayMethod)
- };
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[][]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(object) });
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- errorNo++;
- }
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[][]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(object), typeof(int), typeof(ModuleBuilderGetArrayMethod) });
}
- [Fact]
- public void TestWithValidValuesOnMultiDimensionArray()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_MultiDimensionalArray(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(ModuleBuilderGetArrayMethod[,]);
- string methodName = "PosTest7_";
- int errorNo = 1;
- Type[] parametersType = new Type[] {
- typeof(object)
- };
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- errorNo++;
- }
-
- parametersType = new Type[] {
- typeof(object),
- typeof(int),
- typeof(ModuleBuilderGetArrayMethod)
- };
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[,]), callingConvention.ToString() + "1", callingConvention, typeof(int), new Type[] { typeof(object) });
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(int),
- parametersType);
- errorNo++;
- }
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[,]), callingConvention.ToString() + "2", callingConvention, typeof(int), new Type[] { typeof(object), typeof(int), typeof(ModuleBuilderGetArrayMethod) });
}
- [Fact]
- public void TestWithParameterTypesToNull()
+ [Theory]
+ [MemberData(nameof(CallingConventions_TestData))]
+ public void GetArrayMethod_NullParameters(CallingConventions callingConvention)
{
- CallingConventions[] conventions = new CallingConventions[] {
- CallingConventions.Any,
- CallingConventions.ExplicitThis,
- CallingConventions.HasThis,
- CallingConventions.Standard,
- CallingConventions.VarArgs
- };
- Type arrayClass = typeof(ModuleBuilderGetArrayMethod[]);
- string methodName = "PosTest8_";
-
- for (int i = 0; i < conventions.Length; ++i)
- {
- VerificationHelper(TestModuleBuilder,
- arrayClass,
- methodName + i.ToString(),
- conventions[i],
- typeof(void),
- null);
- }
+ ModuleBuilder module = Helpers.DynamicModule();
+ VerifyGetArrayMethod(module, typeof(ModuleBuilderGetArrayMethod[]), callingConvention.ToString(), callingConvention, typeof(void), null);
}
- [Fact]
- public void TestThrowsExceptionWhenNotArray()
+ [Theory]
+ [InlineData(typeof(ModuleBuilderGetArrayMethod))]
+ [InlineData(typeof(int))]
+ [InlineData(typeof(Array))]
+ [InlineData(typeof(void))]
+ public void GetArrayMethod_ArrayClassNotArray_ThrowsArgumentException(Type arrayClass)
{
- VerificationHelper(
- TestModuleBuilder,
- typeof(ModuleBuilderGetArrayMethod),
- "NegTest1_1",
- CallingConventions.Standard,
- typeof(void),
- new Type[] { },
- typeof(ArgumentException));
- VerificationHelper(
- TestModuleBuilder,
- typeof(int),
- "NegTest1_2",
- CallingConventions.Standard,
- typeof(void),
- new Type[] { },
- typeof(ArgumentException));
- VerificationHelper(
- TestModuleBuilder,
- typeof(Array),
- "NegTest1_3",
- CallingConventions.Standard,
- typeof(void),
- new Type[] { },
- typeof(ArgumentException));
- VerificationHelper(
- TestModuleBuilder,
- typeof(void),
- "NegTest1_4",
- CallingConventions.Standard,
- typeof(void),
- new Type[] { },
- typeof(ArgumentException));
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentException>(null, () => module.GetArrayMethod(arrayClass, "TestMethod", CallingConventions.Standard, typeof(void), new Type[0]));
}
[Fact]
- public void TestThrowsExceptionOnNullArrayClassOrMethodName()
+ public void GetArrayMethod_NullArgument_ThrowsArgumentNullException()
{
- VerificationHelper(
- TestModuleBuilder,
- null,
- "NegTest2_1",
- CallingConventions.Standard,
- typeof(void),
- new Type[] { },
- typeof(ArgumentNullException));
- VerificationHelper(
- TestModuleBuilder,
- typeof(ArgumentNullException[]),
- null,
- CallingConventions.Standard,
- typeof(void),
- new Type[] { },
- typeof(ArgumentNullException));
- VerificationHelper(
- TestModuleBuilder,
- typeof(ArgumentNullException[]),
- "NegTest2_2",
- CallingConventions.Standard,
- typeof(void),
- new Type[] { null },
- typeof(ArgumentNullException));
- }
+ ModuleBuilder module = Helpers.DynamicModule();
- private void VerificationHelper(ModuleBuilder module, Type arrayClass, string methodName, CallingConventions convention, Type returnType, Type[] parameterTypes)
- {
- MethodInfo method = module.GetArrayMethod(arrayClass, methodName, convention, returnType, parameterTypes);
+ Assert.Throws<ArgumentNullException>("arrayClass", () => module.GetArrayMethod(null, "TestMethod", CallingConventions.Standard, typeof(void), new Type[0]));
+ Assert.Throws<ArgumentNullException>("methodName", () => module.GetArrayMethod(typeof(string[]), null, CallingConventions.Standard, typeof(void), new Type[0]));
- Assert.True(method.DeclaringType.Equals(arrayClass));
- Assert.Equal(method.Name, methodName);
- Assert.Equal(method.CallingConvention, convention);
- Assert.True(method.ReturnType.Equals(returnType));
+ Assert.Throws<ArgumentNullException>("argument", () => module.GetArrayMethod(typeof(string[]), "TestMethod", CallingConventions.Standard, typeof(void), new Type[] { null }));
}
- private void VerificationHelper(ModuleBuilder module, Type arrayClass, string methodName, CallingConventions convention, Type returnType, Type[] parameterTypes, Type desiredException)
+ private void VerifyGetArrayMethod(ModuleBuilder module, Type arrayClass, string methodName, CallingConventions callingConvention, Type returnType, Type[] parameterTypes)
{
- Assert.Throws(desiredException, () => { MethodInfo method = module.GetArrayMethod(arrayClass, methodName, convention, returnType, parameterTypes); });
+ MethodInfo method = module.GetArrayMethod(arrayClass, methodName, callingConvention, returnType, parameterTypes);
+
+ Assert.Equal(arrayClass, method.DeclaringType);
+ Assert.Equal(methodName, method.Name);
+ Assert.Equal(callingConvention, method.CallingConvention);
+ Assert.Equal(returnType, method.ReturnType);
}
}
}
--- /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 ModuleBuilderSetCustomAttribute
+ {
+ [Fact]
+ public void SetCustomAttribute_ConstructorInfo_ByteArray()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
+ module.SetCustomAttribute(attributeConstructor, new byte[] { 01, 00, 05, 00, 00, 00 });
+
+ object[] attributes = module.GetCustomAttributes().ToArray();
+ Assert.Equal(1, attributes.Length);
+ Assert.True(attributes[0] is IntAllAttribute);
+ Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
+ }
+
+ [Fact]
+ public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructor_ThrowsArgumentNullException()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentNullException>("con", () => module.SetCustomAttribute(null, new byte[] { 01, 00, 05, 00, 00, 00 }));
+ }
+ [Fact]
+ public void SetCustomAttribute_CustomAttributeBuilder()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ ConstructorInfo attributeConstructor = typeof(IntAllAttribute).GetConstructor(new Type[] { typeof(int) });
+ CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(attributeConstructor, new object[] { 5 });
+ module.SetCustomAttribute(attributeBuilder);
+
+ object[] attributes = module.GetCustomAttributes().ToArray();
+ Assert.Equal(1, attributes.Length);
+ Assert.True(attributes[0] is IntAllAttribute);
+ Assert.Equal(5, ((IntAllAttribute)attributes[0])._i);
+ }
+
+ [Fact]
+ public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException()
+ {
+ ModuleBuilder module = Helpers.DynamicModule();
+ Assert.Throws<ArgumentNullException>("customBuilder", () => module.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;
-using System.Linq;
-
-namespace System.Reflection.Emit.Tests
-{
- [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
- public class MBMyAttribute1 : Attribute
- {
- public int i;
-
- public MBMyAttribute1(int i)
- {
- this.i = i;
- }
- }
-
- public class ModuleBuilderSetCustomAttribute1
- {
- [Fact]
- public void TestSetCustomAttribute()
- {
- AssemblyName TestAssemblyName = new AssemblyName();
- TestAssemblyName.Name = "TestAssembly";
- AssemblyBuilder TestAssembly = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
- ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssembly, "Module1");
- ConstructorInfo infoConstructor = typeof(MBMyAttribute1).GetConstructor(new Type[] { typeof(int) });
- TestModuleBuilder.SetCustomAttribute(infoConstructor, new byte[] { 01, 00, 05, 00, 00, 00 });
- object[] attributes = TestModuleBuilder.GetCustomAttributes().Select(a => (object)a).ToArray();
- Assert.Equal(1, attributes.Length);
- Assert.True(attributes[0] is MBMyAttribute1);
- Assert.Equal(5, ((MBMyAttribute1)attributes[0]).i);
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullConstructorInfo()
- {
- AssemblyName TestAssemblyName = new AssemblyName();
- TestAssemblyName.Name = "TestAssembly";
- AssemblyBuilder TestAssembly = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
- ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssembly, "Module1");
- Assert.Throws<ArgumentNullException>(() => { TestModuleBuilder.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 MBMyAttribute2 : Attribute
- {
- public int i;
-
- public MBMyAttribute2(int i)
- {
- this.i = i;
- }
- }
-
- public class ModuleBuilderSetCustomAttribute2
- {
- [Fact]
- public void TestSetCustomAttribute()
- {
- AssemblyName TestAssemblyName = new AssemblyName();
- TestAssemblyName.Name = "TestAssembly";
- AssemblyBuilder TestAssembly = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
- ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssembly, "Module1");
- ConstructorInfo infoConstructor = typeof(MBMyAttribute2).GetConstructor(new Type[] { typeof(int) });
- CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(infoConstructor, new object[] { 5 });
- TestModuleBuilder.SetCustomAttribute(attributeBuilder);
- object[] attributes = TestModuleBuilder.GetCustomAttributes().Select(a => (object)a).ToArray();
- Assert.Equal(1, attributes.Length);
- Assert.True(attributes[0] is MBMyAttribute2);
- Assert.Equal(5, ((MBMyAttribute2)attributes[0]).i);
- }
-
- [Fact]
- public void TestThrowsExceptionOnNullBuilder()
- {
- AssemblyName TestAssemblyName = new AssemblyName();
- TestAssemblyName.Name = "TestAssembly";
- AssemblyBuilder TestAssembly = AssemblyBuilder.DefineDynamicAssembly(TestAssemblyName, AssemblyBuilderAccess.Run);
- ModuleBuilder TestModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(TestAssembly, "Module1");
- CustomAttributeBuilder attributeBuilder = null;
- Assert.Throws<ArgumentNullException>(() => { TestModuleBuilder.SetCustomAttribute(attributeBuilder); });
- }
- }
-}
<Compile Include="ModuleBuilder\ModuleBuilderCreateGlobalFunctions.cs" />
<Compile Include="ModuleBuilder\ModuleBuilderDefineEnum.cs" />
<Compile Include="ModuleBuilder\ModuleBuilderDefineInitializedData.cs" />
- <Compile Include="ModuleBuilder\ModuleBuilderDefineType1.cs" />
- <Compile Include="ModuleBuilder\ModuleBuilderDefineType2.cs" />
- <Compile Include="ModuleBuilder\ModuleBuilderDefineType3.cs" />
+ <Compile Include="ModuleBuilder\ModuleBuilderDefineType.cs" />
<Compile Include="ModuleBuilder\ModuleBuilderDefineUninitializedData.cs" />
<Compile Include="ModuleBuilder\ModuleBuilderGetArrayMethod.cs" />
- <Compile Include="ModuleBuilder\ModuleBuilderSetCustomAttribute1.cs" />
- <Compile Include="ModuleBuilder\ModuleBuilderSetCustomAttribute2.cs" />
+ <Compile Include="ModuleBuilder\ModuleBuilderSetCustomAttribute.cs" />
<Compile Include="PropertyBuilder\PropertyBuilderAddOtherMethod.cs" />
<Compile Include="PropertyBuilder\PropertyBuilderAttributes.cs" />
<Compile Include="PropertyBuilder\PropertyBuilderCanRead.cs" />
{
public class EmptyAttribute : Attribute { }
+ [AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
+ public class IntAllAttribute : Attribute
+ {
+ public int _i;
+ public IntAllAttribute(int i) { _i = i; }
+ }
+
public static class Helpers
{
public static AssemblyBuilder DynamicAssembly(string name = "TestAssembly")