Tag: generics

通用约束忽略协方差

假设我们有一个类似的界面 public interface IEnumerable { /*…*/ } 这是T 共变体 。 然后我们有另一个接口和一个实现它的类: public interface ISomeInterface {} public class SomeClass : ISomeInterface {} 现在,协方差允许我们执行以下操作 IEnumerable e = Enumerable.Empty(); 因此, IEnumerable 可分配给IEnumerable类型的变量(或方法参数)。 但是如果我们在通用方法中尝试这个: public void GenericMethod(IEnumerable p) where T : ISomeInterface { IEnumerable e = p; // or TestMethod(p); } public void TestMethod(IEnumerable x) {} 我们得到编译器错误CS0266告诉我们IEnumerable无法转换为IEnumerable 。 约束清楚地表明T是从ISomeInterface派生的,并且由于IEnumerable在T是共变量,因此该赋值应该起作用(如上所示)。 […]

比较实现IComparable的项目的问题

我正在研究一种扩展方法,它通过特定的选择器找到最小项目。 代码下面 public static T MinBy(this IEnumerable src, Func selector) where K : struct, IComparable, IConvertible { var min = default(K); T minItem = default(T); foreach (var item in src) { var current = selector(item); if (current < min) { min = current; minItem = item; } } return minItem; } 它给出错误Error Operator ‘<' cannot […]

为什么ICollection 同时实现IEnumerable 和IEnumerable

为什么ICollection实现IEnumerable和IEnumerable ? 这样做的目的是什么? IEnumerable如何使ICollection受益?

TryParse Nullable类型通常

我为以下Nullable类型编写了重载的静态TryParse方法: int? , short? long? , double? , DateTime? , decimal? , float? , bool? , byte? 和char? 。 以下是一些实现: protected static bool TryParse(string input, out int? value) { int outValue; bool result = Int32.TryParse(input, out outValue); value = outValue; return result; } protected static bool TryParse(string input, out short? value) { short outValue; bool […]

generics指针?

好吧所以我想创建一个将改变数据类型值的generics类。 我想这样做的原因是我可以使用undo和redo方法。 我可以为我需要的每种值类型编写一个类。 IE double,int …但如果我可以创建一个通用类来做这件事会容易得多。 这就是我所拥有的 class CommandChangeDouble : Command { double _previous; double _new; double* _objectRef; public unsafe CommandChangeDouble(double* o, double to) { _objectRef = o; _previous = *o; _new = to; *_objectRef = _new; } public unsafe void Undo() { *_objectRef = _previous; } public unsafe void Redo() { *_objectRef = _new; […]

如何获取MethodInfo的通用扩展方法?

我有一个IEnumerable ,我想通过reflection调用Enumerable.Contains方法。 我只是在努力使语法正确。 这是我现在拥有的: var containsMethod = typeof(Enumerable).GetMethod(“Contains”, new[] { typeof(IEnumerable), typeof(T) }); 这只是返回null。 获取MethodInfo的正确方法是什么?

为什么会导致CS0695?

public interface PipelineElement { IEnumerable Run(IEnumerable input, Action errorReporter); } public interface Stage { } public abstract class PipelineElementBase : PipelineElement, PipelineElement where TIn : Stage where TOut : Stage { IEnumerable PipelineElement.Run(IEnumerable input, Action errorReporter) { return this.Run(input.Cast(), errorReporter).Cast(); } public abstract IEnumerable Run(IEnumerable input, Action errorReporter); } object没有实现Stage ,因此TIn和TOut都不能成为object ,对吧? 那么为什么编译器认为PipelineElement和PipelineElement可以变得相同? 编辑:是的,完全可以多次实现相同的通用接口: public […]

将list 转换为list

如何在C#中将List转换为List ?

inheritanceWCF中的通用契约

更多WCF问题…… 🙂 我的所有工作流程都实现了相同的3种方法。 经过大量的复制和粘贴后,我决定让它们从同一个界面inheritance: [ServiceContract(Namespace = “http://schema.company.com/messages/”)] public interface IBasicContract where TRequest : class where TResponse : class { [OperationContract(Name = “GetReport”, Action = “http://schema.company.com/messages/GetReport”, ReplyAction = “http://schema.company.com/messages/GetReportResponse”)] TResponse GetReport(TRequest inquiry); [OperationContract(Name = “GetRawReport”, Action = “http://schema.company.com/messages/GetRawReport”, ReplyAction = “http://schema.company.com/messages/GetRawReportResponse”)] string GetRawReport(string guid); [OperationContract(Name = “GetArchiveReport”, Action = “http://schema.company.com/messages/GetArchiveReport”, ReplyAction = “http://schema.company.com/messages/GetArchiveReportResponse”)] TResponse GetArchiveReport(string guid); […]

运算符as和generics类

我正在为CLR脚本编写.NET On-the-Fly编译器,并希望执行方法使得generics可接受: object Execute() { return type.InvokeMember(..); } T Execute() { return Execute() as T; /* doesn’t work: The type parameter ‘T’ cannot be used with the ‘as’ operator because it does not have a class type constraint nor a ‘class’ constraint */ // also neither typeof(T) not T.GetType(), so on are possible return (T) […]