Najbrž vsi poznate JS ukaz evaluate(eval), kjer mu podaš JS kodo v obliki stringa in on to izvede.
Obstaja kaj takega v c#?
Recimo v enem stringu imam nek ime: "MyClass.Test". Imam tudi tak objekt:
public static class MyClass
{
public static Test { get { return "bla"; } }
}
Sedaj pa bi rad v eno spremenljivko dobil ta bla na podlagi stringa, ki predstavlja objekt:
public string Get(string _Eval)
{
return Evaluate(_Eval); //vrnil bi "bla"
}
Seveda mora stvar biti zelo hitra, drugače mi ne pomaga.
Obstaja kaj takega?
spirit1 - petek, 04. maj 2007
o hudica.. se ti je pa dalo Fora:mogoce bi ti DynamicMethod bil bolj v pomoc. glavno vprasanje je seveda, zakaj hoces to narediti?
DejanStanic - petek, 04. maj 2007
using System; System;using System.CodeDom.Compiler; System.CodeDom.Compiler;using System.Reflection; System.Reflection;using Microsoft.JScript; Microsoft.JScript;namespace Infoteh.Classes.Numerics{public static class Evaluator Infoteh.Classes.Numerics{public static class Evaluatorpublic static class Evaluator{static Evaluator(){CodeDomProvider cdp = new JScriptCodeProvider();CompilerParameters cp = new CompilerParameters();cp.GenerateInMemory = true;CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source);Assembly assembly = cr.CompiledAssembly;type = assembly.GetType("Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){trystatic Evaluator(){CodeDomProvider cdp = new JScriptCodeProvider();CompilerParameters cp = new CompilerParameters();cp.GenerateInMemory = true;CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source);Assembly assembly = cr.CompiledAssembly;type = assembly.GetType("Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryCodeDomProvider cdp = new JScriptCodeProvider();CompilerParameters cp = new CompilerParameters();cp.GenerateInMemory = true;CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source);Assembly assembly = cr.CompiledAssembly;type = assembly.GetType("Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryCompilerParameters cp = new CompilerParameters();cp.GenerateInMemory = true;CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source);Assembly assembly = cr.CompiledAssembly;type = assembly.GetType("Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){trytrue;CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source);Assembly assembly = cr.CompiledAssembly;type = assembly.GetType("Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryCompilerResults cr = cdp.CompileAssemblyFromSource(cp, source);Assembly assembly = cr.CompiledAssembly;type = assembly.GetType("Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryAssembly assembly = cr.CompiledAssembly;type = assembly.GetType("Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){try"Evaluator.Evaluator");evaluator = Activator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryActivator.CreateInstance(type);}private static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryprivate static object evaluator = null;private static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryprivate static Type type = null;private static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){tryprivate static readonly string source = @"package Evaluator{class Evaluator{public function Eval(expr: String): String{return eval(expr);}}}";public static object AsObject(string expression){try;public static object AsObject(string expression){trypublic static object AsObject(string expression){trytry{return type.InvokeMember("Eval", BindingFlags.InvokeMethod, null, evaluator, new object[] { expression });}catchreturn type.InvokeMember("Eval", BindingFlags.InvokeMethod, null, evaluator, new object[] { expression });}catchcatch{return null;}}public static string AsString(string expression){object o = AsObject(expression);return o == null ? null : o.ToString();}public static double? AsDouble(string expression){string s = AsString(expression);double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} return null;}}public static string AsString(string expression){object o = AsObject(expression);return o == null ? null : o.ToString();}public static double? AsDouble(string expression){string s = AsString(expression);double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} public static string AsString(string expression){object o = AsObject(expression);return o == null ? null : o.ToString();}public static double? AsDouble(string expression){string s = AsString(expression);double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} object o = AsObject(expression);return o == null ? null : o.ToString();}public static double? AsDouble(string expression){string s = AsString(expression);double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} return o == null ? null : o.ToString();}public static double? AsDouble(string expression){string s = AsString(expression);double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} public static double? AsDouble(string expression){string s = AsString(expression);double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} string s = AsString(expression);double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} double d;return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null;}public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} public static int? AsInt(string expression){string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} string s = AsString(expression);int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} int i;return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}} return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null;}}}
DejanStanic - petek, 04. maj 2007
using System;using System.CodeDom.Compiler;using System.Reflection;using Microsoft.JScript;namespace Infoteh.Classes.Numerics{ public static class Evaluator { static Evaluator() { CodeDomProvider cdp = new JScriptCodeProvider(); CompilerParameters cp = new CompilerParameters(); cp.GenerateInMemory = true; CompilerResults cr = cdp.CompileAssemblyFromSource(cp, source); Assembly assembly = cr.CompiledAssembly; type = assembly.GetType("Evaluator.Evaluator"); evaluator = Activator.CreateInstance(type); } private static object evaluator = null; private static Type type = null; private static readonly string source = @" package Evaluator { class Evaluator { public function Eval(expr: String): String { return eval(expr); } } } "; public static object AsObject(string expression) { try { return type.InvokeMember("Eval", BindingFlags.InvokeMethod, null, evaluator, new object[] { expression }); } catch { return null; } } public static string AsString(string expression) { object o = AsObject(expression); return o == null ? null : o.ToString(); } public static double? AsDouble(string expression) { string s = AsString(expression); double d; return double.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out d) ? (double?)d : null; } public static int? AsInt(string expression) { string s = AsString(expression); int i; return int.TryParse(s, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out i) ? (int?)i : null; } }}