Tag: 约束

为什么带有“where T:class”约束的Generic 方法接受接口

我有这个interface : public interface ITestInterface { int TestInt { get; set; } } 和这个generics方法(带有T : class约束): public void Test() where T : class { // DoSomething } 这个电话: Test(); 当interface 不是一个class (或者它是什么?)时,一切都编译并运行。 为什么会这样? 我第一次在我的WCF代理类上看到这个: public partial class TestServiceClient: System.ServiceModel.ClientBase, TestNamespace.ITestService ClientBase具有此定义: public abstract class ClientBase : ICommunicationObject, IDisposable where TChannel : class

如何指定.NET Generics约束中不允许的类型?

是否可以在不允许某些类型的generics类上指定约束? 我不知道是否可能,如果是,我不确定语法是什么。 就像是: public class Blah where : !string { } 我似乎无法找到任何允许这种约束的符号。

类型参数“T”与外部类型“…”中的类型参数同名

public abstract class EntityBase { … } public interface IFoobar { void Foo(int x) where T : EntityBase, new(); } public interface IFoobar where T : EntityBase, new() { void Foo(int x); } public class Foobar : IFoobar, IFoobar where T : EntityBase, new() { public void Foo(int x) { … } void IFoobar.Foo(int x) […]

C#类型转换错误,尽管通用约束

为什么,对于类P的类型参数T的“必须从Ainheritance”的通用约束,第一次调用成功但第二次调用失败并且注释中详细说明了类型转换错误: abstract class A { } static class S { public static void DoFirst(A argument) { } public static void DoSecond(ICollection argument) { } } static class P where T : A, new() { static void Do() { S.DoFirst(new T()); // this call is OK S.DoSecond(new List()); // this call won’t compile with: /* cannot […]

为什么带约束的generics扩展方法不被识别为扩展方法?

可能重复: 没有使用generics扩展方法的类型推断 考虑两种方法: public static IEnumerable Merge (this IEnumerable<IEnumerable> coll) public static IEnumerable Merge (this IEnumerable coll) where T : IEnumerable 两者都编译得很好,在这两种情况下,generics类型的类型将在调用者的编译时知道,因此是扩展类型的确切类型。 你可以调用两个,但只有第一个作为扩展名。 为什么? 更新1 要查看它失败,请使用第二种方法,例如: var x = new List<List>(); var y = x.Merge(); 更新 – 关闭 难道你们不认为原帖是太精心设计得到清晰的画面吗? 出于教育目的,我认为这篇文章不应该被关闭,即使技术上(即答案)是重复的。 只需2美分。

如何使用约束将T限制为值类型?

我想限制N可以使用约束可以采用的类型。 我希望将N限制为int或decimal。 public static Chart PopulateInto(List yAxis, List xAxis) where N : int, decimal { // Do stuff here } 任何帮助表示…

为什么接口不起作用,而抽象类却使用generics类约束?

下面的代码显示了一个带有类型约束的通用类( Pub )。 该类有一个可以引发的事件,允许我们向订阅者传递消息。 约束是消息必须实现IMsg (或者当它是抽象类时从IMsginheritance)。 Pub还提供了一个Subscribe方法,当且仅当对象实现了IHandler ,才允许对象订阅notify事件。 使用.NET 4,下面的代码显示baseImplementer.NotifyEventHandler上的错误,指出: “No overload for ‘IHandler.NotifyEventHandler(IMsg)’ matches delegate ‘System.Action'” 问题:(使用更新的订阅方法) 一旦我将`IMsg`更改为抽象类而不是接口,为什么错误会消失? public interface IMsg { } // Doesn’t work //public abstract class IMsg { } // Does work public class Msg : IMsg { } public class Pub where T : IMsg { public event Action notify; […]

entity framework中唯一字段的选项 – dbSet的导航属性?

经过大量研究后,似乎Entity Framework 4.4不支持Unique约束。 是的,它可以并且应该在数据库中完成,但我更喜欢它在模型validation中发生,因此对用户的警告更漂亮。 对于程序员来说,能够使用[Unique]属性来装饰属性是理想的,并且应该可以某种方式,例如: public class UserGroup { public int UserGroupID { get; set; } [Required] [Unique] public string Name { get; set; } [Required] public string Description { get; set; } } 我正在考虑的选项: 1)让存储库在SaveChanges()中执行一些额外的工作,扫描已修改实体的[Unique]属性并命中数据库以检查唯一性。 缺点:此validation仅在我们调用SaveChanges()时发生,理想情况下它可以更早发生(例如,当UI控件validation时)。 2)为UserGroup模型提供一个延迟加载的导航属性给AllUserGroups: public virtual ICollection AllUserGroups { get; set; } 然后编程UniqueAttribute {}来扫描此属性并检查值等。 问题 :如何配置Entity Framework(代码优先)将所有记录加载到此“导航属性”中? 它似乎只想要一个带有外键等的导航属性,而我只想要它们。 3)在UI中手动编写此validation – 可怕且绝对的最后手段。 […]

为什么我们不能使用密封类作为通用约束?

你能猜出在generics中不允许使用密封类进行类型约束的原因是什么? 我只有一个解释是给机会使用裸约束。

C#generics约束

是否可以枚举通用约束中“可用”的类型? T MyMethod() where T : int, double, string 为什么我要这样做是因为我有一个小的评估引擎,并且想编写这样的代码: bool expression.Evaluate(); 要么 int expression.Evaluate(); 但我想禁止 MyCustomClass expression.Evalaute();