XML SAX解析器,用于使用反射编写脚本

时间:2021-12-02 01:09:18

I'd like an opinion about to create an hypothetic scripting system using XML. The idea is to use a SAX parser and C# reflection.

我想要使​​用XML创建一个假设的脚本系统。我们的想法是使用SAX解析器和C#反射。

I cannot find a library/framework which allow to specify custom action using XML files. At this time I use XML for serialize application classes, bug could be awesome to specify which actions the application shall execute using XML.

我找不到允许使用XML文件指定自定义操作的库/框架。这时我使用XML来序列化应用程序类,bug可能非常棒,可以指定应用程序使用XML执行哪些操作。

So, I'm thinking about:

所以,我在考虑:

  • SAX parser implementation for C#?
  • C#的SAX解析器实现?

  • XML script conventions?
  • XML脚本约定?

What I'd like to achieve is the: - Possibility to assign a class instance property - Possibility to call class instance routines

我想要实现的是: - 分配类实例属性的可能性 - 调用类实例例程的可能性

Assuming that the class instance is implicit, the following XML script would be gold for me:

假设类实例是隐式的,以下XML脚本对我来说是金色的:

<Script>
    <Property Name='SomeProperty'>
        <Value>
            < ... >
        </Value>
    </Property>
    <RoutineCall>
        <Parameters>
            <Parameter Name='ArgName'>
                < ... >
            </Parameter>
        </Parameters>
    </RoutineCall>
</Script>

It is already done by someone? Is it possible?

它已经由某人完成了?可能吗?

2 个解决方案

#1


1  

Although not SAX parsers, MSBuild and NAnt are excellent examples of XML-based scripting platforms. Both support developing custom actions and have large communities of users who may be able to help as you go along.

虽然不是SAX解析器,但MSBuild和NAnt是基于XML的脚本平台的优秀示例。两者都支持开发自定义操作,并拥有大量用户,可以随时为您提供帮助。

If you must build this from scratch, I recommend building serializable classes implementing a simple execution interface (e.g., a Run() method, known events, etc.). You can then use the .NET XmlSerializer to deserialize objects, cast to the interface, and invoke their routines.

如果你必须从头开始构建它,我建议构建实现简单执行接口的可序列化类(例如,Run()方法,已知事件等)。然后,您可以使用.NET XmlSerializer反序列化对象,强制转换为接口并调用其例程。

#2


1  

Microsoft does not have a SAX parser for .Net. However, they do have an XmlTextReader class that you can use to read xml a node at a time. In addition to MSBuild it seems as though the xaml dialect used by Microsoft first for WPF and now in .Net 4.0 WWF might be similar to what you want to do as far as scripting. I am not sure what your requirements are for this project, if you really wanted to go all out you could use the code generation technology in .Net to compile the script to possibly get better performance.

Microsoft没有.Net的SAX解析器。但是,它们确实有一个XmlTextReader类,您可以使用它来一次读取xml节点。除了MSBuild之外,似乎Microsoft首先使用的用于WPF的xaml方言和现在用于.Net 4.0 WWF的xaml方言可能类似于您想要编写的脚本。我不确定你对这个项目的要求是什么,如果你真的想要全力以赴,你可以使用.Net中的代码生成技术来编译脚本以获得更好的性能。

#1


1  

Although not SAX parsers, MSBuild and NAnt are excellent examples of XML-based scripting platforms. Both support developing custom actions and have large communities of users who may be able to help as you go along.

虽然不是SAX解析器,但MSBuild和NAnt是基于XML的脚本平台的优秀示例。两者都支持开发自定义操作,并拥有大量用户,可以随时为您提供帮助。

If you must build this from scratch, I recommend building serializable classes implementing a simple execution interface (e.g., a Run() method, known events, etc.). You can then use the .NET XmlSerializer to deserialize objects, cast to the interface, and invoke their routines.

如果你必须从头开始构建它,我建议构建实现简单执行接口的可序列化类(例如,Run()方法,已知事件等)。然后,您可以使用.NET XmlSerializer反序列化对象,强制转换为接口并调用其例程。

#2


1  

Microsoft does not have a SAX parser for .Net. However, they do have an XmlTextReader class that you can use to read xml a node at a time. In addition to MSBuild it seems as though the xaml dialect used by Microsoft first for WPF and now in .Net 4.0 WWF might be similar to what you want to do as far as scripting. I am not sure what your requirements are for this project, if you really wanted to go all out you could use the code generation technology in .Net to compile the script to possibly get better performance.

Microsoft没有.Net的SAX解析器。但是,它们确实有一个XmlTextReader类,您可以使用它来一次读取xml节点。除了MSBuild之外,似乎Microsoft首先使用的用于WPF的xaml方言和现在用于.Net 4.0 WWF的xaml方言可能类似于您想要编写的脚本。我不确定你对这个项目的要求是什么,如果你真的想要全力以赴,你可以使用.Net中的代码生成技术来编译脚本以获得更好的性能。