From 10ffc45068284596ad356b0d19643d8c73064b0f Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Sat, 25 Jun 2016 15:29:56 +0100 Subject: [PATCH] Cleanup PropertyBuilder tests Commit migrated from https://github.com/dotnet/corefx/commit/65d4697479facd6fa58b295076eed20434fb59fe --- .../PropertyBuilderAddOtherMethod.cs | 191 ++------ .../PropertyBuilder/PropertyBuilderAttributes.cs | 89 +--- .../PropertyBuilder/PropertyBuilderCanRead.cs | 180 ++----- .../PropertyBuilder/PropertyBuilderCanWrite.cs | 179 ++----- .../PropertyBuilderDeclaringType.cs | 146 +----- .../PropertyBuilderGetIndexParameters.cs | 54 +-- .../PropertyBuilder/PropertyBuilderGetValue.cs | 27 ++ .../PropertyBuilder/PropertyBuilderGetValue1.cs | 63 --- .../tests/PropertyBuilder/PropertyBuilderName.cs | 85 +--- .../PropertyBuilder/PropertyBuilderPropertyType.cs | 129 +---- .../PropertyBuilder/PropertyBuilderSetConstant.cs | 520 ++------------------- .../PropertyBuilderSetCustomAttribute.cs | 144 ++++++ .../PropertyBuilderSetCustomAttribute1.cs | 169 ------- .../PropertyBuilderSetCustomAttribute2.cs | 173 ------- .../PropertyBuilder/PropertyBuilderSetGetMethod.cs | 250 ++-------- .../PropertyBuilder/PropertyBuilderSetSetMethod.cs | 254 ++-------- .../PropertyBuilder/PropertyBuilderSetValue.cs | 32 ++ .../PropertyBuilder/PropertyBuilderSetValue1.cs | 68 --- .../tests/System.Reflection.Emit.Tests.csproj | 7 +- 19 files changed, 502 insertions(+), 2258 deletions(-) create mode 100644 src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue1.cs create mode 100644 src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute1.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute2.cs create mode 100644 src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue.cs delete mode 100644 src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue1.cs diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAddOtherMethod.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAddOtherMethod.cs index c9cfd91..fd5f245 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAddOtherMethod.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAddOtherMethod.cs @@ -2,198 +2,63 @@ // 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 PropertyBuilderTest2 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestWithPublicSingleParameterIntReturnTypeMethod() + [Theory] + [InlineData(MethodAttributes.Public, CallingConventions.HasThis, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)] + [InlineData(MethodAttributes.Private, CallingConventions.HasThis, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)] + [InlineData(MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Any, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance)] + public void AddOtherMethod(MethodAttributes attributes, CallingConventions callingConventions, BindingFlags bindingFlags) { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - Type[] paramTypes = new Type[] - { - typeof(int) - }; + Type[] paramTypes = new Type[] { typeof(int) }; - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - new Type[0]); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - MethodAttributes.Public, - CallingConventions.HasThis, - typeof(int), - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]); + MethodBuilder method = type.DefineMethod("TestMethod", attributes, callingConventions, typeof(int), paramTypes); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ldarg_1); methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.AddOtherMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, - BindingFlags.Public | - BindingFlags.NonPublic | - BindingFlags.Instance); - VerifyAddMethodsResult(myProperty, myMethodBuilder); - } + property.AddOtherMethod(method); + Type createdType = type.CreateTypeInfo().AsType(); + PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingFlags); - [Fact] - public void TestWithPrivateSingleParameterIntReturnTypeMethod() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - Type[] paramTypes = new Type[] - { - typeof(int) - }; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - new Type[0]); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - MethodAttributes.Private, - CallingConventions.HasThis, - typeof(int), - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldarg_1); - methodILGenerator.Emit(OpCodes.Ret); - - myPropertyBuilder.AddOtherMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, - BindingFlags.Public | - BindingFlags.NonPublic | - BindingFlags.Instance); - VerifyAddMethodsResult(myProperty, myMethodBuilder); - } - - [Fact] - public void TestWithPublicStaticSingleParameterIntReturnTypeMethod() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - Type[] paramTypes = new Type[] - { - typeof(int) - }; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - new Type[0]); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - MethodAttributes.Static | - MethodAttributes.Public, - typeof(int), - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ret); - - myPropertyBuilder.AddOtherMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, - BindingFlags.Public | - BindingFlags.NonPublic | - BindingFlags.Instance | - BindingFlags.Static); - VerifyAddMethodsResult(myProperty, myMethodBuilder); - } - - private void VerifyAddMethodsResult(PropertyInfo myProperty, MethodBuilder expectedMethod) - { - MethodInfo[] actualMethods = myProperty.GetAccessors(true); + MethodInfo[] actualMethods = createdProperty.GetAccessors(true); Assert.Equal(1, actualMethods.Length); - Assert.Equal(expectedMethod.Name, actualMethods[0].Name); + Assert.Equal(method.Name, actualMethods[0].Name); } [Fact] - public void TestThrowsExceptionOnNullMethodBuilder() + public void AddOtherMethod_NullMethodBuilder_ThrowsArgumentNullExceptio() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]); - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - new Type[0]); - myMethodBuilder = null; - Assert.Throws(() => - { - myPropertyBuilder.AddOtherMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - }); + Assert.Throws("mdBuilder", () => property.AddOtherMethod(null)); } [Fact] - public void TestThrowsExceptionOnCreateTypeCalled() + public void AddOtherMethod_TypeAlreadyCreated_ThrowsInvalidOperationException() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - Type[] paramTypes = new Type[] - { - typeof(int) - }; + Type[] paramTypes = new Type[] { typeof(int) }; - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - new Type[0]); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - MethodAttributes.Public, - CallingConventions.HasThis, - typeof(int), - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), paramTypes); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ldarg_1); methodILGenerator.Emit(OpCodes.Ret); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.AddOtherMethod(myMethodBuilder); }); + + type.CreateTypeInfo().AsType(); + Assert.Throws(() => property.AddOtherMethod(method)); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAttributes.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAttributes.cs index c2c2eff..bf201c9 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAttributes.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderAttributes.cs @@ -2,92 +2,23 @@ // 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 PropertyBuilderTest3 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicPropertyName = "TestDynamicProperty"; - - private TypeBuilder GetTypeBuilder(string typeName, TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder( - myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(typeName, typeAtt); - } - - [Fact] - public void TestWithHasDefault() - { - PropertyAttributes propertyAttr = PropertyAttributes.HasDefault; - ExecutePosTest(propertyAttr); - } - - [Fact] - public void TestWithNone() - { - PropertyAttributes propertyAttr = PropertyAttributes.None; - ExecutePosTest(propertyAttr); - } - - [Fact] - public void TestWithRTSpecialName() - { - PropertyAttributes propertyAttr = PropertyAttributes.RTSpecialName; - ExecutePosTest(propertyAttr); - } - - [Fact] - public void TestWithSpecialName() + [Theory] + [InlineData(PropertyAttributes.HasDefault)] + [InlineData(PropertyAttributes.None)] + [InlineData(PropertyAttributes.RTSpecialName)] + [InlineData(PropertyAttributes.SpecialName)] + [InlineData(PropertyAttributes.SpecialName | PropertyAttributes.RTSpecialName | PropertyAttributes.None | PropertyAttributes.HasDefault)] + public void ExecutePosTest(PropertyAttributes attributes) { - PropertyAttributes propertyAttr = PropertyAttributes.SpecialName; - ExecutePosTest(propertyAttr); - } - - [Fact] - public void TestWithCombinations() - { - PropertyAttributes propertyAttr = - PropertyAttributes.SpecialName | - PropertyAttributes.RTSpecialName | - PropertyAttributes.None | - PropertyAttributes.HasDefault; - ExecutePosTest(propertyAttr); - } - - private void ExecutePosTest(PropertyAttributes propertyAttr) - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - PropertyAttributes actualAttributes; - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Class | - TypeAttributes.Public); - - myPropertyBuilder = myTypeBuilder.DefineProperty( - DynamicPropertyName, - propertyAttr, - typeof(int), - null); - actualAttributes = myPropertyBuilder.Attributes; - Assert.Equal(propertyAttr, actualAttributes); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", attributes, typeof(int), null); + Assert.Equal(attributes, property.Attributes); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanRead.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanRead.cs index ec73102..90fcb11 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanRead.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanRead.cs @@ -2,176 +2,76 @@ // 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 PropertyBuilderTest4 + public class PropertyBuilderTest { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - [Fact] - public void TestPropertyWithGetAccessor() + public void CanRead_OnlyGetAccessor_ReturnsTrue() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.Private | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - expectedValue = true; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField( - DynamicFieldName, - typeof(int), - FieldAttributes.Private); - - myPropertyBuilder = myTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - getMethodAtt, - typeof(int), - new Type[0], - myFieldBuilder); - - myPropertyBuilder.SetGetMethod(myMethodBuilder); - actualValue = myPropertyBuilder.CanRead; - Assert.Equal(expectedValue, actualValue); + MethodAttributes getMethodAttributes = MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), new Type[0]); + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldarg_0); + methodILGenerator.Emit(OpCodes.Ldfld, field); + methodILGenerator.Emit(OpCodes.Ret); + + property.SetGetMethod(method); + Assert.True(property.CanRead); } [Fact] - public void TestPropertyWithSetAccessor() + public void CanRead_OnlyGetAccessor_ReturnsFalse() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - expectedValue = false; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + MethodAttributes setMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) }); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ldarg_1); - methodILGenerator.Emit(OpCodes.Stfld, myFieldBuilder); + methodILGenerator.Emit(OpCodes.Stfld, field); methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - actualValue = myPropertyBuilder.CanRead; - Assert.Equal(expectedValue, actualValue); + property.SetSetMethod(method); + Assert.False(property.CanRead); } [Fact] - public void TestPropertyWithNoAccessors() + public void CanRead_NoAccessors_ReturnsFalse() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - expectedValue = false; - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - actualValue = myPropertyBuilder.CanRead; - Assert.Equal(expectedValue, actualValue); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + + Assert.False(property.CanRead); } [Fact] - public void TestPropertyWithPublicStaticGetAccessor() + public void CanRead_PublicStaticGetAccessor_ReturnsTrue() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.Static | - MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - expectedValue = true; - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); - methodILGenerator.Emit(OpCodes.Ret); + MethodAttributes getMethodAttributes = MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null); - myPropertyBuilder.SetGetMethod(myMethodBuilder); - actualValue = myPropertyBuilder.CanRead; - Assert.Equal(expectedValue, actualValue); - } - - private MethodBuilder ImplementMethod(TypeBuilder myTypeBuilder, string methodName, MethodAttributes methodAttr, Type returnType, Type[] paramTypes, FieldBuilder myFieldBuilder) - { - MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod( - methodName, - methodAttr, - returnType, - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldfld, field); methodILGenerator.Emit(OpCodes.Ret); - return myMethodBuilder; + property.SetGetMethod(method); + Assert.True(property.CanRead); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanWrite.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanWrite.cs index 008624d..9af7011 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanWrite.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderCanWrite.cs @@ -2,176 +2,77 @@ // 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 PropertyBuilderTest5 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - [Fact] - public void TestPropertyWithSetAccessor() + public void CanWrite_OnlySetAccessor_ReturnsFalse() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Private | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - expectedValue = true; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField( - DynamicFieldName, - typeof(int), - FieldAttributes.Private); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }, - myFieldBuilder); + MethodAttributes setMethodAttributes = MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) }); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - actualValue = myPropertyBuilder.CanWrite; - Assert.Equal(expectedValue, actualValue); + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldarg_0); + methodILGenerator.Emit(OpCodes.Ldarg_1); + methodILGenerator.Emit(OpCodes.Stfld, field); + methodILGenerator.Emit(OpCodes.Ret); + + property.SetSetMethod(method); + Assert.True(property.CanWrite); } [Fact] - public void TestPropertyWithGetAccessor() + public void CanWrite_OnlyGetAccessor_ReturnsFalse() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - expectedValue = false; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - new Type[0]); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), new Type[0]); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); + methodILGenerator.Emit(OpCodes.Ldfld, field); methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.SetGetMethod(myMethodBuilder); - actualValue = myPropertyBuilder.CanWrite; - Assert.Equal(expectedValue, actualValue); + property.SetGetMethod(method); + Assert.False(property.CanWrite); } [Fact] - public void TestPropertyWithNoAccessors() + public void CanWrite_NoAccessors_ReturnsFalse() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - expectedValue = false; - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - actualValue = myPropertyBuilder.CanWrite; - Assert.Equal(expectedValue, actualValue); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + + Assert.False(property.CanWrite); } [Fact] - public void TestPropertyWithPublicStaticSetAccessor() + public void CanWrite_PublicStaticSetAccessor_ReturnsTrue() { - bool actualValue, expectedValue; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Static | - MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - expectedValue = true; - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); - methodILGenerator.Emit(OpCodes.Ret); + MethodAttributes setMethodAttributes = MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", setMethodAttributes, null, new Type[] { typeof(int) }); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - actualValue = myPropertyBuilder.CanWrite; - Assert.Equal(expectedValue, actualValue); - } - - private MethodBuilder ImplementMethod(TypeBuilder myTypeBuilder, string methodName, MethodAttributes methodAttr, Type returnType, Type[] paramTypes, FieldBuilder myFieldBuilder) - { - MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod( - methodName, - methodAttr, - returnType, - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldarg_1); - methodILGenerator.Emit(OpCodes.Stfld, myFieldBuilder); + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldfld, field); methodILGenerator.Emit(OpCodes.Ret); - return myMethodBuilder; + property.SetSetMethod(method); + Assert.True(property.CanWrite); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderDeclaringType.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderDeclaringType.cs index 3490216..ea3b013 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderDeclaringType.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderDeclaringType.cs @@ -2,156 +2,60 @@ // 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 PropertyBuilderTest6 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicNestedTypeName = "TestDynamicNestedType"; - private const string DynamicDerivedTypeName = "TestDynamicDerivedType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(string typeName, TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder( - myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(typeName, typeAtt); - } - [Fact] - public void TestPropertyInRootClass() + public void DeclaringType_RootClass() { - Type actualType; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Class | - TypeAttributes.Public); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - actualType = myPropertyBuilder.DeclaringType; - Assert.Equal(actualType, myTypeBuilder.AsType()); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + Assert.Equal(type.AsType(), property.DeclaringType); } [Fact] - public void TestPropertyInNestedClass() + public void DeclaringType_NestedClass() { - Type actualType; - TypeBuilder myTypeBuilder, myNestedTypeBuilder; - PropertyBuilder myPropertyBuilder; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + TypeBuilder nestedType = type.DefineNestedType("NestedType", TypeAttributes.Class | TypeAttributes.NestedPublic); - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Class | - TypeAttributes.Public); - myNestedTypeBuilder = myTypeBuilder.DefineNestedType( - DynamicNestedTypeName, - TypeAttributes.Class | - TypeAttributes.NestedPublic); - - myPropertyBuilder = myNestedTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - actualType = myPropertyBuilder.DeclaringType; - Assert.Equal(myNestedTypeBuilder.AsType(), actualType); + PropertyBuilder property = nestedType.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + Assert.Equal(nestedType.AsType(), property.DeclaringType); } [Fact] - public void TestPropertyInRootInterface() + public void DeclaringType_RootInterface() { - Type actualType; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract | TypeAttributes.Interface | TypeAttributes.Public); - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Class | - TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - actualType = myPropertyBuilder.DeclaringType; - Assert.Equal(myTypeBuilder.AsType(), actualType); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + Assert.Equal(type.AsType(), property.DeclaringType); } [Fact] - public void TestPropertyInDerivedClass() + public void DeclaringType_DerivedClass() { - Type actualType; - TypeBuilder myTypeBuilder, myDerivedTypeBuilder; - PropertyBuilder myPropertyBuilder; - - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Class | - TypeAttributes.Public); - myDerivedTypeBuilder = GetTypeBuilder( - DynamicDerivedTypeName, - TypeAttributes.Class | - TypeAttributes.Public); - myDerivedTypeBuilder.SetParent(myTypeBuilder.AsType()); + TypeBuilder baseClass = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public, typeName: "BaseClass"); + TypeBuilder subClass = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public, typeName: "SubClass"); + subClass.SetParent(baseClass.AsType()); - myPropertyBuilder = myDerivedTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - actualType = myPropertyBuilder.DeclaringType; - Assert.Equal(myDerivedTypeBuilder.AsType(), actualType); + PropertyBuilder property = subClass.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + Assert.Equal(subClass.AsType(), property.DeclaringType); } [Fact] - public void TestPropertyInBaseClass() + public void DeclaringType_BaseClass() { - Type actualType; - TypeBuilder myTypeBuilder, myDerivedTypeBuilder; - PropertyBuilder myPropertyBuilder; + TypeBuilder baseClass = Helpers.DynamicType(TypeAttributes.Abstract | TypeAttributes.Public, typeName: "BaseClass"); + TypeBuilder subClass = Helpers.DynamicType(TypeAttributes.Abstract | TypeAttributes.Public | TypeAttributes.Interface, typeName: "SubClass"); + subClass.SetParent(baseClass.AsType()); - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Abstract | - TypeAttributes.Public); - myDerivedTypeBuilder = GetTypeBuilder( - DynamicDerivedTypeName, - TypeAttributes.Abstract | - TypeAttributes.Public | - TypeAttributes.Interface); - myDerivedTypeBuilder.SetParent(myTypeBuilder.AsType()); - myPropertyBuilder = myTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - actualType = myPropertyBuilder.DeclaringType; - Assert.Equal(myTypeBuilder.AsType(), actualType); + PropertyBuilder property = baseClass.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + Assert.Equal(baseClass.AsType(), property.DeclaringType); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetIndexParameters.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetIndexParameters.cs index 3b4e9a3..45f3758 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetIndexParameters.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetIndexParameters.cs @@ -2,62 +2,26 @@ // 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 PropertyBuilderTest7 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - [Fact] - public void TestThrowsExceptionForNotSupported() + public void GetIndexParameters_ThrowsNotSupportedException() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - bool nonPublic = 0 == (_generator.GetInt32() & 1); - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - MethodAttributes.Public, - CallingConventions.HasThis, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + MethodBuilder method = type.DefineMethod("TestProperty", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), null); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.AddOtherMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.GetIndexParameters(); }); + property.AddOtherMethod(method); + + Type myType = type.CreateTypeInfo().AsType(); + Assert.Throws(() => property.GetIndexParameters()); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue.cs new file mode 100644 index 0000000..9a657f0 --- /dev/null +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue.cs @@ -0,0 +1,27 @@ +// 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 PropertyBuilderTest8 + { + [Fact] + public void GetValue_ThrowsNotSupportedException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), null); + + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldarg_0); + methodILGenerator.Emit(OpCodes.Ret); + property.AddOtherMethod(method); + + type.CreateTypeInfo().AsType(); + Assert.Throws(() => property.GetValue(null, null)); + } + } +} diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue1.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue1.cs deleted file mode 100644 index f96bc0a..0000000 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderGetValue1.cs +++ /dev/null @@ -1,63 +0,0 @@ -// 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 PropertyBuilderTest8 - { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestThrowsExceptionForNotSupported() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - bool nonPublic = 0 == (_generator.GetInt32() & 1); - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - MethodAttributes.Public, - CallingConventions.HasThis, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.AddOtherMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.GetValue(null, null); }); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderName.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderName.cs index d7bfa39..019b27a 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderName.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderName.cs @@ -2,89 +2,36 @@ // 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.Collections.Generic; using Xunit; namespace System.Reflection.Emit.Tests { public class PropertyBuilderTest9 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicNestedTypeName = "TestDynamicNestedType"; - private const string DynamicDerivedTypeName = "TestDynamicDerivedType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTypeBuilder(string typeName, TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder( - myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(typeName, typeAtt); - } - - [Fact] - public void TestNameOfRandomString() - { - string expectedName = new string((char)(_generator.GetInt32() % ushort.MaxValue + 1), 1) + - _generator.GetString(false, 1, 260); - - ExecutePosTest(expectedName); - } - - [Fact] - public void TestWithLanguageKeyword() + private static readonly RandomDataGenerator s_randomDataGenerator = new RandomDataGenerator(); + + public static IEnumerable Names_TestData() { - string expectedName = "class"; - ExecutePosTest(expectedName); + yield return new object[] { new string((char)(s_randomDataGenerator.GetInt32() % ushort.MaxValue + 1), 1) + s_randomDataGenerator.GetString(false, 1, 260) }; + yield return new object[] { "class" }; + yield return new object[] { new string('a', short.MaxValue) }; } - [Fact] - public void TestWithLongString() + [Theory] + [MemberData(nameof(Names_TestData))] + public void Name(string name) { - string expectedName = new string('a', Int16.MaxValue); - ExecutePosTest(expectedName); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty(name, PropertyAttributes.None, typeof(int), null); + Assert.Equal(name, property.Name); } [Fact] - public void TestWithSpecialCharacters() + public void Name_InvalidString() { - string expectedName = "1A\0\t\v\r\n\n\uDC81\uDC91"; - ExecutePosTest(expectedName); - } - - - private void ExecutePosTest(string expectedName) - { - string actualName; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Class | - TypeAttributes.Public); - - myPropertyBuilder = myTypeBuilder.DefineProperty( - expectedName, - PropertyAttributes.None, - typeof(int), - null); - actualName = myPropertyBuilder.Name; - Assert.Equal(expectedName, actualName); + // TODO: move into Names_TestData when #7166 is fixed + Name("1A\0\t\v\r\n\n\uDC81\uDC91"); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderPropertyType.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderPropertyType.cs index 8deacc0..b52adab 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderPropertyType.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderPropertyType.cs @@ -2,132 +2,39 @@ // 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 PropertyBuilderTest10 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicNestedTypeName = "TestDynamicNestedType"; - private const string DynamicDerivedTypeName = "TestDynamicDerivedType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(string typeName, TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly( - myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder( - myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(typeName, typeAtt); - } - - [Fact] - public void TestWithIntType() - { - Type expectedType = typeof(int); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithByteType() - { - Type expectedType = typeof(byte); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithDateTimeType() - { - Type expectedType = typeof(DateTime); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithDoubleType() - { - Type expectedType = typeof(double); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithStringType() - { - Type expectedType = typeof(string); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithArrayType() - { - Type expectedType = typeof(int[]); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithInterfaceType() - { - Type expectedType = typeof(IFoo); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithEnumType() - { - Type expectedType = typeof(Colors); - ExecutePosTest(expectedType); - } - - [Fact] - public void TestWithDelegateType() - { - Type expectedType = typeof(MySelector); - ExecutePosTest(expectedType); - } - - private void ExecutePosTest(Type expectedType) - { - Type actualType; - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - myTypeBuilder = GetTypeBuilder( - DynamicTypeName, - TypeAttributes.Class | - TypeAttributes.Public); - - myPropertyBuilder = myTypeBuilder.DefineProperty( - DynamicPropertyName, - PropertyAttributes.None, - expectedType, - null); - actualType = myPropertyBuilder.PropertyType; - Assert.Equal(expectedType, actualType); + [Theory] + [InlineData(typeof(int))] + [InlineData(typeof(byte))] + [InlineData(typeof(DateTime))] + [InlineData(typeof(double))] + [InlineData(typeof(string))] + [InlineData(typeof(int[]))] + [InlineData(typeof(PropertyTypeInterface))] + [InlineData(typeof(PropertyTypeEnum))] + [InlineData(typeof(PropertyTypeDelegate))] + private void PropertyType(Type returnType) + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, returnType, null); + Assert.Equal(returnType, property.PropertyType); } } - internal interface IFoo + internal interface PropertyTypeInterface { void MethodA(); } - internal enum Colors + internal enum PropertyTypeEnum { Red, Green, Blue } - internal delegate bool MySelector(int value); + internal delegate bool PropertyTypeDelegate(int value); } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetConstant.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetConstant.cs index 2a99895..3e15cc8 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetConstant.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetConstant.cs @@ -2,22 +2,13 @@ // 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.Collections.Generic; using Xunit; namespace System.Reflection.Emit.Tests { public class PropertyBuilderTest11 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; private readonly RandomDataGenerator _generator = new RandomDataGenerator(); private enum Colors @@ -27,498 +18,79 @@ namespace System.Reflection.Emit.Tests Blue = 2 } - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) + public static IEnumerable SetConstant_TestData() { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); + yield return new object[] { typeof(int), 10 }; + yield return new object[] { typeof(bool), true }; + yield return new object[] { typeof(sbyte), (sbyte)10 }; + yield return new object[] { typeof(short), (short)10 }; + yield return new object[] { typeof(long), (long)10 }; - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); + yield return new object[] { typeof(byte), (byte)10 }; + yield return new object[] { typeof(ushort), (ushort)10 }; + yield return new object[] { typeof(uint), (uint)10 }; + yield return new object[] { typeof(ulong), (ulong)10 }; - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestWithIntegerType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(int); - Type[] paramTypes = new Type[0]; - int defaultValue = _generator.GetInt32(); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is int); - Assert.Equal((int)actualValue, defaultValue); - } - - [Fact] - public void TestWithBoolType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(bool); - Type[] paramTypes = new Type[0]; - bool defaultValue = 0 == (_generator.GetInt32() & 1); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is bool); - Assert.Equal((bool)actualValue, defaultValue); - } - - [Fact] - public void TestWithSByteType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(SByte); - Type[] paramTypes = new Type[0]; - SByte defaultValue = (SByte)(_generator.GetInt32() % (SByte.MaxValue + 1)); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is sbyte); - Assert.Equal((sbyte)actualValue, defaultValue); - } - - [Fact] - public void TestWithShortType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(short); - Type[] paramTypes = new Type[0]; - Int16 defaultValue = _generator.GetInt16(); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is short); - Assert.Equal((short)actualValue, defaultValue); - } - - [Fact] - public void TestWithLongType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(long); - Type[] paramTypes = new Type[0]; - long defaultValue = _generator.GetInt64(); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is long); - Assert.Equal((long)actualValue, defaultValue); - } - - [Fact] - public void TestWithByteType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(byte); - Type[] paramTypes = new Type[0]; - byte defaultValue = _generator.GetByte(); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is byte); - Assert.Equal((byte)actualValue, defaultValue); - } - - [Fact] - public void TestWithUShortType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(ushort); - Type[] paramTypes = new Type[0]; - ushort defaultValue = (ushort)(_generator.GetInt32() % (ushort.MaxValue + 1)); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is ushort); - Assert.Equal((ushort)actualValue, defaultValue); - } - - [Fact] - public void TestWithUIntType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(uint); - Type[] paramTypes = new Type[0]; - uint defaultValue = (ushort)(_generator.GetInt64() % ((long)uint.MaxValue + 1)); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is uint); - Assert.Equal((uint)actualValue, defaultValue); - } - - [Fact] - public void TestWithULongType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(ulong); - Type[] paramTypes = new Type[0]; - ulong defaultValue = (ulong)long.MaxValue + (ulong)_generator.GetInt64(); - object actualValue; + yield return new object[] { typeof(float), (float)10 }; + yield return new object[] { typeof(double), (double)10 }; - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is ulong); - Assert.Equal((ulong)actualValue, defaultValue); - } - - [Fact] - public void TestWithFloatType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(float); - Type[] paramTypes = new Type[0]; - float defaultValue = _generator.GetSingle(); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is float); - Assert.Equal((float)actualValue, defaultValue); - } + yield return new object[] { typeof(DateTime), DateTime.Now }; + yield return new object[] { typeof(char), 'a' }; + yield return new object[] { typeof(string), "a" }; - [Fact] - public void TestWithDoubleType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(double); - Type[] paramTypes = new Type[0]; - double defaultValue = _generator.GetDouble(); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is double); - Assert.Equal((double)actualValue, defaultValue); + yield return new object[] { typeof(Colors), Colors.Blue }; + yield return new object[] { typeof(object), null }; + yield return new object[] { typeof(object), "a" }; } - [Fact] - public void TestWithDateTimeType() + [Theory] + [MemberData(nameof(SetConstant_TestData))] + public void SetConstant(Type returnType, object defaultValue) { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(DateTime); + MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + BindingFlags bindingAttributes = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static; Type[] paramTypes = new Type[0]; - DateTime defaultValue = DateTime.Now; - object actualValue; - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is DateTime); - Assert.Equal((DateTime)actualValue, defaultValue); - } + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); - [Fact] - public void TestWithCharType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(char); - Type[] paramTypes = new Type[0]; - char defaultValue = _generator.GetChar(); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is char); - Assert.Equal((char)actualValue, defaultValue); - } - - [Fact] - public void TestWithStringType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(string); - Type[] paramTypes = new Type[0]; - string defaultValue = _generator.GetString(true, 0, 260); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is string); - Assert.Equal((string)actualValue, defaultValue); - } - - [Fact] - public void TestWithEnumType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - Type returnType = typeof(Colors); - Type[] paramTypes = new Type[0]; - Colors defaultValue = (Colors)(_generator.GetInt32() % 3); - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.True(actualValue is Colors); - Assert.Equal((Colors)actualValue, defaultValue); - } - - [Fact] - public void TestWithObjectType() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - Type returnType = typeof(object); - Type[] paramTypes = new Type[0]; - object defaultValue = null; - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.Equal(defaultValue, (object)actualValue); - } - - private object ExecutePosTest(object defaultValue, MethodAttributes getMethodAttr, Type returnType, Type[] paramTypes, BindingFlags bindingAttr) - { - TypeBuilder myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - - PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.HasDefault, - returnType, null); - myPropertyBuilder.SetConstant(defaultValue); - - // Define the "get" accessor method for DynamicPropertyName - MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAttr, returnType, paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnType, null); + property.SetConstant(defaultValue); + + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, returnType, paramTypes); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ret); + + property.SetGetMethod(method); - // Map the 'get' method created above to our PropertyBuilder - myPropertyBuilder.SetGetMethod(myMethodBuilder); - - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - - PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, bindingAttr); - return myProperty.GetConstantValue(); + Type createdType = type.CreateTypeInfo().AsType(); + PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingAttributes); + Assert.Equal(defaultValue, createdProperty.GetConstantValue()); } [Fact] - public void NegTest1() + public void SetConstant_TypeAlreadyCreated_ThrowsInvalidOperationException() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - Type[] paramTypes = new Type[] - { - typeof(int) - }; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - new Type[0]); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - MethodAttributes.Public, - CallingConventions.HasThis, - typeof(int), - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), new Type[0]); + MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public, CallingConventions.HasThis, typeof(int), new Type[] { typeof(int) }); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ldarg_1); methodILGenerator.Emit(OpCodes.Ret); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.SetConstant(1); }); - } - [Fact] - public void PosTest18() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - Type returnType = typeof(object); - Type[] paramTypes = new Type[0]; - object defaultValue = "TestCase"; - object actualValue; - - actualValue = ExecutePosTest( - defaultValue, - getMethodAttr, - returnType, - paramTypes, - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); + type.CreateTypeInfo().AsType(); + Assert.Throws(() => property.SetConstant(1)); } [Fact] - public void NegTest3() + public void SetConstant_TypeNotConstant_ThrowsArgumentException() { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - Type returnType = typeof(decimal); - Type[] paramTypes = new Type[0]; - decimal defaultValue = (decimal)_generator.GetSingle(); - object actualValue; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); - Assert.Throws(() => - { - actualValue = ExecutePosTest(defaultValue, getMethodAttr, returnType, paramTypes, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); - }); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(decimal), null); + Assert.Throws(null, () => property.SetConstant((decimal)10)); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute.cs new file mode 100644 index 0000000..747337a --- /dev/null +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute.cs @@ -0,0 +1,144 @@ +// 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 PropertyBuilderTest12 + { + [Fact] + public void SetCustomAttribute() + { + Type returnType = typeof(int); + Type[] ctorParamTypes = new Type[] { typeof(int) }; + + int expectedValue = 10; + object[] ctorParamValues = new object[] { expectedValue }; + CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes), ctorParamValues); + + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnType, null); + property.SetCustomAttribute(customAttrBuilder); + + MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, returnType, new Type[0]); + + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldarg_0); + methodILGenerator.Emit(OpCodes.Ret); + + property.SetGetMethod(method); + + BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static; + Type createdType = type.CreateTypeInfo().AsType(); + PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingFlags); + object[] attributes = createdProperty.GetCustomAttributes(false).ToArray(); + + Assert.Equal(1, attributes.Length); + Assert.True(attributes[0] is IntPropertyAttribute); + Assert.Equal(expectedValue, (attributes[0] as IntPropertyAttribute).Value); + } + + [Fact] + public void SetCustomAttribute_CustomAttributeBuilder_NullBuilder_ThrowsArgumentNullException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); + + Assert.Throws("customBuilder", () => property.SetCustomAttribute(null)); + } + + [Fact] + public void SetCustomAttribute_CustomAttributeBuilder_TypeNotCreated_ThrowsInvalidOperationException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); + + Type[] ctorParamTypes = new Type[] { typeof(int) }; + object[] ctorParamValues = new object[] { 10 }; + CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes), ctorParamValues); + + type.CreateTypeInfo().AsType(); + Assert.Throws(() => property.SetCustomAttribute(customAttrBuilder)); + } + + [Fact] + public void SetCustomAttribute_ConstructorInfo_ByteArray() + { + Type returnType = typeof(int); + Type[] ctorParamTypes = new Type[] { typeof(int) }; + + int expectedValue = 10; + byte[] binaryAttribute = new byte[6]; + ConstructorInfo con = typeof(IntPropertyAttribute).GetConstructor(ctorParamTypes); + binaryAttribute[0] = 01; + binaryAttribute[1] = 00; + for (int i = 0; i < binaryAttribute.Length - 2; ++i) + { + binaryAttribute[i + 2] = (byte)(expectedValue >> (8 * i) & 0xff); + } + + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, returnType, null); + property.SetCustomAttribute(con, binaryAttribute); + + MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, returnType, new Type[0]); + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldarg_0); + methodILGenerator.Emit(OpCodes.Ret); + + property.SetGetMethod(method); + + BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static; + Type createdType = type.CreateTypeInfo().AsType(); + PropertyInfo createdProperty = createdType.GetProperty("TestProperty", bindingFlags); + object[] attributes = createdProperty.GetCustomAttributes(false).ToArray(); + + Assert.Equal(1, attributes.Length); + Assert.True(attributes[0] is IntPropertyAttribute); + Assert.Equal(expectedValue, (attributes[0] as IntPropertyAttribute).Value); + } + + [Fact] + public void SetCustomAttribute_ConstructorInfo_ByteArray_NullConstructorInfo_ThrowsArgumentNullException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); + + Assert.Throws("con", () => property.SetCustomAttribute(null, new byte[6])); + } + + + [Fact] + public void SetCustomAttribute_ConstructorInfo_ByteArray_TypeAlreadyCreated_ThrowsInvalidOperationException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.HasDefault, typeof(int), null); + + ConstructorInfo con = typeof(IntPropertyAttribute).GetConstructor(new Type[] { typeof(int) }); + + type.CreateTypeInfo().AsType(); + Assert.Throws(() => property.SetCustomAttribute(con, new byte[6])); + } + } + + [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] + public class IntPropertyAttribute : Attribute + { + private int _value; + public int Value { get { return _value; } } + + public IntPropertyAttribute(int value) + { + _value = value; + } + + public IntPropertyAttribute() : this(0) { } + } +} diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute1.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute1.cs deleted file mode 100644 index 78094a4..0000000 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute1.cs +++ /dev/null @@ -1,169 +0,0 @@ -// 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.Linq; -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class PropertyBuilderTest12 - { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestSetCustomAttribute() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(int); - Type[] ctorParamTypes = new Type[] { typeof(int) }; - int expectedValue = _generator.GetInt32(); - object[] ctorParamValues = new object[] { expectedValue }; - CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder( - typeof(PBMyAttribute).GetConstructor(ctorParamTypes), - ctorParamValues); - object[] actualCustomAttrs; - - actualCustomAttrs = ExecutePosTest( - customAttrBuilder, - getMethodAttr, - returnType, - new Type[0], - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.Equal(1, actualCustomAttrs.Length); - Assert.True(actualCustomAttrs[0] is PBMyAttribute); - Assert.Equal(expectedValue, (actualCustomAttrs[0] as PBMyAttribute).Value); - } - - private object[] ExecutePosTest( - CustomAttributeBuilder customAttrBuilder, - MethodAttributes getMethodAttr, - Type returnType, - Type[] paramTypes, - BindingFlags bindingAttr) - { - TypeBuilder myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - - PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.HasDefault, - returnType, null); - myPropertyBuilder.SetCustomAttribute(customAttrBuilder); - // Define the "get" accessor method for DynamicPropertyName - MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAttr, returnType, paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ret); - - // Map the 'get' method created above to our PropertyBuilder - myPropertyBuilder.SetGetMethod(myMethodBuilder); - - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - - PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, bindingAttr); - return myProperty.GetCustomAttributes(false).Select(a => (object)a).ToArray(); - } - - [Fact] - public void TestThrowsExceptionOnNullBuilder() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - Type returnType = typeof(int); - Type[] paramTypes = new Type[0]; - - Assert.Throws(() => - { - ExecutePosTest(null as CustomAttributeBuilder, getMethodAttr, returnType, paramTypes, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); - }); - } - - [Fact] - public void TestThrowsExceptionForCreateTypeCalled() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - Type returnType = typeof(int); - Type[] paramTypes = new Type[0]; - - Type[] ctorParamTypes = new Type[] { typeof(int) }; - int expectedValue = _generator.GetInt32(); - object[] ctorParamValues = new object[] { expectedValue }; - CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder( - typeof(PBMyAttribute).GetConstructor(ctorParamTypes), - ctorParamValues); - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - returnType, - paramTypes); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAttr, - typeof(int), - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldarg_1); - methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.SetGetMethod(myMethodBuilder); - myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.SetCustomAttribute(customAttrBuilder); }); - } - } - - [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] - public class PBMyAttribute : Attribute - { - private int _value; - - public int Value - { - get { return _value; } - } - - public PBMyAttribute(int value) - { - _value = value; - } - - public PBMyAttribute() : this(0) - { } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute2.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute2.cs deleted file mode 100644 index 74eedfd..0000000 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetCustomAttribute2.cs +++ /dev/null @@ -1,173 +0,0 @@ -// 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.Linq; -using System.Reflection; -using System.Reflection.Emit; -using System.Threading; -using Xunit; - -namespace System.Reflection.Emit.Tests -{ - public class PropertyBuilderTest13 - { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - private readonly RandomDataGenerator _generator = new RandomDataGenerator(); - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestSetCustomAttribute() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(int); - Type[] ctorParamTypes = new Type[] { typeof(int) }; - int expectedValue = _generator.GetInt32(); - byte[] binaryAttr = new byte[6]; - object[] actualCustomAttrs; - ConstructorInfo con = typeof(PBMyAttribute2).GetConstructor(ctorParamTypes); - binaryAttr[0] = 01; - binaryAttr[1] = 00; - for (int i = 0; i < binaryAttr.Length - 2; ++i) - { - binaryAttr[i + 2] = (byte)(expectedValue >> (8 * i) & 0xff); - } - actualCustomAttrs = ExecutePosTest( - con, - binaryAttr, - getMethodAttr, - returnType, - new Type[0], - BindingFlags.Public | - BindingFlags.Instance | - BindingFlags.NonPublic | - BindingFlags.Static); - Assert.Equal(1, actualCustomAttrs.Length); - Assert.True(actualCustomAttrs[0] is PBMyAttribute2); - Assert.Equal(expectedValue, (actualCustomAttrs[0] as PBMyAttribute2).Value); - } - - private object[] ExecutePosTest( - ConstructorInfo con, - byte[] binaryAttribute, - MethodAttributes getMethodAttr, - Type returnType, - Type[] paramTypes, - BindingFlags bindingAttr) - { - TypeBuilder myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - - PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.HasDefault, - returnType, null); - myPropertyBuilder.SetCustomAttribute(con, binaryAttribute); - // Define the "get" accessor method for DynamicPropertyName - MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAttr, returnType, paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ret); - - // Map the 'get' method created above to our PropertyBuilder - myPropertyBuilder.SetGetMethod(myMethodBuilder); - - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - - PropertyInfo myProperty = myType.GetProperty(DynamicPropertyName, bindingAttr); - return myProperty.GetCustomAttributes(false).Select(a => (object)a).ToArray(); - } - - [Fact] - public void TestThrowsExceptionOnNullConstructorInfo() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - Type returnType = typeof(int); - Type[] paramTypes = new Type[0]; - - Assert.Throws(() => - { - ExecutePosTest(null as ConstructorInfo, new byte[] { 01, 00, 01, 02, 03, 04 }, getMethodAttr, returnType, paramTypes, BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); - }); - } - - - [Fact] - public void TestThrowsExceptionOnCreateTypeCalled() - { - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - Type returnType = typeof(int); - Type[] ctorParamTypes = new Type[] { typeof(int) }; - Type[] paramTypes = new Type[0]; - int expectedValue = _generator.GetInt32(); - byte[] binaryAttr = new byte[6]; - ConstructorInfo con = typeof(PBMyAttribute2).GetConstructor(ctorParamTypes); - binaryAttr[0] = 01; - binaryAttr[1] = 00; - for (int i = 0; i < binaryAttr.Length - 2; ++i) - { - binaryAttr[i + 2] = (byte)(expectedValue >> (8 * i) & 0xff); - } - TypeBuilder myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - PropertyBuilder myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - returnType, - paramTypes); - MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAttr, - typeof(int), - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldarg_1); - methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.SetGetMethod(myMethodBuilder); - myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.SetCustomAttribute(con, binaryAttr); }); - } - } - - [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] - public class PBMyAttribute2 : Attribute - { - private int _value; - - public int Value - { - get { return _value; } - } - - public PBMyAttribute2(int value) - { - _value = value; - } - - public PBMyAttribute2() : this(0) - { } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetGetMethod.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetGetMethod.cs index 45e3958..1b5aff1 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetGetMethod.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetGetMethod.cs @@ -2,248 +2,60 @@ // 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 PropertyBuilderTest14 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestForPrivateSetAccessor() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.Private | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); - methodILGenerator.Emit(OpCodes.Ret); - - myPropertyBuilder.SetGetMethod(myMethodBuilder); - MethodInfo actualMethod = myPropertyBuilder.GetGetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); - } - - [Fact] - public void TestForProtectedGetAccessor() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.Family | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); - methodILGenerator.Emit(OpCodes.Ret); - - myPropertyBuilder.SetGetMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - MethodInfo actualMethod = myPropertyBuilder.GetGetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); - } - - [Fact] - public void TestForInternalGetAccessor() + [Theory] + [InlineData(MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.Family | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.FamORAssem | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + public void SetGetMethod(MethodAttributes methodAttributes) { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.FamORAssem | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + MethodBuilder method = type.DefineMethod("TestMethod", methodAttributes, typeof(int), null); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); + methodILGenerator.Emit(OpCodes.Ldfld, field); methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.SetGetMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - MethodInfo actualMethod = myPropertyBuilder.GetGetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); + property.SetGetMethod(method); + MethodInfo actualMethod = property.GetGetMethod(true); + Assert.Equal(method.Name, actualMethod.Name); } - + [Fact] - public void TestForPublicStaticGetAccessor() + public void SetGetMethod_NullMethodBuilder_ThrowsArgumentNullException() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - bool nonPublic = false; - MethodAttributes getMethodAtt = MethodAttributes.Static | - MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); - methodILGenerator.Emit(OpCodes.Ret); - - myPropertyBuilder.SetGetMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - MethodInfo actualMethod = myPropertyBuilder.GetGetMethod(nonPublic); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + Assert.Throws("mdBuilder", () => property.SetGetMethod(null)); } [Fact] - public void TestForPublicInstanceGetAccessor() + public void SetGetMethod_TypeAlreadyCreated_ThrowsInvalidOperationException() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldfld, field); methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.SetGetMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - MethodInfo actualMethod = myPropertyBuilder.GetGetMethod(false); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); - } - - [Fact] - public void TestThrowsExceptionForNullBuilder() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder = null; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - Assert.Throws(() => { myPropertyBuilder.SetGetMethod(myMethodBuilder); }); - } - - [Fact] - public void TestThrowsExceptionForCreateTypeCalled() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes getMethodAtt = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAtt, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); - methodILGenerator.Emit(OpCodes.Ret); - myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.SetGetMethod(myMethodBuilder); }); + type.CreateTypeInfo().AsType(); + Assert.Throws(() => property.SetGetMethod(method)); } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetSetMethod.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetSetMethod.cs index 2c81d41..f2b4d8e 100644 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetSetMethod.cs +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetSetMethod.cs @@ -2,251 +2,63 @@ // 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 PropertyBuilderTest15 { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestForPrivateSetAccessor() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Private | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }, - myFieldBuilder); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - MethodInfo actualMethod = myPropertyBuilder.GetSetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); - } - - [Fact] - public void TestForProtectedSetAccessor() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Family | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }, - myFieldBuilder); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - MethodInfo actualMethod = myPropertyBuilder.GetSetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); - } - - [Fact] - public void TestForInternalSetAccessor() + [Theory] + [InlineData(MethodAttributes.Private | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.Family | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.FamORAssem | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.Static | MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + [InlineData(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig)] + public void SetSetMethod(MethodAttributes methodAttributes) { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.FamORAssem | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }, - myFieldBuilder); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - MethodInfo actualMethod = myPropertyBuilder.GetSetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + MethodBuilder method = ImplementMethod( type, "TestMethod", methodAttributes, null, new Type[] { typeof(int) }, field); + + property.SetSetMethod(method); + MethodInfo actualMethod = property.GetSetMethod(true); + Assert.Equal(method.Name, actualMethod.Name); } - + [Fact] - public void TestForPublicStaticSetAccessor() + public void SetSetMethod_NullMethodBuilder_ThrowsArgumentNullException() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Static | - MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }, - myFieldBuilder); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - MethodInfo actualMethod = myPropertyBuilder.GetSetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + Assert.Throws("mdBuilder", () => property.SetSetMethod(null)); } [Fact] - public void TestForPublicInstanceSetAccessor() + public void TestThrowsExceptionForCreateTypeCalled() { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }, - myFieldBuilder); - myPropertyBuilder.SetSetMethod(myMethodBuilder); - MethodInfo actualMethod = myPropertyBuilder.GetSetMethod(true); - Assert.Equal(myMethodBuilder.Name, actualMethod.Name); - } + MethodAttributes setMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = ImplementMethod(type, "TestMethod", setMethodAttributes, null, new Type[] { typeof(int) }, field); - [Fact] - public void TestThrowsExceptionForNullMethodBuilder() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder = null; - - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - Assert.Throws(() => { myPropertyBuilder.SetSetMethod(myMethodBuilder); }); + type.CreateTypeInfo().AsType(); + Assert.Throws(() => { property.SetSetMethod(method); }); } - [Fact] - public void TestThrowsExceptionForCreateTypeCalled() + private MethodBuilder ImplementMethod(TypeBuilder type, string methodName, MethodAttributes methodAttr, Type returnType, Type[] paramTypes, FieldBuilder field) { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - MethodAttributes setMethodAtt = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod(methodName, methodAttr, returnType, paramTypes); - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - FieldBuilder myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = ImplementMethod( - myTypeBuilder, - DynamicMethodName, - setMethodAtt, - null, - new Type[] { typeof(int) }, - myFieldBuilder); - myTypeBuilder.CreateTypeInfo().AsType(); - Assert.Throws(() => { myPropertyBuilder.SetSetMethod(myMethodBuilder); }); - } - - private MethodBuilder ImplementMethod( - TypeBuilder myTypeBuilder, - string methodName, - MethodAttributes methodAttr, - Type returnType, - Type[] paramTypes, - FieldBuilder myFieldBuilder) - { - MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod( - methodName, - methodAttr, - returnType, - paramTypes); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); + ILGenerator methodILGenerator = method.GetILGenerator(); methodILGenerator.Emit(OpCodes.Ldarg_0); methodILGenerator.Emit(OpCodes.Ldarg_1); - methodILGenerator.Emit(OpCodes.Stfld, myFieldBuilder); + methodILGenerator.Emit(OpCodes.Stfld, field); methodILGenerator.Emit(OpCodes.Ret); - return myMethodBuilder; + return method; } } } diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue.cs new file mode 100644 index 0000000..ce35cdc --- /dev/null +++ b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue.cs @@ -0,0 +1,32 @@ +// 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 PropertyBuilderTest16 + { + [Fact] + public void SetValue_ThrowsNotSupportedException() + { + TypeBuilder type = Helpers.DynamicType(TypeAttributes.Class | TypeAttributes.Public); + FieldBuilder field = type.DefineField("TestField", typeof(int), FieldAttributes.Private); + PropertyBuilder property = type.DefineProperty("TestProperty", PropertyAttributes.None, typeof(int), null); + + MethodAttributes getMethodAttributes = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.HideBySig; + MethodBuilder method = type.DefineMethod("TestMethod", getMethodAttributes, typeof(int), null); + + ILGenerator methodILGenerator = method.GetILGenerator(); + methodILGenerator.Emit(OpCodes.Ldarg_0); + methodILGenerator.Emit(OpCodes.Ldfld, field); + methodILGenerator.Emit(OpCodes.Ret); + property.SetGetMethod(method); + + Type createdType = type.CreateTypeInfo().AsType(); + object obj = createdType.GetConstructor(new Type[0]).Invoke(null); + Assert.Throws(() => property.SetValue(obj, 99, null)); + } + } +} diff --git a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue1.cs b/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue1.cs deleted file mode 100644 index 87c87da..0000000 --- a/src/libraries/System.Reflection.Emit/tests/PropertyBuilder/PropertyBuilderSetValue1.cs +++ /dev/null @@ -1,68 +0,0 @@ -// 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 PropertyBuilderTest16 - { - private const string DynamicAssemblyName = "TestDynamicAssembly"; - private const string DynamicModuleName = "TestDynamicModule"; - private const string DynamicTypeName = "TestDynamicType"; - private const string DynamicFieldName = "TestDynamicFieldA"; - private const string DynamicPropertyName = "TestDynamicProperty"; - private const string DynamicMethodName = "DynamicMethodA"; - - private TypeBuilder GetTypeBuilder(TypeAttributes typeAtt) - { - AssemblyName myAssemblyName = new AssemblyName(); - myAssemblyName.Name = DynamicAssemblyName; - AssemblyBuilder myAssemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(myAssemblyName, - AssemblyBuilderAccess.Run); - - ModuleBuilder myModuleBuilder = TestLibrary.Utilities.GetModuleBuilder(myAssemblyBuilder, - DynamicModuleName); - - return myModuleBuilder.DefineType(DynamicTypeName, typeAtt); - } - - [Fact] - public void TestThrowsExceptionForNotSupported() - { - TypeBuilder myTypeBuilder; - PropertyBuilder myPropertyBuilder; - MethodBuilder myMethodBuilder; - FieldBuilder myFieldBuilder; - MethodAttributes getMethodAttr = MethodAttributes.Public | - MethodAttributes.SpecialName | - MethodAttributes.HideBySig; - myTypeBuilder = GetTypeBuilder(TypeAttributes.Class | TypeAttributes.Public); - myFieldBuilder = myTypeBuilder.DefineField(DynamicFieldName, - typeof(int), - FieldAttributes.Private); - myPropertyBuilder = myTypeBuilder.DefineProperty(DynamicPropertyName, - PropertyAttributes.None, - typeof(int), - null); - myMethodBuilder = myTypeBuilder.DefineMethod(DynamicMethodName, - getMethodAttr, - typeof(int), - null); - ILGenerator methodILGenerator = myMethodBuilder.GetILGenerator(); - - methodILGenerator.Emit(OpCodes.Ldarg_0); - methodILGenerator.Emit(OpCodes.Ldfld, myFieldBuilder); - methodILGenerator.Emit(OpCodes.Ret); - myPropertyBuilder.SetGetMethod(myMethodBuilder); - Type myType = myTypeBuilder.CreateTypeInfo().AsType(); - object obj = myType.GetConstructor(new Type[0]).Invoke(null); - Assert.Throws(() => { myPropertyBuilder.SetValue(obj, 99, null); }); - } - } -} diff --git a/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj b/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj index 02de0b6..fba7297 100644 --- a/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj +++ b/src/libraries/System.Reflection.Emit/tests/System.Reflection.Emit.Tests.csproj @@ -103,15 +103,14 @@ - + - - + - + -- 2.7.4