c# - Blazor 编译器如何生成序列号?

标签 c# asp.net-core compilation blazor

Blazor Component Documentation建议在构建自定义 RenderTree 时使用常量序列号:

Generating the sequence numbers has lost all the useful information about where the if/else branches and loops were present in the original code. This results in a diff twice as long as before. ... In more realistic cases with complex and deeply nested structures, and especially with loops, the performance cost is more severe. Instead of immediately identifying which loop blocks or branches have been inserted or removed, the diff algorithm has to recurse deeply into the render trees and usually build far longer edit scripts because it is misinformed about how the old and new structures relate to each other.

如果集合的大小无法在编译时确定,那么 Blazor 编译器如何确定枚举元素(即循环中重复的元素)的序列值?

最佳答案

下面是 Blazor 编译器如何使用定义表元素的代码编译 .razor 文件。请注意,在循环中添加的所有元素都具有相同的序列号

protected override void BuildRenderTree(RenderTreeBuilder builder)
            {
                builder.OpenElement(0, "table");
                builder.OpenElement(1, "tbody");

                for (var row = 0; row < 3; row++)
                {
                    builder.OpenElement(2, "tr");
                    for (var col = 0; col < 3; col++)
                    {
                        builder.OpenElement(3, "td");
                        builder.AddAttribute(4, "class", "tictactoe-cell");
                        builder.CloseElement();
                    }

                    builder.CloseElement();
                }

                builder.CloseElement();
                builder.CloseElement();
            }
        }

我建议您用一些文本(“Blazor!”)定义一个简单的组件,并在另一个组件中动态实例化它,即用户输入文本框的次数。运行看看是否有效。现在转到编译器生成的 .g.cs 文件,查看编译器最初生成的内容。来告诉我们你的实验吧。

你的问题非常重要,当我们想要创建一个基于从数据库中检索的数据的菜单组件、搜索的个人资料结果列表等时,熟练掌握它是必不可少的。

希望这有助于...

关于c# - Blazor 编译器如何生成序列号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56482826/

相关文章:

gcc - 使用 GCC 4.8 快照配置 GDC 的构建

c++ - 为什么clang和GCC不使用xchg来实现std::swap?

c# - MSMQ 连接缓存安全漏洞?

c# - UWP 应用程序发布编译中的 System.TimeZoneNotFoundException

c# - 使用LINQ创建搜索栏不断崩溃

c# - 通过 TContextService 和 TContextImplementation 对 AddDbContextCheck 进行健康检查

java - 编译错误 - 在 IntelliJ IDEA 中复制类

c# 内存游戏,需要在游戏结束时添加消息

c# - 类应该同时实现 IAsyncDisposable 和 IDisposable 吗?

c# - 使用属性路由在 .NET Core 2.0 中接受 API 的多个参数