2010-03-02 15 views
10

मेरे पास सी # में एक डेस्कटॉप एप्लिकेशन लिखा गया है, मैं सी #/वीबी पर स्क्रिप्ट योग्य बनाना चाहता हूं। आदर्श रूप में, उपयोगकर्ता एक पक्ष फलक खोलने हैं और तरहमैं अपने आवेदन को सी # में स्क्रिप्ट करने योग्य कैसे बना सकता हूं?

foreach (var item in myApplication.Items) 
    item.DoSomething(); 

वाक्य रचना हाइलाइटिंग और कोड पूरा होने के बाद भयानक होगा बातें लिखते हैं, लेकिन मैं इसे बिना नहीं रह सकता है। मैं नहीं उपयोगकर्ताओं को Visual Studio 2010 स्थापित करने की आवश्यकता है।

मैं संकलक का आह्वान करने, आउटपुट असेंबली लोड करने और चलाने के बारे में सोच रहा हूं।

क्या कोई बेहतर तरीका है?

क्या Microsoft.CSharp उत्तर है?

+1

यदि आप वर्णन करते हैं कि आप वास्तव में क्या हासिल करना चाहते हैं तो यह आपकी सहायता करेगा। – Perpetualcoder

+1

रुको, क्या आप पूछ रहे हैं, "मैं अपने सी # एप्लिकेशन को एक स्क्रिप्टिंग भाषा में कैसे स्क्रिप्ट योग्य बना सकता हूं?" या "मैं सी # में अपना सी # एप्लिकेशन स्क्रिप्ट योग्य कैसे बना सकता हूं?" –

+1

सी # आवेदन स्क्रिप्ट योग्य सी # –

उत्तर

1

एक स्क्रिप्टिंग भाषा का उपयोग करें। Tcl, LUA या यहां तक ​​कि जावास्क्रिप्ट भी दिमाग में आता है।

वास्तव में आसान Tcl का प्रयोग किया जाता है:

using System.Runtime.InteropServices; 
using System; 

namespace TclWrap { 
    public class TclAPI { 
     [DllImport("tcl84.DLL")] 
     public static extern IntPtr Tcl_CreateInterp(); 
     [DllImport("tcl84.Dll")] 
     public static extern int Tcl_Eval(IntPtr interp,string skript); 
     [DllImport("tcl84.Dll")] 
     public static extern IntPtr Tcl_GetObjResult(IntPtr interp); 
     [DllImport("tcl84.Dll")] 
     public static extern string Tcl_GetStringFromObj(IntPtr tclObj,IntPtr length); 
    } 
    public class TclInterpreter { 
     private IntPtr interp; 
     public TclInterpreter() { 
      interp = TclAPI.Tcl_CreateInterp(); 
      if (interp == IntPtr.Zero) { 
       throw new SystemException("can not initialize Tcl interpreter"); 
      } 
     } 
     public int evalScript(string script) { 
      return TclAPI.Tcl_Eval(interp,script);   
     } 
     public string Result { 
      get { 
       IntPtr obj = TclAPI.Tcl_GetObjResult(interp); 
       if (obj == IntPtr.Zero) { 
        return ""; 
       } else { 
        return TclAPI.Tcl_GetStringFromObj(obj,IntPtr.Zero); 
       } 
      } 
     } 
    } 
} 

तो यह चाहते का उपयोग करें:

TclInterpreter interp = new TclInterpreter(); 
string result; 
if (interp.evalScript("set a 3; {exp $a + 2}")) { 
    result = interp.Result; 
} 
+0

क्या इनमें से किसी भी स्क्रिप्टिंग भाषाओं के लिए कोई सी # पुल हैं और यदि आप लिंक प्रदान कर सकते हैं? –

4

आप के बारे में IronPython या IronRuby में सोचा है?

+0

प्रदर्शन ऐसा कुछ है जिसे आपको मॉनिटर करने की आवश्यकता हो सकती है। उत्तर के लिए +1 – Perpetualcoder

+1

यह इस बात पर निर्भर करता है कि स्क्रिपिंग भाग में कितना प्रोग्राम तर्क है और कितना भारी उठाना .NET/C# कोड है। पाइथन आमतौर पर बड़े, उच्च प्रदर्शन सी ++ प्रोग्राम स्क्रिप्ट करने योग्य (उदाहरण के लिए वीडियो गेम, पिक्सार के आंतरिक मेनव एनीमेशन सॉफ्टवेयर इत्यादि) बनाने के लिए उपयोग किया जाता है लेकिन पाइथन नई सुविधाओं के निर्माण का पारंपरिक तरीका नहीं है बल्कि पाइथन एक उच्च स्तरीय तरीके डालने की अनुमति देगा दिलचस्प, उपन्यास तरीकों से एक साथ मौजूदा उच्च प्रदर्शन टुकड़े। –

+1

या आयरनजेएस? एक सी # दोस्त के लिए थोड़ा और सुलभ। –

1

आप संकलक को किसी भी तरह से बुलाएंगे, क्योंकि सी # एक संकलित भाषा है। इसे करने का सबसे अच्छा तरीका CSharpCodeProvider - класс में चेक किया जा सकता है।

+1

आप रनटाइम पर कंपाइलर को नहीं बुलाएंगे। हालांकि आप क्लियर का आह्वान करेंगे। –

+1

आप कंपाइलर का आह्वान करेंगे। csc.exe को वैसे भी बुलाया जाएगा, आप – Andrey

1

मैं पावरशेल या MEF का उपयोग करूंगा। यह वास्तव में इस बात पर निर्भर करता है कि आपके द्वारा स्क्रिटेबल और आपके पास किस प्रकार का एप्लिकेशन है। पावरशेल के बारे में सबसे अच्छा हिस्सा यह सीधे होस्ट करने योग्य है और सीधे स्क्रिप्टिंग तरीके से .NET इंटरफेस का उपयोग करने के लिए डिज़ाइन किया गया है।

0

आपका आवेदन किस भाषा में लिखा गया है? यदि सी ++, तो आप Google V8, एक एम्बेड करने योग्य ईसीएमएस्क्रिप्ट/जावास्क्रिप्ट इंजन पर विचार कर सकते हैं।

1

आप एक उदाहरण के रूप में निम्नलिखित खुला स्रोत समाधान का उपयोग कर सकते हैं: https://bitbucket.org/jlyonsmith/coderunner/wiki/Home

+0

को जांचने के लिए परावर्तक का उपयोग कर सकते हैं कोडप्लेक्स जल्द ही बंद हो रहा है, इसलिए आप लिंक टूटा जाने से पहले कुछ करने पर विचार कर सकते हैं। –

1

मैं ठीक उसी समस्या थी और googling और कुछ संशोधनों का एक सा के साथ मैं Microsoft.CSharp.CSharpCodeProvider जो के लिए अनुमति देता का उपयोग कर इसे हल एक सी # टेम्पलेट संपादित करें जो मैं उन लोगों को प्रस्तुत करता हूं जो मेरे आवेदन के पूर्ण ऑब्जेक्ट मॉडल का खुलासा करते हैं और वे एप्लिकेशन से ही पैरामीटर पास कर सकते हैं और परिणाम को वापस कर सकते हैं।

पूर्ण सी # समाधान http://qurancode.com से डाउनलोड किया जा सकता है। लेकिन यहाँ मुख्य कोड है कि करता है सिर्फ इतना है कि:

using System; 
using System.Text; 
using System.IO; 
using System.Collections.Generic; 
using System.Reflection; 
using System.CodeDom.Compiler; 
using Microsoft.CSharp; 
using System.Security; 
using Model; // this is my application Model with my own classes 


public static class ScriptRunner 
{ 
    private static string s_scripts_directory = "Scripts"; 
    static ScriptRunner() 
    { 
     if (!Directory.Exists(s_scripts_directory)) 
     { 
      Directory.CreateDirectory(s_scripts_directory); 
     } 
    } 

    /// <summary> 
    /// Load a C# script fie 
    /// </summary> 
    /// <param name="filename">file to load</param> 
    /// <returns>file content</returns> 
    public static string LoadScript(string filename) 
    { 
     StringBuilder str = new StringBuilder(); 
     string path = s_scripts_directory + "/" + filename; 
     if (File.Exists(filename)) 
     { 
      using (StreamReader reader = File.OpenText(path)) 
      { 
       string line = ""; 
       while ((line = reader.ReadLine()) != null) 
       { 
        str.AppendLine(line); 
       } 
      } 
     } 
     return str.ToString(); 
    } 

    /// <summary> 
    /// Compiles the source_code 
    /// </summary> 
    /// <param name="source_code">source_code must implements IScript interface</param> 
    /// <returns>compiled Assembly</returns> 
    public static CompilerResults CompileCode(string source_code) 
    { 
     CSharpCodeProvider provider = new CSharpCodeProvider(); 

     CompilerParameters options = new CompilerParameters(); 
     options.GenerateExecutable = false; // generate a Class Library assembly 
     options.GenerateInMemory = true;  // so we don;t have to delete it from disk 

     Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); 
     foreach (Assembly assembly in assemblies) 
     { 
      options.ReferencedAssemblies.Add(assembly.Location); 
     } 

     return provider.CompileAssemblyFromSource(options, source_code); 
    } 

    /// <summary> 
    /// Execute the IScriptRunner.Run method in the compiled_assembly 
    /// </summary> 
    /// <param name="compiled_assembly">compiled assembly</param> 
    /// <param name="args">method arguments</param> 
    /// <returns>object returned</returns> 
    public static object Run(Assembly compiled_assembly, object[] args, PermissionSet permission_set) 
    { 
     if (compiled_assembly != null) 
     { 
      // security is not implemented yet !NIY 
      // using Utilties.PrivateStorage was can save but not diaplay in Notepad 
      // plus the output is saved in C:\Users\<user>\AppData\Local\IsolatedStorage\... 
      // no contral over where to save make QuranCode unportable applicaton, which is a no no 
      //// restrict code security 
      //permission_set.PermitOnly(); 

      foreach (Type type in compiled_assembly.GetExportedTypes()) 
      { 
       foreach (Type interface_type in type.GetInterfaces()) 
       { 
        if (interface_type == typeof(IScriptRunner)) 
        { 
         ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes); 
         if ((constructor != null) && (constructor.IsPublic)) 
         { 
          // construct object using default constructor 
          IScriptRunner obj = constructor.Invoke(null) as IScriptRunner; 
          if (obj != null) 
          { 
           return obj.Run(args); 
          } 
          else 
          { 
           throw new Exception("Invalid C# code!"); 
          } 
         } 
         else 
         { 
          throw new Exception("No default constructor was found!"); 
         } 
        } 
        else 
        { 
         throw new Exception("IScriptRunner is not implemented!"); 
        } 
       } 
      } 

      // revert security restrictions 
      //CodeAccessPermission.RevertPermitOnly(); 
     } 
     return null; 
    } 

    /// <summary> 
    /// Execute a public static method_name(args) in compiled_assembly 
    /// </summary> 
    /// <param name="compiled_assembly">compiled assembly</param> 
    /// <param name="methode_name">method to execute</param> 
    /// <param name="args">method arguments</param> 
    /// <returns>method execution result</returns> 
    public static object ExecuteStaticMethod(Assembly compiled_assembly, string methode_name, object[] args) 
    { 
     if (compiled_assembly != null) 
     { 
      foreach (Type type in compiled_assembly.GetTypes()) 
      { 
       foreach (MethodInfo method in type.GetMethods()) 
       { 
        if (method.Name == methode_name) 
        { 
         if ((method != null) && (method.IsPublic) && (method.IsStatic)) 
         { 
          return method.Invoke(null, args); 
         } 
         else 
         { 
          throw new Exception("Cannot invoke method :" + methode_name); 
         } 
        } 
       } 
      } 
     } 
     return null; 
    } 

    /// <summary> 
    /// Execute a public method_name(args) in compiled_assembly 
    /// </summary> 
    /// <param name="compiled_assembly">compiled assembly</param> 
    /// <param name="methode_name">method to execute</param> 
    /// <param name="args">method arguments</param> 
    /// <returns>method execution result</returns> 
    public static object ExecuteInstanceMethod(Assembly compiled_assembly, string methode_name, object[] args) 
    { 
     if (compiled_assembly != null) 
     { 
      foreach (Type type in compiled_assembly.GetTypes()) 
      { 
       foreach (MethodInfo method in type.GetMethods()) 
       { 
        if (method.Name == methode_name) 
        { 
         if ((method != null) && (method.IsPublic)) 
         { 
          object obj = Activator.CreateInstance(type, null); 
          return method.Invoke(obj, args); 
         } 
         else 
         { 
          throw new Exception("Cannot invoke method :" + methode_name); 
         } 
        } 
       } 
      } 
     } 
     return null; 
    } 
} 

मैं तो एक सी # इंटरफ़ेस परिभाषित उपयोगकर्ता कोड, जहां वे वे अपने ठोस भागो विधि के अंदर की तरह anythng डाल करने के लिए स्वतंत्र हैं द्वारा लागू किया जाना:

/// <summary> 
/// Generic method runner takes any number and type of args and return any type 
/// </summary> 
public interface IScriptRunner 
{ 
    object Run(object[] args); 
} 

और यहाँ स्टार्टअप टेम्पलेट उपयोगकर्ता कर सकते हैं प्रदान करता है:

using System; 
using System.Collections.Generic; 
using System.Windows.Forms; 
using System.Text; 
using System.IO; 
using Model; 

public class MyScript : IScriptRunner 
{ 
    private string m_scripts_directory = "Scripts"; 

    /// <summary> 
    /// Run implements IScriptRunner interface 
    /// to be invoked by QuranCode application 
    /// with Client, current Selection.Verses, and extra data 
    /// </summary> 
    /// <param name="args">any number and type of arguments</param> 
    /// <returns>return any type</returns> 
    public object Run(object[] args) 
    { 
     try 
     { 
      if (args.Length == 3) // ScriptMethod(Client, List<Verse>, string) 
      { 
       Client client = args[0] as Client; 
       List<Verse> verses = args[1] as List<Verse>; 
       string extra = args[2].ToString(); 
       if ((client != null) && (verses != null)) 
       { 
        return MyMethod(client, verses, extra); 
       } 
      } 
      return null; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, Application.ProductName); 
      return null; 
     } 
    } 

    /// <summary> 
    /// Write your C# script insde this method. 
    /// Don't change its name or parameters 
    /// </summary> 
    /// <param name="client">Client object holding a reference to the currently selected Book object in TextMode (eg Simplified29)</param> 
    /// <param name="verses">Verses of the currently selected Chapter/Page/Station/Part/Group/Quarter/Bowing part of the Book</param> 
    /// <param name="extra">any user parameter in the TextBox next to the EXE button (ex Frequency, LettersToJump, DigitSum target, etc)</param> 
    /// <returns>true to disply back in QuranCode matching verses. false to keep script window open</returns> 
    private long MyMethod(Client client, List<Verse> verses, string extra) 
    { 
     if (client == null) return false; 
     if (verses == null) return false; 
     if (verses.Count == 0) return false; 

     int target; 
     if (extra == "") 
     { 
      target = 0; 
     } 
     else 
     { 
      if (!int.TryParse(extra, out target)) 
      { 
       return false; 
      } 
     } 

     try 
     { 
      long total_value = 0L; 
      foreach (Verse verse in verses) 
      { 
       total_value += Client.CalculateValue(verse.Text); 
      } 
      return total_value; 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, Application.ProductName); 
      return 0L; 
     } 
    } 
} 

और यह कैसे मैं इसे अपने MainForm.cs

से कहते हैं
#region Usage from MainForm 
if (!ScriptTextBox.Visible) 
{ 
    ScriptTextBox.Text = ScriptRunner.LoadScript(@"Scripts\Template.cs"); 
    ScriptTextBox.Visible = true; 
} 
else // if visible 
{ 
    string source_code = ScriptTextBox.Text; 
    if (source_code.Length > 0) 
    { 
     Assembly compiled_assembly = ScriptRunner.CompileCode(source_code); 
     if (compiled_assembly != null) 
     { 
      object[] args = new object[] { m_client, m_client.Selection.Verses, "19" }; 
      object result = ScriptRunner.Run(compiled_assembly, args); 
      // process result here 
     } 
    } 
    ScriptTextBox.Visible = false; 
} 
#endregion 

अभी भी सिंटैक्स हाइलाइटिंग और कोडकंपलेशन है।

शुभकामनाएं!

+0

पुन * "मेरे आवेदन के पूर्ण ऑब्जेक्ट मॉडल का खुलासा करता है" *: ऑब्जेक्ट मॉडल क्या है? क्या यह सब (सार्वजनिक) कक्षाएं उनके सार्वजनिक तरीकों/गुणों के साथ हैं, या क्या? –

संबंधित मुद्दे