mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-10-15 18:55:07 +08:00
Update db.InsertableByObject
This commit is contained in:
@@ -1,10 +1,23 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace SqlSugar
|
namespace SqlSugar
|
||||||
{
|
{
|
||||||
public class InsertMethodInfo
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -675,10 +675,51 @@ namespace SqlSugar
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Insertable
|
#region Insertable
|
||||||
public InsertMethodInfo InsertableByObject(object singleEntityObjectOrListObject)
|
public InsertMethodInfo InsertableByObject(object singleEntityObjectOrListObject)
|
||||||
{
|
{
|
||||||
InsertMethodInfo result = new InsertMethodInfo();
|
if (singleEntityObjectOrListObject == null)
|
||||||
return result;
|
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()
|
public virtual IInsertable<T> Insertable<T>(T[] insertObjs) where T : class, new()
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user