zoukankan      html  css  js  c++  java
  • 使用ImpromptuInterface反射库方便的创建自定义DfaGraphWriter

    在本文中,我为创建的自定义的DfaGraphWriter实现奠定了基础。DfaGraphWriter是公开的,因此您可以如上一篇文章中所示在应用程序中使用它,但它使用的所有类均已标记为internal。这使得创建自己的版本成为问题。要解决此问题,我使用了一个开源的反射库ImpromptuInterface,使创建自定义的DfaGraphWriter实现更加容易。

    作者:依乐祝
    原文地址:https://andrewlock.net/creating-a-custom-dfagraphwriter-using-impromptuinterface-and reflection/
    译文地址:https://www.cnblogs.com/yilezhu/p/13336066.html

    我们将从查看现有的DfaGraphWriter开始,以了解其使用的internal类以及导致我们的问题。然后,我们来看一下使用一些自定义接口和ImpromptuInterface库来允许我们调用这些类。在下一篇文章中,我们将研究如何使用自定义界面创建的自定义版本DfaGraphWriter

    探索现有的 DfaGraphWriter

    DfaGraphWriter类是存在于ASP.NET Core中的一个“pubternal”文件夹中的。它已注册为单例,并使用注入的IServiceProvider来解析DfaMatcherBuilder

     public class DfaGraphWriter
    {
        private readonly IServiceProvider _services;
        public DfaGraphWriter(IServiceProvider services)
        {
            _services = services;
        }
    
        public void Write(EndpointDataSource dataSource, TextWriter writer)
        {
            // retrieve the required DfaMatcherBuilder
            var builder = _services.GetRequiredService<DfaMatcherBuilder>();
    
            // loop through the endpoints in the dataSource, and add them to the builder
            var endpoints = dataSource.Endpoints;
            for (var i = 0; i < endpoints.Count; i++)
            {
                if (endpoints[i] is RouteEndpoint endpoint && (endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>()?.SuppressMatching ?? false) == false)
                {
                    builder.AddEndpoint(endpoint);
                }
            }
    
            // Build the DfaTree. 
            // This is what we use to create the endpoint graph
            var tree = builder.BuildDfaTree(includeLabel: true);
    
            // Add the header
            writer.WriteLine("digraph DFA {");
    
            // Visit each node in the graph to create the output
            tree.Visit(WriteNode);
    
            //Close the graph
            writer.WriteLine("}");
    
            // Recursively walks the tree, writing it to the TextWriter
            void WriteNode(DfaNode node)
            {
                // Removed for brevity - we'll explore it in the next post
            }
        }
    }
    

    上面的代码显示了图形编写者Write方法的所有操作,终结如下:

    • 获取一个 DfaMatcherBuilder
    • 写入所有的端点EndpointDataSourceDfaMatcherBuilder
    • 调用DfaMatcherBuilderBuildDfaTree。这将创建一个DfaNode的 图。
    • 访问DfaNode树中的每一个,并将其写入TextWriter输出。我们将在下一篇文章中探讨这种方法。

    创建我们自己的自定义编写器的目的是通过控制如何将不同的节点写入输出来定制最后一步,因此我们可以创建更多的描述性的图形,如我先前所示:

    对端点图使用不同的样式

    我们的问题是两个重点类,DfaMatcherBuilderDfaNode,是internal所以我们不能轻易实例化它们,或者使用它们的写入方法。这给出了两个选择:

    • 重新实现这些internal类,包括它们依赖的其他任何internal类。
    • 使用反射在现有类上创建和调用方法。

    这些都不是很好的选择,但是鉴于端点图不是性能关键的东西,我决定使用反射将是最简单的。为了使事情变得更加简单,我使用了开源库ImpromptuInterface

    ImpromptuInterface使反射更容易

    ImpromptuInterface是一个库它使调用动态对象或调用存储在对象引用中的底层对象上的方法变得更加容易。它本质上增加了简单的duck/structural类型,允许您为对象使用stronlgy类型化接口。它使用Dynamic Language RuntimeReflection.Emit来实现。

    例如,让我们获取我们要使用的现有DfaMatcherBuilder类。即使我们不能直接引用它,我们仍然可以从DI容器中获取此类的实例,如下所示:

    // get the DfaMatcherBuilder type - internal, so needs reflection :(
    Type matcherBuilder = typeof(IEndpointSelectorPolicy).Assembly
        .GetType("Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilder");
    
    object rawBuilder = _services.GetRequiredService(matcherBuilder);
    

    rawBuilder是一个object引用,但它包含了一个DfaMatcherBuilder的实例。我们不能直接在调用它的方法,但是我们可以通过直接构建MethodInfo和直接调用invoke来使用反射来调用它们。。

    ImpromptuInterface通过提供一个可以直接调用方法的静态接口,使该过程更加容易。例如,对于DfaMatcherBuilder,我们只需要调用两个方法AddEndpointBuildDfaTree。原始类如下所示:

    internal class DfaMatcherBuilder : MatcherBuilder
    {
        public override void AddEndpoint(RouteEndpoint endpoint) { /* body */ }
        public DfaNode BuildDfaTree(bool includeLabel = false)
    }
    

    我们可以创建一个暴露这些方法的接口:

    public interface IDfaMatcherBuilder
    {
        void AddEndpoint(RouteEndpoint endpoint);
        object BuildDfaTree(bool includeLabel = false);
    }
    

    然后,我们可以使用ImpromptuInterface ActLike<>方法创建实现了IDfaMatcherBuilder的代理对象。此代理包装rawbuilder对象,因此当您在接口上调用方法时,它将在底层调用DfaMatcherBuilder中的等效的方法:

    使用ImpromptuInterface添加包装代理

    在代码中,如下所示:

    // An instance of DfaMatcherBuilder in an object reference
    object rawBuilder = _services.GetRequiredService(matcherBuilder);
    
    // wrap the instance in the ImpromptuInterface interface
    IDfaMatcherBuilder builder = rawBuilder.ActLike<IDfaMatcherBuilder>();
    
    // we can now call methods on the builder directly, e.g. 
    object rawTree =  builder.BuildDfaTree();
    

    原始DfaMatcherBuilder.BuildDfaTree()方法和接口版本之间有一个重要区别:原始方法返回一个DfaNode,但这是另一个internal类,因此我们无法在接口中引用它。

    相反,我们为DfaNode创建另一个ImpromptuInterface,暴露我们将需要的属性(在接下来的文章中你就会明白为什么我们需要他们):

    public interface IDfaNode
    {
        public string Label { get; set; }
        public List<Endpoint> Matches { get; }
        public IDictionary Literals { get; } // actually a Dictionary<string, DfaNode>
        public object Parameters { get; } // actually a DfaNode
        public object CatchAll { get; } // actually a DfaNode
        public IDictionary PolicyEdges { get; } // actually a Dictionary<object, DfaNode>
    }
    

    在下一篇文章中,我们将在WriteNode的方法中使用这些属性,但是有一些复杂性。在原始DfaNode类中,ParametersCatchAll属性返回DfaNode对象。在我们IDfaNode版本的属性中,我们必须返回object。我们无法引用DfaNode(因为是internal)并且我们不能返回IDfaNode,因为DfaNode 它没有实现IDfaNode,因此您不能将object引用隐式转换为IDfaNode。你必须使用ImpromptuInterface显式地添加一个实现了接口的代理,。

    例如:

    // Wrap the instance in the ImpromptuInterface interface
    IDfaMatcherBuilder builder = rawBuilder.ActLike<IDfaMatcherBuilder>();
    
    // We can now call methods on the builder directly, e.g. 
    object rawTree =  builder.BuildDfaTree();
    // Use ImpromptuInterface to add an IDfaNode wrapper
    IDfaNode tree = rawTree.ActLike<IDfaNode>();
    
    // We can now call methods and properties on the node...
    object rawParameters = tree.Parameters;
    // ...but they need to be wrapped using ImpromptuInterface too
    IDfaNode parameters = rawParameters.ActLike<IDfaNode>();
    

    返回Dictionary类型的属性还有另一个问题:LiteralsPolicyEdges。实际返回的类型分别为Dictionary<string, DfaNode>Dictionary<object, DfaNode>,但是我们需要使用一个包含该DfaNode类型的类型。不幸的是,这意味着我们不得不退回到.NET 1.1 IDictionary接口!

    您不能将一个Dictionary<string, DfaNode>强制转换为IDictionary<string, object>,因为这样做将是不安全的协方差形式

    IDictionary是一个非泛型接口,因此keyvalue仅作为object公开。对于string键,您可以直接进行转换,对于,DfaNode我们可以使用ImpromptuInterface为我们创建代理包装器:

    // Enumerate the key-value pairs as DictinoaryEntrys
    foreach (DictionaryEntry dictEntry in node.Literals)
    {
        // Cast the key value to a string directly
        var key = (string)dictEntry.Key;
        // Use ImpromptuInterface to add a wrapper
        IDfaNode value = dictEntry.Value.ActLike<IDfaNode>();
    }
    

    现在,我们已经拥有了通过实现WriteNode来创建自定义DfaWriter实现所需的一切对象,但是这篇文章已经有点长了,所以我们将在下一篇文章中探讨如何实现这一点!

    摘要

    在本文中,我探讨了DfaWriter在ASP.NET Core 中的实现以及它使用的两个internal类:DfaMatcherBuilderDfaNode。这些类是内部类的事实使得创建我们自己的DfaWriter实现非常棘手。为了干净地实现它,我们将不得不重新实现这两种类型以及它们所依赖的所有类。

    作为替代,我使用ImpromptuInterface库创建了一个包装器代理,该代理实现与被包装的对象拥有类似的方法。这使用反射来调用包装属性上的方法,但允许我们使用强类型接口。在下一篇文章中,我将展示如何使用这些包装器创建一个定制的DfaWriter来进行端点图的自定义。

  • 相关阅读:
    hdu 4521 小明系列问题——小明序列(线段树 or DP)
    hdu 1115 Lifting the Stone
    hdu 5476 Explore Track of Point(2015上海网络赛)
    Codeforces 527C Glass Carving
    hdu 4414 Finding crosses
    LA 5135 Mining Your Own Business
    uva 11324 The Largest Clique
    hdu 4288 Coder
    PowerShell随笔3 ---别名
    PowerShell随笔2---初始命令
  • 原文地址:https://www.cnblogs.com/yilezhu/p/13336066.html
Copyright © 2011-2022 走看看