From 69785904cf71b6510d83cf0011b07818072deefe Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Mon, 8 Jul 2019 20:01:40 -0700 Subject: [PATCH] Blazor API review changes: CompilerServices (#11911) * Blazor API review changes: CompilerServices Fixes: #11907 These are the changes from the compiler-related API review that are safe to make (these methods are not used). I'll follow up with `BindMethods.GetValue` separately because that's used by the compiler. --- .../test/RenderingRazorIntegrationTest.cs | 31 - ...ft.AspNetCore.Components.netstandard2.0.cs | 41 +- src/Components/Components/src/BindMethods.cs | 62 -- .../src/CompilerServices/RuntimeHelpers.cs | 20 + .../Microsoft.AspNetCore.Components.csproj | 2 +- .../src/RenderTree/RenderTreeBuilder.cs | 42 -- .../Components/src/RuntimeHelpers.cs | 3 - .../UIEventArgsRenderTreeBuilderExtensions.cs | 547 ------------------ .../Components/test/RenderTreeBuilderTest.cs | 6 +- .../Components/test/RendererTest.cs | 10 +- 10 files changed, 36 insertions(+), 728 deletions(-) create mode 100644 src/Components/Components/src/CompilerServices/RuntimeHelpers.cs delete mode 100644 src/Components/Components/src/UIEventArgsRenderTreeBuilderExtensions.cs diff --git a/src/Components/Blazor/Build/test/RenderingRazorIntegrationTest.cs b/src/Components/Blazor/Build/test/RenderingRazorIntegrationTest.cs index af4bffa202..034e2b0c76 100644 --- a/src/Components/Blazor/Build/test/RenderingRazorIntegrationTest.cs +++ b/src/Components/Blazor/Build/test/RenderingRazorIntegrationTest.cs @@ -290,37 +290,6 @@ namespace Microsoft.AspNetCore.Blazor.Build.Test frame => AssertFrame.Attribute(frame, "data-def", "Expression value", 2)); } - [Fact] - public void SupportsAttributesWithEventHandlerValues() - { - // Arrange/Act - var component = CompileToComponent( - @" - @code { - public bool HandlerWasCalled { get; set; } = false; - - void MyHandleEvent(Microsoft.AspNetCore.Components.UIEventArgs eventArgs) - { - HandlerWasCalled = true; - } - }"); - var handlerWasCalledProperty = component.GetType().GetProperty("HandlerWasCalled"); - - // Assert - Assert.False((bool)handlerWasCalledProperty.GetValue(component)); - Assert.Collection(GetRenderTree(component), - frame => AssertFrame.Element(frame, "elem", 2, 0), - frame => - { - Assert.Equal(RenderTreeFrameType.Attribute, frame.FrameType); - Assert.Equal(1, frame.Sequence); - Assert.NotNull(frame.AttributeValue); - - ((Action)frame.AttributeValue)(null); - Assert.True((bool)handlerWasCalledProperty.GetValue(component)); - }); - } - [Fact] public void SupportsUsingStatements() { diff --git a/src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs b/src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs index c44fe63724..65a1782fd4 100644 --- a/src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs +++ b/src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs @@ -45,13 +45,6 @@ namespace Microsoft.AspNetCore.Components } public static partial class BindMethods { - public static Microsoft.AspNetCore.Components.EventCallback GetEventHandlerValue(Microsoft.AspNetCore.Components.EventCallback value) where T : Microsoft.AspNetCore.Components.UIEventArgs { throw null; } - public static Microsoft.AspNetCore.Components.EventCallback GetEventHandlerValue(Microsoft.AspNetCore.Components.EventCallback value) where T : Microsoft.AspNetCore.Components.UIEventArgs { throw null; } - public static System.MulticastDelegate GetEventHandlerValue(System.Action value) where T : Microsoft.AspNetCore.Components.UIEventArgs { throw null; } - public static System.MulticastDelegate GetEventHandlerValue(System.Action value) where T : Microsoft.AspNetCore.Components.UIEventArgs { throw null; } - public static System.MulticastDelegate GetEventHandlerValue(System.Func value) where T : Microsoft.AspNetCore.Components.UIEventArgs { throw null; } - public static System.MulticastDelegate GetEventHandlerValue(System.Func value) where T : Microsoft.AspNetCore.Components.UIEventArgs { throw null; } - public static string GetEventHandlerValue(string value) where T : Microsoft.AspNetCore.Components.UIEventArgs { throw null; } public static string GetValue(System.DateTime value, string format) { throw null; } public static T GetValue(T value) { throw null; } } @@ -442,31 +435,6 @@ namespace Microsoft.AspNetCore.Components public UIEventArgs() { } public string Type { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } } } - public static partial class UIEventArgsRenderTreeBuilderExtensions - { - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Action value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - public static void AddAttribute(this Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder, int sequence, string name, System.Func value) { } - } public partial class UIFocusEventArgs : Microsoft.AspNetCore.Components.UIEventArgs { public UIFocusEventArgs() { } @@ -566,6 +534,13 @@ namespace Microsoft.AspNetCore.Components protected void TriggerOnLocationChanged(bool isinterceptedLink) { } } } +namespace Microsoft.AspNetCore.Components.CompilerServices +{ + public static partial class RuntimeHelpers + { + public static T TypeCheck(T value) { throw null; } + } +} namespace Microsoft.AspNetCore.Components.Forms { public sealed partial class EditContext @@ -740,9 +715,7 @@ namespace Microsoft.AspNetCore.Components.RenderTree public void AddAttribute(int sequence, in Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame frame) { } public void AddAttribute(int sequence, string name, Microsoft.AspNetCore.Components.EventCallback value) { } public void AddAttribute(int sequence, string name, System.Action value) { } - public void AddAttribute(int sequence, string name, System.Action value) { } public void AddAttribute(int sequence, string name, bool value) { } - public void AddAttribute(int sequence, string name, System.Func value) { } public void AddAttribute(int sequence, string name, System.Func value) { } public void AddAttribute(int sequence, string name, System.MulticastDelegate value) { } public void AddAttribute(int sequence, string name, object value) { } diff --git a/src/Components/Components/src/BindMethods.cs b/src/Components/Components/src/BindMethods.cs index fb5b890ffb..13ca0e3fa0 100644 --- a/src/Components/Components/src/BindMethods.cs +++ b/src/Components/Components/src/BindMethods.cs @@ -3,7 +3,6 @@ using System; using System.Globalization; -using System.Threading.Tasks; namespace Microsoft.AspNetCore.Components { @@ -24,67 +23,6 @@ namespace Microsoft.AspNetCore.Components value == default ? null : (format == null ? value.ToString() : value.ToString(format)); - /// - /// Not intended to be used directly. - /// - public static string GetEventHandlerValue(string value) - where T : UIEventArgs - { - return value; - } - /// - /// Not intended to be used directly. - /// - public static MulticastDelegate GetEventHandlerValue(Action value) - where T : UIEventArgs - { - return value; - } - - /// - /// Not intended to be used directly. - /// - public static MulticastDelegate GetEventHandlerValue(Func value) - where T : UIEventArgs - { - return value; - } - - /// - /// Not intended to be used directly. - /// - public static MulticastDelegate GetEventHandlerValue(Action value) - where T : UIEventArgs - { - return value; - } - - /// - /// Not intended to be used directly. - /// - public static MulticastDelegate GetEventHandlerValue(Func value) - where T : UIEventArgs - { - return value; - } - - /// - /// Not intended to be used directly. - /// - public static EventCallback GetEventHandlerValue(EventCallback value) - where T : UIEventArgs - { - return value; - } - - /// - /// Not intended to be used directly. - /// - public static EventCallback GetEventHandlerValue(EventCallback value) - where T : UIEventArgs - { - return value; - } } } diff --git a/src/Components/Components/src/CompilerServices/RuntimeHelpers.cs b/src/Components/Components/src/CompilerServices/RuntimeHelpers.cs new file mode 100644 index 0000000000..87ff64cd91 --- /dev/null +++ b/src/Components/Components/src/CompilerServices/RuntimeHelpers.cs @@ -0,0 +1,20 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Components.CompilerServices +{ + /// + /// Used by generated code produced by the Components code generator. Not intended or supported + /// for use in application code. + /// + public static class RuntimeHelpers + { + /// + /// Not intended for use by application code. + /// + /// + /// + /// + public static T TypeCheck(T value) => value; + } +} diff --git a/src/Components/Components/src/Microsoft.AspNetCore.Components.csproj b/src/Components/Components/src/Microsoft.AspNetCore.Components.csproj index 104048d5c0..4a6ddef33d 100644 --- a/src/Components/Components/src/Microsoft.AspNetCore.Components.csproj +++ b/src/Components/Components/src/Microsoft.AspNetCore.Components.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 diff --git a/src/Components/Components/src/RenderTree/RenderTreeBuilder.cs b/src/Components/Components/src/RenderTree/RenderTreeBuilder.cs index e5773a41b2..3b48edd123 100644 --- a/src/Components/Components/src/RenderTree/RenderTreeBuilder.cs +++ b/src/Components/Components/src/RenderTree/RenderTreeBuilder.cs @@ -224,23 +224,6 @@ namespace Microsoft.AspNetCore.Components.RenderTree AddAttribute(sequence, name, (MulticastDelegate)value); } - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public void AddAttribute(int sequence, string name, Action value) - { - AddAttribute(sequence, name, (MulticastDelegate)value); - } - /// /// /// Appends a frame representing a -valued attribute. @@ -258,23 +241,6 @@ namespace Microsoft.AspNetCore.Components.RenderTree AddAttribute(sequence, name, (MulticastDelegate)value); } - /// - /// - /// Appends a frame representing a -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public void AddAttribute(int sequence, string name, Func value) - { - AddAttribute(sequence, name, (MulticastDelegate)value); - } - /// /// /// Appends a frame representing a delegate-valued attribute. @@ -287,14 +253,6 @@ namespace Microsoft.AspNetCore.Components.RenderTree /// An integer that represents the position of the instruction in the source code. /// The name of the attribute. /// The value of the attribute. - /// - /// This method is provided for infrastructure purposes, and is used to be - /// to provide support for delegates of specific - /// types. For a good programming experience when using a custom delegate type, define an - /// extension method similar to - /// - /// that calls this method. - /// public void AddAttribute(int sequence, string name, MulticastDelegate value) { AssertCanAddAttribute(); diff --git a/src/Components/Components/src/RuntimeHelpers.cs b/src/Components/Components/src/RuntimeHelpers.cs index b639ffd530..3f4fc66aa0 100644 --- a/src/Components/Components/src/RuntimeHelpers.cs +++ b/src/Components/Components/src/RuntimeHelpers.cs @@ -1,10 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Components.Forms; namespace Microsoft.AspNetCore.Components { diff --git a/src/Components/Components/src/UIEventArgsRenderTreeBuilderExtensions.cs b/src/Components/Components/src/UIEventArgsRenderTreeBuilderExtensions.cs deleted file mode 100644 index 0efb067997..0000000000 --- a/src/Components/Components/src/UIEventArgsRenderTreeBuilderExtensions.cs +++ /dev/null @@ -1,547 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Components.RenderTree; - -namespace Microsoft.AspNetCore.Components -{ - /// - /// Extensions methods on for event handlers. - /// - /// - /// - /// These methods enable method-group to delegate conversion for delegates and methods that accept - /// types derived from . - /// - /// - /// This enhances the programming experience for using event handlers with the render tree builder - /// in components written in pure C#. These extension methods make it possible to write code like: - /// - /// builder.AddAttribute(0, "onkeypress", MyKeyPressHandler); - /// - /// Where void MyKeyPressHandler(UIKeyboardEventArgs e) is a method defined in the same class. - /// In this example, the author knows that the onclick event is associated with the - /// event args type. The component author is responsible for - /// providing a delegate that matches the expected event args type, an error will result in a failure - /// at runtime. - /// - /// - /// When a component is authored in Razor (.cshtml), the Razor code generator will maintain a mapping - /// between event names and event arg types that can be used to generate more strongly typed code. - /// Generated code for the same case will look like: - /// - /// builder.AddAttribute(0, "onkeypress", BindMethods.GetEventHandlerValue<UIKeyboardEventArgs>(MyKeyPressHandler)); - /// - /// - /// - public static class UIEventArgsRenderTreeBuilderExtensions - { - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Action value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - - /// - /// - /// Appends a frame representing an -valued attribute. - /// - /// - /// The attribute is associated with the most recently added element. If the value is null and the - /// current element is not a component, the frame will be omitted. - /// - /// - /// The . - /// An integer that represents the position of the instruction in the source code. - /// The name of the attribute. - /// The value of the attribute. - public static void AddAttribute(this RenderTreeBuilder builder, int sequence, string name, Func value) - { - if (builder == null) - { - throw new ArgumentNullException(nameof(builder)); - } - - builder.AddAttribute(sequence, name, (MulticastDelegate)value); - } - } -} diff --git a/src/Components/Components/test/RenderTreeBuilderTest.cs b/src/Components/Components/test/RenderTreeBuilderTest.cs index 91c1d3b318..6047140ff4 100644 --- a/src/Components/Components/test/RenderTreeBuilderTest.cs +++ b/src/Components/Components/test/RenderTreeBuilderTest.cs @@ -399,7 +399,7 @@ namespace Microsoft.AspNetCore.Components.Test } [Fact] - public void CannotAddEventHandlerAttributeAtRoot() + public void CannotDelegateAttributeAtRoot() { // Arrange var builder = new RenderTreeBuilder(new TestRenderer()); @@ -407,7 +407,7 @@ namespace Microsoft.AspNetCore.Components.Test // Act/Assert Assert.Throws(() => { - builder.AddAttribute(0, "name", eventInfo => { }); + builder.AddAttribute(0, "name", new Action(eventInfo => { })); }); } @@ -437,7 +437,7 @@ namespace Microsoft.AspNetCore.Components.Test { builder.OpenElement(0, "some element"); builder.AddContent(1, "hello"); - builder.AddAttribute(2, "name", eventInfo => { }); + builder.AddAttribute(2, "name", new Action(eventInfo => { })); }); } diff --git a/src/Components/Components/test/RendererTest.cs b/src/Components/Components/test/RendererTest.cs index 1cd41792bf..508236c5ad 100644 --- a/src/Components/Components/test/RendererTest.cs +++ b/src/Components/Components/test/RendererTest.cs @@ -2198,12 +2198,12 @@ namespace Microsoft.AspNetCore.Components.Test { builder.AddContent(0, "Child event count: " + eventCount); builder.OpenComponent(1); - builder.AddAttribute(2, nameof(EventComponent.OnTest), args => + builder.AddAttribute(2, nameof(EventComponent.OnTest), new Action(args => { eventCount++; rootComponent.TriggerRender(); childComponent.TriggerRender(); - }); + })); builder.CloseComponent(); }); var rootComponentId = renderer.AssignRootComponentId(rootComponent); @@ -3638,7 +3638,7 @@ namespace Microsoft.AspNetCore.Components.Test => _renderHandle.Render(builder => { builder.OpenElement(0, "my button"); - builder.AddAttribute(1, "my click handler", eventArgs => OnClick(eventArgs)); + builder.AddAttribute(1, "my click handler", new Action(eventArgs => OnClick(eventArgs))); builder.CloseElement(); }); } @@ -4069,12 +4069,12 @@ namespace Microsoft.AspNetCore.Components.Test builder.OpenElement(0, "element with event"); builder.AddAttribute(1, nameof(BoundString), BoundString); - builder.AddAttribute(2, "ontestevent", (UIChangeEventArgs eventArgs) => + builder.AddAttribute(2, "ontestevent", new Action((UIChangeEventArgs eventArgs) => { BoundString = (string)eventArgs.Value; TriggerRender(); GC.KeepAlive(unrelatedThingToMakeTheLambdaCapture); - }); + })); builder.SetUpdatesAttributeName(nameof(BoundString)); builder.CloseElement(); }