我想知道在定义表时是否有使用泛型类型的方法?
DefineTable方法使您的模型在移动电话上定义本地表。使用尖括号指定模型类型。但是,我希望传递一个Type对象并使用它来定义表,但是,你不能在尖括号内使用变量。
下面是代码示例以及我正在寻找的更多解释......
这是目前正在实施的方式:
public async Task Init()
{
this.initialized = true;
const string Path = "syncorder.db";
var store = new MobileServiceSQLiteStore(Path);
store.DefineTable<Order>();
}
这是为了了解我想要实现的目标:
public async Task Init(Type tableType)
{
this.initialized = true;
const string Path = "syncorder.db";
var store = new MobileServiceSQLiteStore(Path);
//This is what i would like to do, but cant since its not possible to put variables inside angular brackets
store.DefineTable<tableType>();
}
我还尝试检查是否可以将Type对象作为参数传递给DefineTable方法(因为在某些情况下这是可能的)但是没有重载允许这样做。
//This is not possible...
store.DefineTable(tableType);
无论如何,所有的帮助都表示赞赏。谢谢!
答案 0 :(得分:1)
所以我想出了如何做到这一点,以防任何人遇到这个问题是下面的解决方案:
public Task Init(Type tableType)
{
try
{
this.initialized = true;
const string Path = "syncorder.db";
var store = new MobileServiceSQLiteStore(Path);
MethodInfo myDefineTable = RuntimeReflectionExtensions.GetRuntimeMethod(typeof(MobileServiceSQLiteStoreExtensions), "DefineTable", new Type[] { typeof(MobileServiceSQLiteStore) });
myDefineTable = myDefineTable.MakeGenericMethod(tableType);
myDefineTable.Invoke(null, new object[] { store });
for the parameters to pass into the method
}
catch (Exception ex)
{
this.initialized = false;
throw ex;
}
}
希望这有助于某人......干杯!
答案 1 :(得分:0)
我认为您可以执行以下操作:
public IMobileServiceSyncTable<T> Init<T>()
{
var store = new MobileServiceSQLiteStore(DatabaseName);
store.DefineTable<T>();
var Client = Client = new MobileServiceClient(SystemOptions.AzureStorage);
Client.SyncContext.InitializeAsync(store);
return Client.GetSyncTable<T>();
}