在C#4 DLR和Javascript跨pretation? [英] DLR and Javascript interpretation in c# 4?

查看:134
本文介绍了在C#4 DLR和Javascript跨pretation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用DLR从C#执行一个JavaScript code。

I want to execute a javascript code from c# using DLR.

所以我写了使用简单的code C# Jint

So I wrote a simple code using c# and Jint :

  var script = @"
                               function show( )
                               {
                                      return  parseInt('123asd'); //in js it's 123
                               };
                               return show();";

                var result = new JintEngine().Run(script);
                Console.WriteLine(result);

parseInt函数('123asd')在JavaScript是: 123

parseInt('123asd') in javascript is : 123

但结果我得到的是:

  • 也许我看不到全貌,但如果在世界的另一端程序员送我自己的脚本文件,我(和他)希望得到的结果是一致的!

  • Maybe I don't see the whole picture, but if a programmer on the other side of the world sends me his script file, I (and him) expect the result to be consistent!

如果我错了,在我会用什么方案上的.Net运行的其他code? (我将不得不非常非常可疑每行的的code ...)

If I was mistaken, in what scenario would I use running other code on .Net? (I will have to be very very suspicious for every line of code...)

推荐答案

这似乎是目前的行为Jint的 partseInt 的功能。

This seems to be current behavior of Jint's partseInt function.

相关code:

// most common case
double result;
if(double.TryParse(number,NumberStyles.Any, CultureInfo.InvariantCulture, out result)) {
    // parseInt(12.42) == 42
    return NumberClass.New(sign * Math.Floor(result));
}
else {
    return this["NaN"];
}

.NET的 double.TryParse 将无法解析123asd 123 ,但返回它不能分析整个字符串。

.NET's double.TryParse won't parse '123asd' as 123, but returns that it cannot parse the whole string.

您应该填补Jint bug报告。

You should fill in bug report in Jint.

E:这是问题的标准。你永远不能得到在所有实施的标准100%兼容。总是会有差异。只要看看IE VS Chrome浏览器火狐VS VS歌剧。所有这些都使用不同的JavaScript引擎,所以你可以期待的差异。如果你真的想成为跨平台兼容性,您应按照标准执行,并把所有的不规范行为,如错误的执行。它是由你,如果你尝试解决这个错误或报告,并希望它是固定的。

E: This is problem with "standards". You can never get standards 100% compatible across all implementations. There will always be differences. Just look at IE vs Chrome vs Firefox vs Opera. All of those use different javascript engines, so you can expect differences. If you really want to be cross-compatible, you should implement according to standard and treat all non-standard behavior as bug in implementation. It is up to you if you try to work around this bug or report it and expect it to be fixed.

这篇关于在C#4 DLR和Javascript跨pretation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆