| | |
| Stránka: 1 z 1
| [ Príspevkov: 3 ] | |
Autor | Správa |
---|
Registrovaný: 23.04.09 Prihlásený: 26.09.15 Príspevky: 23 Témy: 7 | Napísal nico: 12.06.2014 14:51 | |
|
Ahoj, mam stiahnuty kod z netu pre vypocet matematickych formulacii cez string a potreboval by som implementovat aj vlastne metody z triedy tj. nie len class Math. Stiahnuty kod: Kód: string code = "Math.Pow(23 + 48, 2) - (7.76 * 3.14)"; string result = Eval.StringEval(code); Console.WriteLine("Results: " + result);
Eval Class -------------------- using System; using System.Collections.Generic; using System.Reflection; using System.Text; using System.CodeDom; using System.CodeDom.Compiler;
public static class Eval { private static string funcprefix = "using System;\r\n" + "public delegate void Proc();\r\n" + "public class Wrapper { \r\n" + " public static object Set(string name, object value) { \r\n" + " AppDomain.CurrentDomain.SetData(name, value);\r\n" + " return value; \r\n" + " }\r\n" + " public static object Get(string name) { \r\n" + " return AppDomain.CurrentDomain.GetData(name);\r\n" + " }\r\n" + " public static object Invoke(Proc proc) { \r\n" + " proc();\r\n" + " return null; \r\n" + " }\r\n" + " public static object Eval() { return \r\n"; static string funcsuffix = "; \r\n} }"; public static string StringEval(string expr) { string program = funcprefix + expr + funcsuffix; CompilerParameters cp = new CompilerParameters(); cp.GenerateExecutable = false; cp.GenerateInMemory = true;
CompilerResults results = CodeDomProvider.CreateProvider("C#").CompileAssemb lyFromSource(cp, new string[]{program}); if ( results.Errors.HasErrors ) { if ( results.Errors[0].ErrorNumber == "CS0029" ) return StringEval("Invoke(delegate { " + expr + "; })"); throw new Exception(results.Errors[0].ErrorText); } else { Assembly assm = results.CompiledAssembly; Type target = assm.GetType("Wrapper"); MethodInfo method = target.GetMethod("Eval"); object result = method.Invoke(null, null); return result == null ? null : result.ToString(); } } }
... a potreboval by som nieco na sposob Kód: string code = "Test(Math.Pow(23 + 48, 2) - (7.76 * 3.14))"; Vopred vdaka za rozumnu radu.
|
|
Registrovaný: 23.09.13 Prihlásený: 04.12.14 Príspevky: 68 Témy: 1 |
Zaujimava tema, skusal som, da sa to. Sprav si samostatnu libku kde naimplementujes svoji staticku triedu so svojimi statickymi matematickymi metodami. uprav StringEval nasledovne (MyMathLib.dll je tvoja matematicka dll) Kód: public static string StringEval(string expr) { string program = funcprefix + expr + funcsuffix; CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("MyMathLib.dll"); cp.GenerateExecutable = false; cp.GenerateInMemory = true;
CompilerResults results = CodeDomProvider.CreateProvider("C#").CompileAssemblyFromSource(cp, new string[] { program }); if (results.Errors.HasErrors) { if (results.Errors[0].ErrorNumber == "CS0029") return StringEval("Invoke(delegate { " + expr + "; })"); throw new Exception(results.Errors[0].ErrorText); } else { Assembly assm = results.CompiledAssembly; Type target = assm.GetType("Wrapper"); MethodInfo method = target.GetMethod("Eval"); object result = method.Invoke(null, null); return result == null ? null : result.ToString(); } } vola to potom budes nejako takto: Kód: string code = "MyMathLib.MyMathFunctions.TestFunction(Math.Pow(23 + 48, 2) - (7.76 * 3.14))"; Namespace.NazovTriedy.Funkcia
Naposledy upravil killer dňa 13.06.2014 16:57, celkovo upravené 1 | Keď to dáš nabudúce do [code] bude to ešte lepšie |
|
|
Registrovaný: 23.04.09 Prihlásený: 26.09.15 Príspevky: 23 Témy: 7 | Napísal autor témy nico: 13.06.2014 16:23 | |
|
pisem si tvoj nick ...dobra praca, vdaka
|
|
| Stránka: 1 z 1
| [ Príspevkov: 3 ] | |
Podobné témy | Témy | Odpovede | Zobrazenia | Posledný príspevok |
---|
| C# - Formulovanie matematickych operacii pomocou string v Assembler, C, C++, Pascal, Java | 0 | 348 | 26.09.2015 20:59 nico | | Vietnamskí lekári si pri operácii svietili mobilmi v Novinky | 11 | 461 | 07.06.2007 16:19 Lub0$ | | String v PHP, ASP | 2 | 615 | 19.04.2010 21:20 Antuanet | | Java String v Assembler, C, C++, Pascal, Java | 4 | 1174 | 31.03.2010 22:00 ado21 | | Connection String v Technológia .NET | 3 | 1634 | 10.09.2009 12:21 TJ | | std::string v Assembler, C, C++, Pascal, Java | 2 | 688 | 01.01.2010 0:20 koso | | String od do v Delphi, Visual Basic | 1 | 626 | 30.11.2009 18:55 ha2 | | Java int = string v Assembler, C, C++, Pascal, Java | 4 | 505 | 17.02.2016 20:33 neutroN | | pascal: string 255+ v Assembler, C, C++, Pascal, Java | 4 | 558 | 03.02.2011 18:08 juho | | Query string - vs _ v Internetový marketing, SEO, reklama | 4 | 466 | 18.08.2012 22:41 emer | | c++ praca so string v Assembler, C, C++, Pascal, Java | 2 | 386 | 16.11.2014 13:17 mork4 | | vysledok gettype stale string v PHP, ASP | 6 | 466 | 14.12.2010 17:05 Feko | | [VYRIESENE] String do binary v PHP, ASP | 2 | 444 | 07.03.2009 15:57 Tominator | | String = nieco z DB v PHP, ASP | 2 | 375 | 23.03.2012 14:49 exoomer | | C++ string to float v Assembler, C, C++, Pascal, Java | 11 | 1044 | 27.08.2010 12:46 Progy | | html adresa - cut string v PHP, ASP | 14 | 1739 | 14.05.2018 9:21 baumax |
| Nemôžete zakladať nové témy v tomto fóre Nemôžete odpovedať na témy v tomto fóre Nemôžete upravovať svoje príspevky v tomto fóre Nemôžete mazať svoje príspevky v tomto fóre
|
|