简体   繁体   English

C#泛型装箱错误

[英]C# Generics boxing errors

I have the following code that worked before I changed to use generics for the service: 在更改为对服务使用泛型之前,我有下面的代码可以工作:

Update: I added some more class and interface info in response to comments: 更新:为了回应评论,我添加了更多的类和接口信息:

public class Service<T1,T2> : BaseService, IService<T1>
        where T1 : IAuditableTable
        where T2 : IAuditableTable
{
    private IAzureTable<T1> _T1repository;
    private IAzureTable<T2> _T2repository;

    public Service(string ds)
    {
        base.Initialize(ds);
        _T1repository = StorageHelper.GetTable<T1>(ds);
        _T2repository = StorageHelper.GetTable<T2>(ds);
    }

    public IEnumerable<AdminDetail> ShowDetails()
    {
        return base.ShowDetails(_T1repository, _T2repository);
    } 

    ...

and

public IEnumerable<AdminSummary> ShowSummary<T1, T2>(
        IAzureTable<T1> master, IAzureTable<T2> detail)
    where T1 : AuditableTable
    where T2 : AuditableTable
{
    ...

public abstract class AuditableTable : TableServiceEntity, IAuditableTable  
{
    ...

public interface IAzureTable<T> : IInitializer

public interface IService<T>
        where T : IAuditableTable
    {
        IEnumerable<AdminSummary> ShowSummary();
    }

private IService<Account> _account;
vm.AdminSummaries = _account.ShowSummary(); << calls the report

When I try to compile I get the following message: 当我尝试编译时,出现以下消息:

The type 'T2' cannot be used as type parameter 'T2' in the generic type or method 'Services.BaseService.ShowSummary<T1,T2>(AzureToolkit.IAzureTable<T1>, AzureToolkit.IAzureTable<T2>)'. 在通用类型或方法“ Services.BaseService.ShowSummary <T1,T2>(AzureToolkit.IAzureTable <T1>,AzureToolkit.IAzureTable <T2>)”中,类型“ T2”不能用作类型参数“ T2”。 There is no boxing conversion or type parameter conversion from 'T2' to 'Storage.Models.AuditableTable'. 没有从“ T2”到“ Storage.Models.AuditableTable”的装箱转换或类型参数转换。

The ShowSummary needs types of AuditableTable (which does implement IAuditableTable ). ShowSummary需要类型的AuditableTable (它确实实现了IAuditableTable )。 You supply values of just IAuditableTable where it needs the concrete class. 您仅在需要具体类的地方提供IAuditableTable值。 The other way around (concrete implementation in parameter of interface type) would probably work. 另一种方法(在接口类型的参数中具体实现)可能会起作用。

您可能需要对T2class约束。

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

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