Add SqlFunc.OnlyInSelectConvertToString(it.name,methodInfo)

This commit is contained in:
sunkaixuan 2023-10-29 14:38:36 +08:00
parent 1539dffd28
commit 0a95b319f2
6 changed files with 47 additions and 3 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@ -13,5 +14,6 @@ namespace SqlSugar
public string Format { get; set; }
public string PropertyName { get; set; }
public string MethodName { get; set; }
public MethodInfo MethodInfo { get; set; }
}
}

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@ -392,5 +393,10 @@ namespace SqlSugar
{
throw new NotSupportedException("Can only be used in expressions");
}
public static string OnlyInSelectConvertToString(string stringValue, MethodInfo methodInfo)
{
throw new NotSupportedException("Can only be used in expressions");
}
}
}

View File

@ -263,5 +263,20 @@ namespace SqlSugar
this.Context.Result.Replace(ExpressionConst.FormatSymbol, "-");
}
}
private void AppendOnlyInSelectConvertToString(ExpressionParameter parameter, Expression item, string asName)
{
var name =GetNewExpressionValue((item as MethodCallExpression)?.Arguments[0]);
var methodInfo = ExpressionTool.DynamicInvoke(((item as MethodCallExpression)?.Arguments[1]));
if (this.Context.SugarContext.QueryBuilder.QueryableFormats == null)
this.Context.SugarContext.QueryBuilder.QueryableFormats = new List<QueryableFormat>();
this.Context.SugarContext.QueryBuilder.QueryableFormats.Add(new QueryableFormat()
{
Format = "",
PropertyName = asName,
MethodName = "OnlyInSelectConvertToString",
MethodInfo= (MethodInfo)methodInfo
});
parameter.Context.Result.Append(this.Context.GetAsString2(asName, name));
}
}
}

View File

@ -18,6 +18,11 @@ namespace SqlSugar
parameter.Context.Result.Append(this.Context.GetAsString2(asName, this.Context.DbMehtods.NewUid(null)));
return;
}
else if (ExpressionTool.GetMethodName(item) == "OnlyInSelectConvertToString")
{
AppendOnlyInSelectConvertToString(parameter, item, asName);
return;
}
else if (ExpressionTool.GetMethodName(item) == "ToString"
&&(item as MethodCallExpression)?.Arguments?.Count()==1
&& (item as MethodCallExpression)?.Object?.Type!=UtilConstants.DateType

View File

@ -2,7 +2,7 @@
<package >
<metadata>
<id>SqlSugarCore</id>
<version>5.1.4.111</version>
<version>5.1.4.113-preview28</version>
<authors>sunkaixuan</authors>
<owners>果糖大数据科技</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>

View File

@ -135,9 +135,25 @@ namespace SqlSugar
{
addValue = Convert.ToDouble(addValue + "").ToString(valueFomatInfo.Format);
}
else if (valueFomatInfo.TypeString == "Enum")
else if (valueFomatInfo.TypeString == "Enum")
{
addValue =ChangeType2( addValue,valueFomatInfo.Type)?.ToString();
addValue =ChangeType2(addValue, valueFomatInfo.Type)?.ToString();
}
}
else if (valueFomatInfo.MethodName== "OnlyInSelectConvertToString")
{
var methodInfo = valueFomatInfo.MethodInfo;
if (methodInfo != null)
{
// 如果方法是静态的传递null作为第一个参数否则传递类的实例
object instance = methodInfo.IsStatic ? null : Activator.CreateInstance(methodInfo.ReflectedType);
// 创建一个包含参数值的object数组
object[] parameters = new object[] { addValue };
// 调用方法
addValue=methodInfo.Invoke(instance, parameters);
}
}
return addValue;