sqlite-net
sqlite-net copied to clipboard
How to work with Abstract types
Hello, I have multiple types like this
var type = Assembly
.Load("App.Core")
.GetTypes()
.FirstOrDefault(
t =>
t.Name.Equals(
"some name",
StringComparison.InvariantCultureIgnoreCase
)
);
how to make them work with something like Connection.DeleteAllAsync<type>(); ?
This is not working:
public async Task ClearTableAsync<T>()
{
var connection = new SQLiteAsyncConnection("your_database_path");
var items = await connection.Table<T>().ToListAsync();
foreach (var item in items)
{
await connection.DeleteAsync(item);
}
}
this is working
var clearMethod = typeof(SQLiteAsyncConnection).GetMethods()
.Where(m => m.Name == "DropTableAsync" && m.IsGenericMethodDefinition)
.Select(m => new { M = m, P = m.GetParameters(), A = m.GetGenericArguments() })
.Where(x => x.P.Length == 0 && x.A.Length == 1)
.Select(x => x.M)
.First()
.MakeGenericMethod(type);
await (Task<int>)clearMethod.Invoke(Connection, null);
Some one have better solutions? I really don't like System.Reflection