gpt4 book ai didi

C#泛型滥用?

转载 作者:行者123 更新时间:2023-11-30 18:21:03 25 4
gpt4 key购买 nike

我不明白为什么会出现错误 cannot be used as type parameter 't' in the generic type or method...There is no implicit reference conversion from...

这是我的全部代码(已简化):

问题出在RefreshLocalListFromLocalStore(TestDataObjectTable);

这行上
using System;
using Microsoft.WindowsAzure.MobileServices;
using Microsoft.WindowsAzure.MobileServices.SQLiteStore;
using Microsoft.WindowsAzure.MobileServices.Sync;

public class TestDataObject
{
#region Properties

public string Id { get; set; }

#endregion
}

public class TestDatabaseManager
{
public MobileServiceClient Client;

private IMobileServiceSyncTable<TestDataObject> TestDataObjectTable;

public TestDatabaseManager()
{
CurrentPlatform.Init();
Client = new MobileServiceClient("SomeUrl");
var store = new MobileServiceSQLiteStore("SomePath");
store.DefineTable<TestDataObject>();

TestDataObjectTable = Client.GetSyncTable<TestDataObject>();

RefreshLocalListFromLocalStore(TestDataObjectTable);
}

#region Methods

public async void RefreshLocalListFromLocalStore<T>(T table) where T : IMobileServiceSyncTable<T>
{
try
{
await table.ToListAsync();
}
catch (Exception e)
{
}
}

#endregion
}

最佳答案

您的通用约束可能是错误的。对 RefreshLocalListFromLocalStore 使用以下声明:

public async void RefreshLocalListFromLocalStore<T>(IMobileServiceSyncTavble<T> table)

在你的声明中,表格应该是表格的表格,这是没有意义的。您仍然可以使用泛型约束来限制您希望表中的元素类型,例如,如果表中的元素应该是符合某些 IEntity 接口(interface)的实体。

public async void RefreshLocalListFromLocalStore<T>(IMobileServiceSyncTavble<T> table) where T : IEntity

关于C#泛型滥用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com