forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EventBuilder implementation (dotnet#95936)
* Add EventBuilder implementation
- Loading branch information
Showing
7 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/libraries/System.Reflection.Emit/src/System/Reflection/Emit/EventBuilderImpl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection.Metadata; | ||
|
||
namespace System.Reflection.Emit | ||
{ | ||
internal sealed class EventBuilderImpl : EventBuilder | ||
{ | ||
private readonly string _name; | ||
private EventAttributes _attributes; | ||
private readonly TypeBuilderImpl _typeBuilder; | ||
private readonly Type _eventType; | ||
|
||
internal EventDefinitionHandle _handle; | ||
internal MethodBuilder? _addOnMethod; | ||
internal MethodBuilder? _raiseMethod; | ||
internal MethodBuilder? _removeMethod; | ||
internal HashSet<MethodBuilder>? _otherMethods; | ||
internal List<CustomAttributeWrapper>? _customAttributes; | ||
|
||
public EventBuilderImpl(string name, EventAttributes attributes, Type eventType, TypeBuilderImpl typeBuilder) | ||
{ | ||
_name = name; | ||
_attributes = attributes; | ||
_typeBuilder = typeBuilder; | ||
_eventType = eventType; | ||
} | ||
|
||
internal EventAttributes Attributes => _attributes; | ||
internal string Name => _name; | ||
internal Type EventType => _eventType; | ||
|
||
protected override void AddOtherMethodCore(MethodBuilder mdBuilder) | ||
{ | ||
ArgumentNullException.ThrowIfNull(mdBuilder); | ||
_typeBuilder.ThrowIfCreated(); | ||
|
||
_otherMethods ??= new HashSet<MethodBuilder>(); | ||
_otherMethods.Add(mdBuilder); | ||
} | ||
|
||
protected override void SetAddOnMethodCore(MethodBuilder mdBuilder) | ||
{ | ||
ArgumentNullException.ThrowIfNull(mdBuilder); | ||
_typeBuilder.ThrowIfCreated(); | ||
|
||
_addOnMethod = mdBuilder; | ||
} | ||
|
||
protected override void SetCustomAttributeCore(ConstructorInfo con, ReadOnlySpan<byte> binaryAttribute) | ||
{ | ||
_typeBuilder.ThrowIfCreated(); | ||
|
||
if (con.ReflectedType!.FullName == "System.Runtime.CompilerServices.SpecialNameAttribute") | ||
{ | ||
_attributes |= EventAttributes.SpecialName; | ||
return; | ||
} | ||
|
||
_customAttributes ??= new List<CustomAttributeWrapper>(); | ||
_customAttributes.Add(new CustomAttributeWrapper(con, binaryAttribute)); | ||
} | ||
|
||
protected override void SetRaiseMethodCore(MethodBuilder mdBuilder) | ||
{ | ||
ArgumentNullException.ThrowIfNull(mdBuilder); | ||
_typeBuilder.ThrowIfCreated(); | ||
|
||
_raiseMethod = mdBuilder; | ||
} | ||
|
||
protected override void SetRemoveOnMethodCore(MethodBuilder mdBuilder) | ||
{ | ||
ArgumentNullException.ThrowIfNull(mdBuilder); | ||
_typeBuilder.ThrowIfCreated(); | ||
|
||
_removeMethod = mdBuilder; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
.../System.Reflection.Emit/tests/PersistableAssemblyBuilder/AssemblySaveEventBuilderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace System.Reflection.Emit.Tests | ||
{ | ||
[ConditionalClass(typeof(PlatformDetection), nameof(PlatformDetection.IsNotBrowser))] | ||
public class AssemblySaveEventBuilderTests | ||
{ | ||
[Fact] | ||
public void DefineEventAndItsAccessors() | ||
{ | ||
using (TempFile file = TempFile.Create()) | ||
{ | ||
AssemblyBuilder ab = AssemblySaveTools.PopulateAssemblyBuilderTypeBuilderAndSaveMethod(out TypeBuilder type, out MethodInfo saveMethod); | ||
EventBuilder eventType = type.DefineEvent("TestEvent", EventAttributes.SpecialName, typeof(int)); | ||
MethodBuilder addMethod = type.DefineMethod("AddMethod", MethodAttributes.Public | MethodAttributes.SpecialName); | ||
MethodBuilder addMethod2 = type.DefineMethod("AddMethod2", MethodAttributes.Public | MethodAttributes.HideBySig, typeof(int), Type.EmptyTypes); | ||
MethodBuilder raiseMethod = type.DefineMethod("RaiseMethod", MethodAttributes.Assembly | MethodAttributes.SpecialName, typeof(int), Type.EmptyTypes); | ||
MethodBuilder removeMethod = type.DefineMethod("RemoveMethod", MethodAttributes.Public, typeof(void), Type.EmptyTypes); | ||
MethodBuilder otherMethod = type.DefineMethod("OtherMethod", MethodAttributes.Family, typeof(int), [typeof(int)]); | ||
CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor([typeof(int)]), [9]); | ||
eventType.SetCustomAttribute(customAttrBuilder); | ||
addMethod.GetILGenerator().Emit(OpCodes.Ret); | ||
ILGenerator adderIL = addMethod2.GetILGenerator(); | ||
adderIL.Emit(OpCodes.Ldc_I4_1); | ||
adderIL.Emit(OpCodes.Ret); | ||
eventType.SetAddOnMethod(addMethod); | ||
eventType.SetAddOnMethod(addMethod2); // last set wins | ||
ILGenerator raiseIL = raiseMethod.GetILGenerator(); | ||
raiseIL.Emit(OpCodes.Ldc_I4_2); | ||
raiseIL.Emit(OpCodes.Ret); | ||
eventType.SetRaiseMethod(raiseMethod); | ||
removeMethod.GetILGenerator().Emit(OpCodes.Ret); | ||
eventType.SetRemoveOnMethod(removeMethod); | ||
ILGenerator otherILGenerator = otherMethod.GetILGenerator(); | ||
otherILGenerator.Emit(OpCodes.Ldarg_1); | ||
otherILGenerator.Emit(OpCodes.Ret); | ||
eventType.AddOtherMethod(otherMethod); | ||
type.CreateType(); | ||
saveMethod.Invoke(ab, new[] { file.Path }); | ||
|
||
Assembly assemblyFromDisk = AssemblySaveTools.LoadAssemblyFromPath(file.Path); | ||
Type typeFromDisk = assemblyFromDisk.Modules.First().GetType("MyType"); | ||
EventInfo eventFromDisk = typeFromDisk.GetEvent("TestEvent"); | ||
Assert.Equal(addMethod2.Name, eventFromDisk.AddMethod.Name); | ||
Assert.Equal(raiseMethod.Name, eventFromDisk.RaiseMethod.Name); | ||
Assert.Equal(removeMethod.Name, eventFromDisk.RemoveMethod.Name); | ||
Assert.Equal(typeof(int).FullName, eventFromDisk.EventHandlerType.FullName); | ||
Assert.NotNull(typeFromDisk.GetMethod("OtherMethod", BindingFlags.NonPublic | BindingFlags.Instance)); | ||
Assert.Equal(EventAttributes.SpecialName, eventFromDisk.Attributes); | ||
IList<CustomAttributeData> caData = eventFromDisk.GetCustomAttributesData(); | ||
Assert.Equal(1, caData.Count); | ||
Assert.Equal(typeof(IntPropertyAttribute).FullName, caData[0].AttributeType.FullName); | ||
Assert.Equal(1, caData[0].ConstructorArguments.Count); | ||
Assert.Equal(9, caData[0].ConstructorArguments[0].Value); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Set_NullValue_ThrowsArgumentNullException() | ||
{ | ||
AssemblySaveTools.PopulateAssemblyBuilderTypeBuilderAndSaveMethod(out TypeBuilder type, out MethodInfo _); | ||
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(string)); | ||
|
||
AssertExtensions.Throws<ArgumentNullException>("eventtype", () => type.DefineEvent("EventTypeNull", EventAttributes.None, null)); | ||
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => eventBuilder.SetRaiseMethod(null)); | ||
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => eventBuilder.SetRemoveOnMethod(null)); | ||
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => eventBuilder.SetAddOnMethod(null)); | ||
AssertExtensions.Throws<ArgumentNullException>("mdBuilder", () => eventBuilder.AddOtherMethod(null)); | ||
AssertExtensions.Throws<ArgumentNullException>("customBuilder", () => eventBuilder.SetCustomAttribute(null)); | ||
} | ||
|
||
[Fact] | ||
public void Set_WhenTypeAlreadyCreated_ThrowsInvalidOperationException() | ||
{ | ||
AssemblySaveTools.PopulateAssemblyBuilderTypeBuilderAndSaveMethod(out TypeBuilder type, out MethodInfo _); | ||
EventBuilder eventBuilder = type.DefineEvent("TestEvent", EventAttributes.None, typeof(int)); | ||
|
||
MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.SpecialName, typeof(int), null); | ||
CustomAttributeBuilder customAttrBuilder = new CustomAttributeBuilder(typeof(IntPropertyAttribute).GetConstructor([typeof(int)]), [10]); | ||
type.CreateType(); | ||
|
||
Assert.Throws<InvalidOperationException>(() => eventBuilder.SetAddOnMethod(method)); | ||
Assert.Throws<InvalidOperationException>(() => eventBuilder.SetRaiseMethod(method)); | ||
Assert.Throws<InvalidOperationException>(() => eventBuilder.SetRemoveOnMethod(method)); | ||
Assert.Throws<InvalidOperationException>(() => eventBuilder.AddOtherMethod(method)); | ||
Assert.Throws<InvalidOperationException>(() => eventBuilder.SetCustomAttribute(customAttrBuilder)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters