Add db.GetRepository

This commit is contained in:
sunkaixuan
2022-10-21 11:56:12 +08:00
parent 97e4dc6ce3
commit 9a113c6292
2 changed files with 30 additions and 1 deletions

View File

@@ -38,7 +38,16 @@ namespace SqlSugar
public RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository
{
Type type = typeof(RepositoryType);
object o = Activator.CreateInstance(type,new string[]{ null});
var isAnyParamter = type.GetConstructors().Any(z => z.GetParameters().Any());
object o = null;
if (isAnyParamter)
{
o=Activator.CreateInstance(type, new string[] { null });
}
else
{
o = Activator.CreateInstance(type);
}
var result= (RepositoryType)o;
if (result.Context == null)
{

View File

@@ -136,6 +136,26 @@ namespace SqlSugar
{
return this.Context.GetSimpleClient<T>();
}
public RepositoryType GetRepository<RepositoryType>() where RepositoryType : ISugarRepository , new()
{
Type type = typeof(RepositoryType);
var isAnyParamter = type.GetConstructors().Any(z => z.GetParameters().Any());
object o = null;
if (isAnyParamter)
{
o = Activator.CreateInstance(type, new string[] { null });
}
else
{
o = Activator.CreateInstance(type);
}
var result = (RepositoryType)o;
if (result.Context == null)
{
result.Context = this.Context;
}
return result;
}
#endregion
#region Insertable