繁体   English   中英

如何在另一个通用基类上添加C#泛型类型约束?

[英]How to add a C# generic type constraint on another generic base class?

我已多次阅读有关C#泛型类型参数约束的MSDN文档,但我无法弄清楚如何执行此操作,或确定它是否可行。

假设我有一个这样的通用基类:

public abstract class Entity<TId> { ... }

这个抽象基类没有任何类型约束, TId可以是任何东西 - 结构,类等。

现在说我有一个通用的接口方法,我想将方法​​的泛型类型约束到上面的类:

public interface ICommandEntities
{
    void Update<TEntity>(TEntity entity) where TEntity : ?????;
}

我可以编译这个:

public interface ICommandEntities
{
    void Update<TEntity, TId>(TEntity entity) where TEntity: Entity<TId>
}

...然而,我需要在执行方法时显式添加两个T1 ant T2泛型args:

commander.Update<AbcEntity, string>(abcEntity);

如果可能的话,我想让编译器推断一切,这样我就可以执行这样的方法:

commander.Update(abcEntity);

这个活动可能吗? 到目前为止,我可以使它工作的唯一方法是在泛型基类之上添加一个空的非泛型基类,并将其用作方法的类型约束:

public abstract Entity {}

public abstract EntityWithId<TId> : Entity { ... }

public interface ICommandEntities
{
    void Update<TEntity>(TEntity entity) where TEntity : Entity;
}

commander.Update(abcEntity);

...但最终我得到一个非常无用的类,充当标记界面。 这是摆脱这种泛型类和接口方法设计的唯一方法吗? 或者我在这里遗漏了什么?

在检查它编译后,我会将其升级为答案。

从您的问题和评论中,您希望参数为Entity<Something> 您不需要将参数化类型直接用作类型,它可用于参数化参数。

所以就这么做

 public void Update(Entity<T1> entity) where ....

简单的选择是更改ICommandEntities的签名:

public interface ICommandEntities
{
    void Update<TId>(Entity<TId> entity)
}

这有效地提供了您所追求的相同约束。

如注释中所述,您应该只创建参数类型BaseClass<T>

class Program
{
    static void Main( string[] args )
    {
        ITest x = new TestClass();

        Console.WriteLine( x.GetTypeArgTypeFrom( new BaseClass<int>() ) );

        Console.ReadKey();

    }
}

public class BaseClass<T>
{
    public Type GetTypeArgType()
    {
        return typeof( T );
    }
}

public interface ITest
{
    Type GetTypeArgTypeFrom<T>( BaseClass<T> bct );
}

public class TestClass : ITest
{
    public Type GetTypeArgTypeFrom<T>( BaseClass<T> bct )
    {
        return bct.GetTypeArgType();
    }
}

暂无
暂无

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

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