Update db.InsertableByObject

This commit is contained in:
sunkaixuan 2022-12-26 11:22:44 +08:00
parent 85fdec957d
commit 42f1995e58
2 changed files with 57 additions and 3 deletions

View File

@ -1,10 +1,23 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace SqlSugar
{
public class InsertMethodInfo
{
internal SqlSugarProvider Context { get; set; }
internal MethodInfo MethodInfo { get; set; }
internal object objectValue { get; set; }
public int ExecuteCommand()
{
if (Context == null) return 0;
var inertable=MethodInfo.Invoke(Context, new object[] { objectValue });
var result= inertable.GetType().GetMethod("ExecuteCommand").Invoke(inertable,new object[] { });
return (int)result;
}
}
}

View File

@ -675,10 +675,51 @@ namespace SqlSugar
#endregion
#region Insertable
public InsertMethodInfo InsertableByObject(object singleEntityObjectOrListObject)
public InsertMethodInfo InsertableByObject(object singleEntityObjectOrListObject)
{
InsertMethodInfo result = new InsertMethodInfo();
return result;
if (singleEntityObjectOrListObject == null)
return new InsertMethodInfo();
if (singleEntityObjectOrListObject.GetType().FullName.IsCollectionsList())
{
var list = ((IList)singleEntityObjectOrListObject);
if (list == null || list.Count == 0)
return new InsertMethodInfo();
var type = list[0].GetType();
var newList = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
foreach (var item in list)
{
newList.Add(item);
}
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Insertable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name.StartsWith("List")))
.Where(it => it.Name == "Insertable").ToList();
var method = methods.Single().MakeGenericMethod(newList.GetType().GetGenericArguments().First());
InsertMethodInfo result = new InsertMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = newList
};
return result;
}
else
{
var methods = this.Context.GetType().GetMethods()
.Where(it => it.Name == "Insertable")
.Where(it => it.GetGenericArguments().Any())
.Where(it => it.GetParameters().Any(z => z.ParameterType.Name == "T"))
.Where(it => it.Name == "Insertable").ToList();
var method = methods.Single().MakeGenericMethod(singleEntityObjectOrListObject.GetType());
InsertMethodInfo result = new InsertMethodInfo()
{
Context = this.Context,
MethodInfo = method,
objectValue = singleEntityObjectOrListObject
};
return result;
}
}
public virtual IInsertable<T> Insertable<T>(T[] insertObjs) where T : class, new()
{