Tag: 扩展方法

现有的LINQ扩展方法类似于Parallel.For?

可能重复: LINQ相当于foren for IEnumerable forienumerable的linq扩展方法非常方便……但如果您只想对枚举中的每个项目应用一些计算而不返回任何内容,则无用。 所以我想知道我是否只是错过了正确的方法,或者它是否真的不存在,因为我宁愿使用内置版本,如果它可用……但我还没找到一个:-) 我可以发誓在某处有一个.ForEach方法,但我还没有找到它。 与此同时,我确实编写了自己的版本,以防它对其他人有用: using System.Collections; using System.Collections.Generic; public delegate void Function(T item); public delegate void Function(object item); public static class EnumerableExtensions { public static void For(this IEnumerable enumerable, Function func) { foreach (object item in enumerable) { func(item); } } public static void For(this IEnumerable enumerable, Function func) { foreach […]

C#中的通用Map / Reduce List Extensions

我正在编写一些扩展来模仿地图并减少Lisp中的函数。 public delegate R ReduceFunction(T t, R previous); public delegate void TransformFunction(T t, params object[] args); public static R Reduce(this List list, ReduceFunction r, R initial) { var aggregate = initial; foreach(var t in list) aggregate = r(t,aggregate); return aggregate; } public static void Transform(this List list, TransformFunction f, params object [] args) { foreach(var […]

如何用reflection调用generics扩展方法?

我写了扩展方法GenericExtension 。 现在我想调用扩展方法Extension 。 但methodInfo的值始终为null。 public static class MyClass { public static void GenericExtension(this Form a, string b) where T : Form { // code… } public static void Extension(this Form a, string b, Type c) { MethodInfo methodInfo = typeof(Form).GetMethod(“GenericExtension”, new[] { typeof(string) }); MethodInfo methodInfoGeneric = methodInfo.MakeGenericMethod(new[] { c }); methodInfoGeneric.Invoke(a, new object[] […]

c#中密封类的扩展方法

我有这个sealed课, public sealed class A { public string AName {get;set;} } 有人可以这样写一个扩展方法: public static class Extensions { public static void ExtensionMethodForA (this A a) { Console.WriteLine(“A’s Extension method!”); } } 问题是,你如何防止这种情况?

如何用reflection调用List 的扩展方法“ElementAt”?

我有问题,在创建List 类型的对象“oListType01”之后,在将其分配给另一个类型为“object”的objet“oObjectType”后,我无法访问任何更多的函数“ElementAt(1)”。 我尝试使用reflection,但我总是在“调用”方法中获得exception(参数冲突)。 有谁知道为什么? 米兰 MyClass01 oMy1 = new MyClass01(); oMy1._ID = “1”; MyClass01 oMy2 = new MyClass01(); oMy2._ID = “3”; IList oListType01 = new List(); oListType01.Add(oMy1); oListType01.Add(oMy2); object oObjectType = new object(); oObjectType = oListType01; 从这里fowrads只有对象oObjectType可用(在实际情况下向上发生在单独的函数调用中)。 在VS oObjectType中显示了我想要反映的两个元素。 MethodInfo mInfo = typeof(System.Linq.Enumerable).GetMethod(“ElementAt”).MakeGenericMethod(typeof(object)); object oSingleObject = mInfo.Invoke(oObjectType, new object[] { 1 });

如果扩展方法与密封类中的方法具有相同的签名,那么调用优先级是什么?

我正在阅读C#3.0中的扩展方法。 我正在阅读的文本暗示一个扩展方法与扩展类中的方法具有相同的签名将是执行顺序的第二个 – 也就是说,密封类中的方法被调用。 如果是这种情况,你怎么能延长密封class?

在.NET Framework 2.0中使用扩展方法

在Visual Studio 2008下 我可以创建一个扩展方法在.NET Framework 2.0项目下工作吗?

你如何为一般类型的类编写C#扩展方法

这应该是一个简单的。 我想在System.Web.Mvc.ViewPage 类中添加一个扩展方法。 这个扩展方法应该怎么样? 我的第一个直觉思想是这样的: namespace System.Web.Mvc { public static class ViewPageExtensions { public static string GetDefaultPageTitle(this ViewPage v) { return “”; } } } 解 一般的解决方案是这个答案 。 扩展System.Web.Mvc.ViewPage类的具体解决方案是我的答案 ,从一般解决方案开始。 不同之处在于,在特定情况下,您需要一般类型化的方法声明和一个声明来强制将generics类型作为引用类型。

是否可以在C#中扩展数组?

我习惯于向IEnumerable等外部类添加方法。 但是我们可以在C#中扩展数组吗? 我打算为数组添加一个方法,即使它是多维的,也可以将它转换为IEnumerable。 与如何在C#中扩展数组无关

扩展接口模式

.Net 3.5中的新扩展允许从接口拆分function。 例如在.Net 2.0中 public interface IHaveChildren { string ParentType { get; } int ParentId { get; } List GetChildren() } 可以(在3.5中)成为: public interface IHaveChildren { string ParentType { get; } int ParentId { get; } } public static class HaveChildrenExtension { public static List GetChildren( this IHaveChildren ) { //logic to get children by […]