Tag: visitor

在C#中使用带有generics的访问者模式

我想知道以下是否是访客模式的可接受用途。 从Accept()或Visit()调用返回时我感到有点不舒服 – 这是否适合使用此模式,如果没有,为什么不呢? 注意:对于长代码示例的道歉,似乎有必要了解我正在做的事情,因为访问者似乎总是有点参与…… interface IAnimalElement { T Accept(IAnimalVisitor visitor); } interface IAnimalVisitor { T Visit(Lion lion); T Visit(Peacock peacock); T VisitZoo(List animals); } abstract class Animal { public int Age { get; protected set; } } class Lion : Animal, IAnimalElement { public Lion(int age) { Age = age; } public int Accept(IAnimalVisitor […]

使用ANTLR构建自己的C#编译器:编译单元

//创建一个扫描程序,从传递给我们的输入流中读取CSLexer lexer = new CSLexer(new ANTLRFileStream(f)); tokens.TokenSource = lexer; // Create a parser that reads from the scanner CSParser parser = new CSParser(tokens); // start parsing at the compilationUnit rule CSParser.compilation_unit_return x = parser.compilation_unit(); object ast = x.Tree; 我怎么能用compilation_unit_return类型的x来提取它的根,它的类,它的方法等? 我必须提取其适配器吗? 我怎么做? 请注意,compilation_unit_return在我的CSParser中定义(由ANTLR自动生成): public class compilation_unit_return : ParserRuleReturnScope { private object tree; override public object Tree […]