引用和使用 JScript.NET“仅限函数”执行程序集

发布于 2024-09-14 23:31:45 字数 1256 浏览 3 评论 0原文

1. 从 JSC 编译程序集

我已经使用服务器端的 JScript 编译器 (jsc.exe) 编译了客户端 JavaScript,试图制作可以通过单元测试项目进行测试的内容,也许甚至可以在服务器端调试的东西。

编译后的文件仅包含函数,如下所示(仅作为示例),并且可以很好地编译为 BitField.exe。请注意,源代码中没有包装类或包。

------ BEGIN FILE (BitField.js) ----------

function BitField(){
    this.values = [];
}
// more functions ...

------- END FILE --------

jsc /fast-  /out:BitField.exe Bitfield.js

结果生成 BitField.exe 程序集。

成功!嗯,有点......


2.测试组件/接入点?

其次,我创建了一个测试项目(在 C# 中)并在 BitField.exe 程序集中成功引用。 (项目类型无关紧要,但我提供更多描述来描绘全貌。)

问题似乎是:我找不到命名空间或可以访问 BitField 内的 BitField 函数的点。我的 C# 测试项目中的 exe 程序集。集会似乎并不“正常”。

换句话说,我需要在 C# 中

using ???WHAT???

注意:我不想使用 JScript“扩展”,这意味着不会在客户端(在网络浏览器中)运行的关键字,例如 classpackage 等,因为我希望代码尽可能干净以供复制和使用。粘贴回客户端脚本环境(不管所说的“干净”代码可以通过 jsc.exe 正常编译,而不使用这些扩展)。当我尝试将函数包装在 packageclass 中时,它开始产生编译错误,因此这是不使用它们的另一个原因 - 因为它们似乎让我改变了我的代码。

关于当其中没有显式容器时如何使用已编译的 JScript 程序集的功能(通过将其引用到另一个程序集)有什么建议吗?


更新/证明

.NET Reflector 视图
替代文字

1. Compiled Assembly from JSC

I've compiled what is intended to be client-side JavaScript using the JScript compiler (jsc.exe) on the server side in an attempt to make something that can be tested from a unit testing project, and maybe even something that can be debugged on the server side.

The compiled file contains only functions as follows (just for example) and it compiles fine into BitField.exe. Notice, no wrapper class or package in the source code.

------ BEGIN FILE (BitField.js) -------

function BitField(){
    this.values = [];
}
// more functions ...

------- END FILE -------

jsc /fast-  /out:BitField.exe Bitfield.js

Results in a BitField.exe assembly.

Success! Well, kind of ....


2. Testing Assembly / Access Point?

Secondly I've created a test project (in C#) and referenced in the BitField.exe assembly successfully. (The type of project is irrelevant but I'm providing more description to paint a full picture.)

The problem seems to be: I cannot find the namespace or a point at which I can access the BitField functions inside the BitField.exe assembly from my C# test project. The assembly doesn't seem to be a "normal".

In other words I need in C#

using ???WHAT???

Note: I don't want to use JScript "extensions", meaning keywords that won't run client-side (in a web browser), for example, class, package etc because I want the code to be clean as possible for copy & paste back into client side script environment (Regardless said "clean" code compiles fine by jsc.exe without use of those extensions). When I try to wrap the functions in package and class it starts producing compile errors so that's another reason not to use them - because they appear to make me alter my code.

Any suggestions as to how I can use the functions of the compiled JScript assembly (by having it referenced into another assembly) when there are no explicit containers in it?


Update / Proof

.NET Reflector view
alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芯好空 2024-09-21 23:31:45

在玩了一段时间并尝试了 jsc.exe 命令行开关的各种组合之后,我很确定您想要做的事情不会像您希望的那样工作。如果您尝试将包含函数的 js 文件编译到 .Net 库程序集中,您会收到错误:

BitField.js(1,1) : error JS1234: Only type and package definitions are allowed inside a library

但是,还有希望!这就是我要做的...

我将保持“干净”的 BitField.js 文件原样,然后创建一个批处理文件,将其包装在 JScript 类中并将其写入“脏”js 文件。如果您将其视为将代码编译到 DLL 中的一部分,那么它非常干净。将 BitField.js 包装到 BitFieldClass.js 中的代码如下所示:

merge-into-class.js

var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1;
var inputFile = fso.OpenTextFile("BitField.js",ForReading, false);
var outputFile = fso.CreateTextFile("BitFieldClass.js", true);

outputFile.write("class BitFieldClass{\n");
while (!inputFile.AtEndOfStream)
{
    var textLine = inputFile.ReadLine();
    outputFile.write (textLine + "\n");
}
outputFile.write("}");
outputFile.close();

然后用于包装它并编译它的批处理文件非常简单:

compile- js.bat

cscript merge-into-class.js
jsc /t:library /out:BitFieldClass.dll bitFieldClass.js

当然,如果您想要处理多个文件,则必须对一些内容进行一些参数化,但希望这足以演示这个想法。

After playing around with it for a while, and trying various combinations of command-line switches for jsc.exe, I'm pretty sure that what you're trying to do won't work as you'd wish it to. If you try to compile a js file that contains functions into a .Net library assembly, you get an error:

BitField.js(1,1) : error JS1234: Only type and package definitions are allowed inside a library

But, there is hope, yet! Here's what I would do...

I would keep your "clean" BitField.js file just as it is, and then create a batch file that wraps it in a JScript class and writes it out to a "dirty" js file. It's pretty clean if you think of it as part of the compilation of the code into the DLL. The code to wrap the BitField.js into BitFieldClass.js would look like this:

merge-into-class.js

var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1;
var inputFile = fso.OpenTextFile("BitField.js",ForReading, false);
var outputFile = fso.CreateTextFile("BitFieldClass.js", true);

outputFile.write("class BitFieldClass{\n");
while (!inputFile.AtEndOfStream)
{
    var textLine = inputFile.ReadLine();
    outputFile.write (textLine + "\n");
}
outputFile.write("}");
outputFile.close();

Then the batch file to wrap it and compile it is really simple:

compile-js.bat

cscript merge-into-class.js
jsc /t:library /out:BitFieldClass.dll bitFieldClass.js

Of course, if you wanted to do multiple files, you'd have to parameterize things a bit, but hopefully this is enough to demonstrate the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文