Article View: alt.pl.comp.lang.csharp
Article #21752Re: jak to sie robi w C# ?
From: Selvin
Date: Mon, 20 Sep 2010 15:00
Date: Mon, 20 Sep 2010 15:00
95 lines
4096 bytes
4096 bytes
1. Uzycie jakiegos języka skryptowego(np IronPython)
2. Napisanie wlasnego parsera wyrazen.
3. w frameworku 4 mozna np tak:
namespace ScriptingInterface
{
public interface IScriptType
{
dynamic RunScript(dynamic _);
}
public class ScriptProvider
{
ScriptProvider(string code)
{
Assembly compiledScript = CompileCode(@"
using System;
using ScriptingInterface;
using System.ComponentModel.Composition;
[Export(typeof(IScriptType))]
public class ScriptType : IScriptType
{
public dynamic RunScript(dynamic _)
{
return " + code + @";
}
}");
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new
AssemblyCatalog( compiledScript));
CompositionContainer _container = new
CompositionContainer(catalog);
_container.SatisfyImportsOnce(this);
}
static Assembly CompileCode(string code)
{
Microsoft.CSharp.CSharpCodeProvider csProvider = new
Microsoft.CSharp.CSharpCodeProvider();
CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = false;
options.GenerateInMemory = true;
options.ReferencedAssemblies.Add("System.Core.dll");
options.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
options.ReferencedAssemblies.Add("System.ComponentModel.Composition.dll");
options.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
CompilerResults result;
result = csProvider.CompileAssemblyFromSource(options,
code);
if (result.Errors.HasErrors)
{
throw new Exception(result.Errors[0].ErrorText);
}
return result.CompiledAssembly;
}
[Import]
IScriptType Script { get; set; }
public static dynamic RunScript(string code, dynamic param)
{
return new
ScriptingInterface.ScriptProvider(code).Script.RunScript(param);
}
}
}
i wywolanie
private void button1_Click(object sender, EventArgs e)
{
dynamic param = new ExpandoObject();
param.iIle = 2;
param.dNorma = 2.55M;
param.dCena = 120.57M;
param.dWsp = 0.2M;
try
{
MessageBox.Show(ScriptingInterface.ScriptProvider.RunScript(textBox1.Text,
param).ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
mankamentem tego rozwiazania jest to ze wyrazenie musi wygladac tak:
_.iIle * _.dNorma * _.dCena * (1 + _.dWsp)
Technical Details
Message-ID:
<29a54593-9d92-4968-ba75-062ef28bbe4f@m1g2000vbh.googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.glorb.com!postnews.google.com!m1g2000vbh.googlegroups.com!not-for-mailReferences:
<4c9721da$0$27026$65785112@news.neostrada.pl>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.gazeta.pl!newsfeed.pionier.net.pl!news.glorb.com!postnews.google.com!m1g2000vbh.googlegroups.com!not-for-mail From: Selvin <przemek.sulikowski@gmail.com> Newsgroups: alt.pl.comp.lang.csharp Subject: Re: jak to sie robi w C# ? Date: Mon, 20 Sep 2010 08:00:26 -0700 (PDT) Organization: http://groups.google.com Lines: 97 Message-ID: <29a54593-9d92-4968-ba75-062ef28bbe4f@m1g2000vbh.googlegroups.com> References: <4c9721da$0$27026$65785112@news.neostrada.pl> NNTP-Posting-Host: 217.97.163.91 Mime-Version: 1.0 X-Trace: posting.google.com 1284994838 20476 127.0.0.1 (20 Sep 2010 15:00:38 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 20 Sep 2010 15:00:38 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: m1g2000vbh.googlegroups.com; posting-host=217.97.163.91; posting-account=R8EpSwoAAADeCYcG96LM5BAc_tdIdesQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.59 Safari/534.3,gzip(gfe) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit