Skip to content

Commit 8a6976e

Browse files
committed
#增加自定义方法调用#
1 parent edd2707 commit 8a6976e

File tree

7 files changed

+75
-37
lines changed

7 files changed

+75
-37
lines changed

APIJSON.NET/2.png

16.2 KB
Loading

APIJSON.NET/3.png

4.67 KB
Loading

APIJSON.NET/APIJSON.NET.Test/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ static void Main(string[] args)
3939
// ");
4040
//IRestResponse response2 = client.Execute(request);
4141
//Console.WriteLine(response2.Content);
42-
43-
42+
string str = "isContain(praiseUserIdList,userId)";
43+
Console.WriteLine(str.Substring(0,str.IndexOf("(")));
44+
Console.WriteLine(str.Substring(str.IndexOf("(")+1).TrimEnd(')'));
45+
4446
Console.ReadLine();
4547
}
4648
}

APIJSON.NET/APIJSON.NET/Controllers/JsonController.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public ActionResult Query([FromBody]string json)
9797
if (ddf != null)
9898
{
9999
zht.Add(subtable, JToken.FromObject(ddf));
100+
100101
}
101102
}
102103
}
@@ -129,28 +130,19 @@ public ActionResult Query([FromBody]string json)
129130
else if (key.Equals("func"))
130131
{
131132
jb = JObject.Parse(item.Value.ToString());
132-
Type type = typeof(MethodList);
133+
Type type = typeof(FuncList);
133134
Object obj = Activator.CreateInstance(type);
134135
var bb = new JObject();
135136
foreach (var f in jb)
136137
{
137138
var types = new List<Type>();
138-
var param = new List<string>();
139+
var param = new List<object>();
139140
foreach (var va in JArray.Parse(f.Value.ToString()))
140141
{
141-
types.Add(typeof(string));
142-
param.Add(va.ToString());
143-
}
144-
MethodInfo mt = type.GetMethod(f.Key, types.ToArray());
145-
if (mt == null)
146-
{
147-
bb.Add(f.Key, "没有获取到相应的函数!");
142+
types.Add(typeof(object));
143+
param.Add(va);
148144
}
149-
else
150-
{
151-
bb.Add(f.Key, JToken.FromObject(mt.Invoke(obj, param.ToArray())));
152-
}
153-
145+
bb.Add(f.Key, JToken.FromObject(selectTable.ExecFunc(f.Key,param.ToArray(), types.ToArray())));
154146
}
155147
ht.Add("func", bb);
156148
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace APIJSON.NET
5+
{
6+
/// <summary>
7+
/// 自定义方法
8+
/// </summary>
9+
public class FuncList
10+
{
11+
public string Merge(object a, object b)
12+
{
13+
return a.ToString() + b.ToString();
14+
}
15+
public object MergeObj(object a, object b)
16+
{
17+
return new { a, b };
18+
}
19+
public bool isContain(object a, object b)
20+
{
21+
return a.ToString().Split(',').Contains(b);
22+
}
23+
}
24+
}

APIJSON.NET/APIJSON.NET/MethodList.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

APIJSON.NET/APIJSON.NET/SelectTable.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq;
10+
using System.Reflection;
11+
using System.Text.RegularExpressions;
12+
1013
public class SelectTable
1114
{
1215
private readonly IIdentityService _identitySvc;
@@ -37,6 +40,24 @@ public bool IsCol(string table, string col)
3740
{
3841
return db.Db.DbMaintenance.GetColumnInfosByTableName(table).Any(it => it.DbColumnName.Equals(col, StringComparison.CurrentCultureIgnoreCase));
3942
}
43+
/// <summary>
44+
/// 动态调用方法
45+
/// </summary>
46+
/// <param name="funcname"></param>
47+
/// <param name="param"></param>
48+
/// <param name="types"></param>
49+
/// <returns></returns>
50+
public object ExecFunc(string funcname,object[] param, Type[] types)
51+
{
52+
Type type = typeof(FuncList);
53+
Object obj = Activator.CreateInstance(type);
54+
MethodInfo mt = type.GetMethod(funcname,types);
55+
if (mt==null)
56+
{
57+
throw new Exception($"{funcname}没有获取到相应的函数");
58+
}
59+
return mt.Invoke(obj, param);
60+
}
4061

4162
public (dynamic,int) GetTableData(string subtable, int page, int count, string json, JObject dd)
4263
{
@@ -78,8 +99,26 @@ public dynamic GetFirstData(string subtable, string json, JObject dd)
7899
JObject values = JObject.Parse(json);
79100
values.Remove("page");
80101
values.Remove("count");
81-
var tb = sugarQueryable(subtable, selectrole, values, dd);
82-
return tb.First();
102+
var tb = sugarQueryable(subtable, selectrole, values, dd).First();
103+
var dic = (IDictionary<string, object>)tb;
104+
foreach (var item in values.Properties().Where(it => it.Name.EndsWith("()")))
105+
{
106+
if (item.Value.IsValue())
107+
{
108+
string func = item.Value.ToString().Substring(0, item.Value.ToString().IndexOf("("));
109+
string param = item.Value.ToString().Substring(item.Value.ToString().IndexOf("(") + 1).TrimEnd(')') ;
110+
var types = new List<Type>();
111+
var paramss = new List<object>();
112+
foreach (var va in param.Split(","))
113+
{
114+
types.Add(typeof(object));
115+
paramss.Add(tb.Where(it => it.Key.Equals(va)).Select(i => i.Value));
116+
}
117+
dic[item.Name] =ExecFunc(func, paramss.ToArray(), types.ToArray());
118+
}
119+
}
120+
121+
return tb;
83122

84123
}
85124
private ISugarQueryable<System.Dynamic.ExpandoObject> sugarQueryable(string subtable, string selectrole, JObject values, JObject dd)

0 commit comments

Comments
 (0)