.NET 4.x Внутри программы на C# выполнить JS код и передать результаты в основной код C#

Узнай цену своей работы

Формулировка задачи:

Добрый день. Требуется внутри программы на C# выполнить JS код и передать результаты в основной код C# , где с ними будут производиться дальнейшие манипуляции. Подскажите, как это можно реализовать?
Листинг программы
  1. var now = new Date();
  2. var then = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
  3. then += ' '
  4. if (now.getHours() < 10) {
  5. then += '0' + now.getHours();
  6. }
  7. else {
  8. then += now.getHours();
  9. }
  10. then += ':';
  11. if (now.getMinutes() < 10) {
  12. then += '0' + now.getMinutes();
  13. }
  14. else {
  15. then += now.getMinutes();
  16. }
  17. console.Print(message + ' time(' + then + ')');
  18. console.Print(message + ' toffset(' + temp + ')');
  19. var L = navigator.plugins.length;
  20. var T = L;
  21. for (var i = 0; i < L; i++)
  22. {
  23. T = T + ' ' + navigator.plugins[i].name + ' ' + navigator.plugins[i].filename
  24. }
  25. console.Print(message + ' tplugc(' + L + ')');
  26. console.Print(message + ' tplug(' + T + ')');
  27. console.Print(message + ' tbuild(' + navigator.productSub + ')');
  28. var canvas = document.createElement('canvas');
  29. var ctx = canvas.getContext('2d');
  30. var txt = 'http://www.plentyoffish.com';
  31. ctx.textBaseline = 'top';
  32. ctx.font = '14px 'Arial'';
  33. ctx.textBaseline = 'alphabetic';
  34. ctx.fillStyle = '#f60';
  35. ctx.fillRect(125, 1, 62, 20);
  36. ctx.fillStyle = '#069';
  37. ctx.fillText(txt, 2, 15);
  38. ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
  39. ctx.fillText(txt, 4, 17);
  40. console.Print(message + ' tcrc(' + canvas.toDataURL() + ')');
Использовал так на C#
Листинг программы
  1. using Noesis.Javascript;
  2. ....
  3. using (JavascriptContext context = new JavascriptContext())
  4. { // Setting external parameters for the context
  5. context.SetParameter("console", new SystemConsole());
  6. context.SetParameter("message", "Hello World !");
  7. context.SetParameter("number", 1);
  8. // Script
  9. String script = @"
  10. var now = new Date();
  11. var then = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
  12. then += ' '
  13. if (now.getHours() < 10) {
  14. then += '0' + now.getHours();
  15. }
  16. else {
  17. then += now.getHours();
  18. }
  19. then += ':';
  20. if (now.getMinutes() < 10) {
  21. then += '0' + now.getMinutes();
  22. }
  23. else {
  24. then += now.getMinutes();
  25. }
  26. console.Print(message + ' time(' + then + ')');
  27. console.Print(message + ' toffset(' + temp + ')');
  28. var L = navigator.plugins.length;
  29. var T = L;
  30. for (var i = 0; i < L; i++)
  31. {
  32. T = T + ' ' + navigator.plugins[i].name + ' ' + navigator.plugins[i].filename
  33. }
  34. console.Print(message + ' tplugc(' + L + ')');
  35. console.Print(message + ' tplug(' + T + ')');
  36. console.Print(message + ' tbuild(' + navigator.productSub + ')');
  37. var canvas = document.createElement('canvas');
  38. var ctx = canvas.getContext('2d');
  39. var txt = 'http://www.plentyoffish.com';
  40. ctx.textBaseline = 'top';
  41. ctx.font = '14px 'Arial'';
  42. ctx.textBaseline = 'alphabetic';
  43. ctx.fillStyle = '#f60';
  44. ctx.fillRect(125, 1, 62, 20);
  45. ctx.fillStyle = '#069';
  46. ctx.fillText(txt, 2, 15);
  47. ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
  48. ctx.fillText(txt, 4, 17);
  49. console.Print(message + ' tcrc(' + canvas.toDataURL() + ')');
  50. ";
  51. // Running the script
  52. context.Run(script);
  53. // Getting a parameter
  54. Console.WriteLine("number: " + context.GetParameter("number"));
  55. }
Но получаю такой ответ Серьезность Код Описание Проект Файл Строка Состояние подавления Ошибка CS0246 Не удалось найти тип или имя пространства имен "SystemConsole" (возможно, отсутствует директива using или ссылка на сборку). WindowsFormsApp2 c:\users\kurgan\documents\visual studio 2017\Projects\WindowsFormsApp2\WindowsFormsApp2\Form1.cs 56 Активный

Решение задачи: «.NET 4.x Внутри программы на C# выполнить JS код и передать результаты в основной код C#»

textual
Листинг программы
  1. var txt = 'http://www.plentyoffish.com';
  2. ctx.textBaseline = 'top';
  3. ctx.font = '14px 'Arial'';
  4. ctx.textBaseline = 'alphabetic';
  5. ctx.fillStyle = '#f60';
  6. ctx.fillRect(125, 1, 62, 20);
  7. ctx.fillStyle = '#069';
  8. ctx.fillText(txt, 2, 15);
  9. ctx.fillStyle = 'rgba(102, 204, 0, 0.7)';
  10. ctx.fillText(txt, 4, 17);
  11. //tcrc'+canvas.toDataURL()';'

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

9   голосов , оценка 4.222 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы