public void TestValue() { var runtime = new AnonymousRTD(); runtime.UseStaticReadonlyFields(); runtime.AddValue("小明"); runtime.AddValue("小明1"); runtime.AddValue("小明"); runtime.AddValue("name", "abc"); runtime.AddValue("name2", "abc"); var nClass = NClass.RandomDomain() .Public() .BodyAppendLine(runtime.FieldsScript) .BodyAppendLine(runtime.MethodScript); var type = nClass.GetType(); var action = runtime.GetInitMethod(nClass); action(); string result0 = nClass.DelegateHandler.Func <string>($"return {type.Name}._anonymous_1;")(); string result1 = nClass.DelegateHandler.Func <string>($"return {type.Name}._anonymous_2;")(); string result2 = nClass.DelegateHandler.Func <string>($"return {type.Name}._anonymous_3;")(); string result3 = nClass.DelegateHandler.Func <string>($"return {type.Name}.name;")(); string result4 = nClass.DelegateHandler.Func <string>($"return {type.Name}.name2;")(); Assert.Equal("小明1", result1); Assert.Equal("小明", result0); Assert.Equal("小明", result2); Assert.Equal("abc", result3); Assert.Equal("abc", result4); }
public void TestDelegate() { Func <string, int> func = item => item.Length; Func <string, int> func1 = item => item.Length + 1; var runtime = new AnonymousRTD(); runtime.UseStaticFields(); var key = runtime.AddValue(func); runtime.AddValue(func1); runtime.AddValue(func); runtime.AddValue("name", "abc"); runtime.AddValue("name2", "abc"); var nClass = NClass.RandomDomain() .Public() .BodyAppendLine(runtime.FieldsScript) .BodyAppendLine(runtime.MethodScript); var type = nClass.GetType(); var action = runtime.GetInitMethod(nClass); action(); int result0 = nClass.DelegateHandler.Func <string, int>($"return {type.Name}.{key}(arg);")("hello"); int result1 = nClass.DelegateHandler.Func <string, int>($"return {type.Name}._anonymous_2(arg);")("hello"); int result2 = nClass.DelegateHandler.Func <string, int>($"return {type.Name}._anonymous_3(arg);")("hello"); string result3 = nClass.DelegateHandler.Func <string>($"return {type.Name}.name;")(); string result4 = nClass.DelegateHandler.Func <string>($"return {type.Name}.name2;")(); Assert.Equal(6, result1); Assert.Equal(5, result0); Assert.Equal(5, result2); Assert.Equal("abc", result3); Assert.Equal("abc", result4); }
public DynamicCacheBuilder(IDictionary <TKey, TValue> pairs, DyanamicCacheDirection queryDirection = DyanamicCacheDirection.Both) { if (queryDirection != DyanamicCacheDirection.ValueToKey) { AnonymousRTD _r2d_handler = AnonymousRTD.UseDomain(typeof(TKey).GetDomain()); var key_builder = new Dictionary <TKey, string>(); foreach (var item in pairs) { key_builder[item.Key] = $"return {_r2d_handler.AddValue(item.Value)};"; } StringBuilder keyBuilder = new StringBuilder(); keyBuilder.Append(ScriptKeyAction(key_builder)); keyBuilder.Append("return default;"); var method = typeof(Func <TKey, TValue>).GetMethod("Invoke"); _r2d_handler.BodyAppend(FakeMethodOperator.RandomDomain() .UseMethod(method) .Unsafe() .StaticMethodBody(keyBuilder.ToString()) .Script); var type = _r2d_handler.Complie(); GetValue = NDelegate .UseDomain(typeof(TKey).GetDomain()) .Func <Func <TKey, TValue> >($"return {_r2d_handler.TypeName}.Invoke;", type)(); } if (queryDirection != DyanamicCacheDirection.KeyToValue) { AnonymousRTD _r2d_handler = AnonymousRTD.UseDomain(typeof(TKey).GetDomain()); var value_builder = new Dictionary <TValue, string>(); foreach (var item in pairs) { if (!value_builder.ContainsKey(item.Value)) { value_builder[item.Value] = $"return new {typeof(TKey).GetDevelopName()}[]{{{_r2d_handler.AddValue(item.Key)}"; } else { value_builder[item.Value] += $",{_r2d_handler.AddValue(item.Key)}"; } } var temp_value_buidler = new Dictionary <TValue, string>(); foreach (var item in value_builder) { temp_value_buidler[item.Key] = item.Value + "};"; } StringBuilder valueBuilder = new StringBuilder(); valueBuilder.Append(ScriptValueAction(temp_value_buidler)); valueBuilder.Append("return null;"); var method = typeof(Func <TValue, TKey[]>).GetMethod("Invoke"); _r2d_handler.Body(FakeMethodOperator.RandomDomain() .UseMethod(method) .Unsafe() .StaticMethodBody(valueBuilder.ToString()) .Script); var type = _r2d_handler.Complie(); GetKeys = NDelegate .UseDomain(typeof(TValue).GetDomain()) .Func <Func <TValue, TKey[]> >($"return {_r2d_handler.TypeName}.Invoke;", type)(); } }