I would like to get a Sqlite Table
attribute from my class.
I have a method to check if a table exists from here :
var info = database.Connection.GetTableInfo(typeof(Customer).Name);
if (info.Any())
{
//do stuff with table
}
where Customer
is:
[Table("Customer")]
public class Customer
{
//class stuff
}
Now my method will work fine as it is but I would like to link it to the Table
attribute rather than the class name incase I change the table name in future.
How do I go about getting my Table
attribute from my class?
Ps. I am using a PCL (Portable Class Library)
Got it. Just needed to use the GetCustomAttributes method on a Type
and feed in the Attribute type I was looking for. So it became:
string tableName = typeof(Customer).Name;
var customAttributes = typeof(Customer).GetCustomAttributes(typeof(SQLite.Net.Attributes.TableAttribute),false);
if (customAttributes.Count() > 0)
{
tableName = (customAttributes.First() as SQLite.Net.Attributes.TableAttribute).Name;
}
Here is the solution for a PCL:
using System.Reflection;
string tableName = typeof(MyClass).Name;
var customAttributes = typeof(MyClass).GetTypeInfo().GetCustomAttributes<SQLite.Net.Attributes.TableAttribute>();
if(customAttributes.Count() > 0)
{
tableName = customAttributes.First().Name;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.