首页 > 解决方案 > C#:有没有办法让 T 更短的通用约束?

问题描述

我有一个方法看起来像:

void Do<T>(T arg) where T: IDisposeable, ICloneable, IRunnable, IMyInterface1;

为了使通用约束更短,我尝试使用新界面

interface ICombinedInterface: IDisposeable, ICloneable, IRunnable, IMyInterface1 {}

void Do<T>(T arg) where T: ICombinedInterface;

但是,一些第三方库没有也不会实现我的接口,所以我不能在我的Do<T>(T arg)方法中使用他们的类。

反正有没有使通用约束更短?

标签: c#generics

解决方案


推荐阅读