简体   繁体   English

C#泛型滥用?

[英]C# Generics abuse?

I can't figure out why I am getting the error cannot be used as type parameter 't' in the generic type or method...There is no implicit reference conversion from... 我无法弄清楚为什么我得到的错误cannot be used as type parameter 't' in the generic type or method...There is no implicit reference conversion from...

Here is my entire piece of code (simplified): 这是我的整段代码(简化):

The problem is on the line RefreshLocalListFromLocalStore(TestDataObjectTable); 问题出在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
}

Your generic constraint is probably wrong. 您的通用约束可能是错误的。 Use the following declaration for RefreshLocalListFromLocalStore : RefreshLocalListFromLocalStore使用以下声明:

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

In your declaration, the table should have been a table of tables, which does not make sense. 在你的声明中,表应该是一个表的表,这是没有意义的。 You can use the generics constraints still to limit what kind of elements you want to have for your table, for example if the elements in the table should be entities complying with some IEntity interface. 您仍然可以使用泛型约束来限制您希望为表提供哪种元素,例如,如果表中的元素应该是符合某些IEntity接口的实体。

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

  T must implement IMobileServiceSyncTable<T> 

but it doesn't, because that would be 但事实并非如此,因为那样

  IMobileServiceSyncTable<IMobileServiceSyncTable<TestDataObject>>

Try this instead: 试试这个:

public async void RefreshLocalListFromLocalStore<T>(IMobileServiceSyncTable<T> table) 
{
    ...
}

The problem is you're using T with constraint of Type IMobileServiceSyncTable<T> where T is again of Type IMobileServiceSyncTable<T> . 问题是你使用T类型IMobileServiceSyncTable<T>约束,其中T再次是类型IMobileServiceSyncTable<T>

Use constraints on the type for generic interface 对通用接口的类型使用约束

public async void RefreshLocalListFromLocalStore<T>(IMobileServiceSyncTable<T> table) 
                                                                  where T : TestDataObject
{

}

See specification of using Type constraints. 请参阅使用类型约束的规范

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM