0

With TransactSql.ScriptDom, it is possible to view object model of an SQL statement by extending the TSqlFragmentVisitor class. For each statement type, there is a separate Visit method which can be overridden. But I want to put exactly the same code into each Visit, of each type. I need something like a generic Visit, for all kinds of Statements. How can I do that?

using Microsoft.SqlServer.TransactSql.ScriptDom;
using System.Reflection;
public class CustomVisitor: TSqlFragmentVisitor
{
    private void DoSomething(dynamic obj)
    {
        foreach (var property in obj.GetType().
            GetProperties(BindingFlags.Public | BindingFlags.Instance))
        {
            // Recursively analyse object model to find specific objects
        }
    }

    // Create table
    public override void Visit(CreateTableStatement node)
    {
        DoSomething(node);
        base.Visit(node);
    }

    // Create view
    public override void Visit(CreateViewStatement node)
    {
        DoSomething(node);
        base.Visit(node);
    }

    // ...
    // Huge number of Visit for different types of statement
}
4

1 回答 1

0

我确实找到了适用于各种语句的通用方法:Visit(TSqlStatement node)。代码现在如下所示:

    using Microsoft.SqlServer.TransactSql.ScriptDom;
    using System.Reflection;
    public class CustomVisitor : TSqlFragmentVisitor
    {
        private void DoSomething(dynamic obj)
        {
            foreach (var property in obj.GetType().
                GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                // Recursively analyse object model to find specific objects
            }
        }

        // Generic statement
        public override void Visit(TSqlStatement node)
        {
            DoSomething(node);
            base.Visit(node);
        }
    }
// Generic statement
public override void Visit(TSqlStatement node)
{
    DoSomething(node);
    base.Visit(node);
}

}

于 2016-02-22T11:52:32.653 回答