JavaScript and C# Interpolation using Microsoft.JScript - c#

How can I add a C# object to JavaScript code?
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.JScript;
using Microsoft.JScript.Vsa;
using Microsoft.Vsa;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program program = new Program();
program.Run();
}
public object Run()
{
VsaEngine Engine = VsaEngine.CreateEngine();
object Result = null;
try
{
//Engine.PushScriptObject(new ScriptObject());
// C# object replace with ?
Result = Eval.JScriptEvaluate("var a = 1; var b = c# object", Engine);
}
catch (Exception ex)
{
return ex.Message;
}
return Result;
}
}
}

Just compile your C# object into an assembly and load that assembly in your jscript code. all the languages that .NET supports can load each other's assemblies.

You can take a look at Script#
As they say,
"Script# brings the power and productivity of C# and .NET tools to Ajax development by compiling C# source code into regular JavaScript."

Related

the type or namespace component data is missing error in using MathWorks.MATLAB.NET.ComponentData; help me how to solve this error?

i am currently deploying matlab over .net i have included my matlab project dll and mwarray.dll of matlab run-time compiler successfully but when i tried in using component data i have 2 errors one is:
name space missing using MathWorks.MATLAB.NET.ComponentData;
second one is:
cannot convert type void MathWorks.MATLAB.NET.Arrays.Mwcellarray in
line cellout = (MWCellArray)obj.braille();
complete code is as mentioned below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CsharpMatlab;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.ComponentData;
namespace MATCsharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Code For Matlab to C#");
MWCellArray cellout = null;
CsharpMatlab.CellExampleApp obj = new CsharpMatlab.CellExampleApp();
cellout = (MWCellArray)obj.braille();
MWNumericArray item1 = (MWNumericArray)cellout[1];
MWNumericArray item2 = (MWNumericArray)cellout[2];
MWCharArray item3 = (MWCharArray)cellout[3];
Console.WriteLine("item1 is {0}", item1);
Console.WriteLine("item2 is {0}", item2);
Console.WriteLine("item3 is {0}", item3);
Console.ReadLine();
}emphasized text
}
}

How to print out the result of a SWI-Prolog query from C# using Swi-cs-pl library

I'm trying to understand how the Swi-cs-pl library works by doing my own little program, but I cannot printout any result from a query based on the example from SWI-Prolog web.
I have this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SbsSW.SwiPlCs;
namespace HolaMundo
{
class Program
{
static void Main(string[] args)
{
if (!PlEngine.IsInitialized)
{
String[] param = { "-q" }; // suppressing informational and banner messages
PlEngine.Initialize(param);
PlQuery.PlCall("assert(father(hader, nevar))");
PlQuery.PlCall("assert(father(hader, sergio))");
PlQuery.PlCall("assert(brother(nevar, sergio):- father(hader, nevar), father(hader, sergio))");
//How do I write out in a Console the answer for a query like:
// brother(nevar, sergio)
PlEngine.PlCleanup();
}
}
}
}
As I said on my code, I just want to query something basic like: brother(nevar, sergio). and get any answer from Console like true or whatever.
Can anyone help me?

Running Python Script/application in Windows Phone 8

I want to run the Skeinforge slicer program written in Python inside my Windows Phone 8 C# application. I have determined that I should probably use IronPython to do this, I have already determined that I can run Skeinforge inside the ipy.exe terminal I got when I installed IronPython. My problem though is that I am struggling to figure out how to host and run a Python script with IronPython inside Windows Phone 8. I have also already managed to get a simple hello world script running inside a Desktop Windows Forms application that transfers the applications console output to the Debug console with the following code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DebugWriter debugW = new DebugWriter();
Console.SetOut(debugW);
}
private void button1_Click(object sender, EventArgs e)
{
TextWriter tw = new StreamWriter("Test.py");
tw.Write(scriptBox.Text);
tw.Close();
try
{
var ipy = Python.CreateRuntime();
dynamic test = ipy.UseFile("Test.py");
}
catch (Exception ex)
{
Debug.WriteLine("Error: " + ex.Message);
}
}
}
}
And this is the DebugWriter:
class DebugWriter : TextWriter
{
private StringBuilder content = new StringBuilder();
public DebugWriter()
{
Debug.WriteLine("Writing console to debug");
}
public override void Write(char value)
{
base.Write(value);
if (value == '\n')
{
Debug.WriteLine(content.ToString());
content = new StringBuilder();
}
else
{
content.Append(value);
}
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
I have no idea how to even add the IronPython libraries to my Windows Phone 8 application though as the standard libraries won't import. I have though tried compiling the apparently now defunct Windows Phone 7 libraries with the master source code and I can import these libraries, but I get absolutely no response on the debug terminal when I try to run my hello world script.
Do any of you have any idea how to get this woring in Windows Phone 8, if you know how to do this in Windows 8/Metro/RT then that would also probably work for WP8.
UPDATE:
I have looked at the debug output again and I seem to get this error when trying to use the WP7 libraries to run a hello world script:
A first chance exception of type 'System.NotImplementedException' occurred in Microsoft.Scripting.DLL
Error: The method or operation is not implemented.
I managed to get Skeinforge running on a modified version of IPY. You can get the source for my application here: http://skeinforgewp8.codeplex.com/

How could Python access C# variable?

I am using visual studio 2010 AddIn template for ArcGis 10.0. Since I already wrote a python code for ArcGIS processing. I would like to insert a button into the ArcMap desktop command bar running this python code. Following is my simplized c# code and the beginning of python code for your reference:
c# code:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.VisualBasic;
namespace CallPython
{
public class CallPython : ESRI.ArcGIS.Desktop.AddIns.Button
{
public CallPython()
{
}
public string inputName()
{
string inputNames;
inputNames = "cavert.shp";
return inputNames;
}
protected override void OnClick()
{
inputName();
System.Diagnostics.Process.Start(
"D:\\From2\\BCAP\\python\\scripts\\trans5.py");
}
protected override void OnUpdate()
{
Enabled = ArcMap.Application != null;
}
}
}
Python code:
import arcpy
import os, win32com.client
from arcpy import env
arcpy.env.overwriteOutput = True
# Set environment settings
env.workspace = "D:/BCAP/trans/tempt/"
# Set local variables
vs_method_source = os.path.abspath("D:/Users/mar/CallPython/CallPython.cs")
vs_file_name = os.path.abspath("D:/Users/mar/CallPython/CallPython.sln")
vs = win32com.client.Dispatch("VisualStudio.DTE.7.1")
doc_template = vs.Documents.Open(vs_file_name)
mm = doc_template.CallPython.inputName() #CallPython is namespace, inputName is method
The last line of python code is the problem. I know it is definitely not right, but didn't know how to access C# namespace, class, then method from python.

Getting DLLs in WinForms application

I have to create a class that will load all the dll's from repository and check whether
they are inheriting from IMFServicePlugin interface and returns the
valid dlls.
that I have done using this...
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms.ComponentModel;
using MFDBAnalyser;
namespace MFDBAnalyserAssemblyValidator
{
public class MFDBAnalyserAssemblyValidator
{
static void Main(string[] args)
{
List<string> assemblyNames = new List<string>();
Assembly[] oAssemblies = new Assembly[args.Length];
for (int assemblyCount = 0; assemblyCount < args.Length; assemblyCount++)
{
oAssemblies[assemblyCount] = Assembly.LoadFile(args[assemblyCount]);
try
{
foreach (Type oType in oAssemblies[assemblyCount].GetTypes())
{
// Check whether class is inheriting from IMFServicePlugin.
if (oType.GetInterface("IMFDBAnalyserPlugin") == typeof(IMFDBAnalyserPlugin))
{
assemblyNames.Add(args[assemblyCount].Substring(args[assemblyCount].LastIndexOf("\\") + 1));
}
}
}
catch (Exception ex)
{
lblError.Text = "ERROR";
}
}
// Passing data one application domain to another.
AppDomain.CurrentDomain.SetData("AssemblyNames", assemblyNames.ToArray());
}
}
}
but this was for loading the dll from the repository but I also want to store these dll in another ORM class.
Can anybody help me out...
If possible plz provide some links so that I can get a sufficient idea of how dll works for an windows/desktop application.
At a first tip you should use Assembly.ReflectionOnlyLoad(). Cause if you load the assembly by using Assembly.LoadFile() the assembly will automatically be put into your local AppDomain!

Categories

Resources