Tag: 扩展方法

如何覆盖ToString()并实现generics?

我有代码,我想做出以下更改: 如何覆盖ToString()? 它说:静态成员… ToString(System.Collections.Generic.List)’不能标记为覆盖,虚拟或抽象。 我如何使它通用? public static override string ToString(this List list) { string output = “”; list.ForEach(item => output += item.ToString() + “,” ); return output; } 谢谢!

在扩展方法中更改数组的大小不起作用?

所以基本上我为数组类型编写了一个小的Add扩展方法。 using System; using System.Linq; public static class Extensions { public static void Add(this T[] _self, T item) { _self = _self.Concat(new T[] { item }).ToArray(); } } public class Program { public static void Main() { string[] test = { “Hello” }; test = test.Concat(new string[] { “cruel” }).ToArray(); test.Add(“but funny”); Console.WriteLine(String.Join(” “, test) […]

generics的扩展方法

我正在看这个问题,我很好奇为什么这个不能编译。 鉴于此代码,任何人都可以解释为什么对IBase.Test()的调用没有解析为正确的扩展方法? public interface IBase { } public interface IChildA : IBase { } public interface IChildB : IBase { } public static class BaseExtensions { public static IBase Test(this IBase self) { return self; } public static T Test(this T self) where T : IChildB { return self; } } public static class TestClass […]

为什么带约束的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美分。

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

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

C#扩展方法优先级

我对扩展方法的工作原理有点困惑。 如果我正确地阅读这个http://msdn.microsoft.com/en-us/library/bb383977.aspx并且这个如果扩展方法与密封类中的方法具有相同的签名,那么调用优先级是什么? 。 然后以下应该写出“实例”,而是写出“扩展方法”。 interface IFoo { } class Foo : IFoo { public void Say() { Console.WriteLine(“Instance”); } } static class FooExts { public static void Say(this IFoo foo) { Console.WriteLine(“Extension method”); } } class Program { static void Main(string[] args) { IFoo foo = new Foo(); foo.Say(); } } 澄清澄清行为的任何帮助。

是否可以定义扩展运算符方法?

是否可以定义一个同时是运算符的扩展方法? 我想要一个固定的类添加使用实际上无法应用的已知运算符的可能性。 对于这种特殊情况,我想这样做: somestring++; //i really know that this string contains a numeric value 我不想为所有代码传播类型转换。 我知道我可以在字符串上创建包装类并定义该运算符,但我想知道是否可以使用MySpecialString来避免搜索和替换每个字符串声明。 编辑:因为大多数人说字符串是密封的,所以推导是不可能的,所以我修改“派生”到“包装”,我的错误。

命名空间’System.Windows’中不存在类型或命名空间名称’Window’

我正在尝试为WPF Window类编写扩展方法。 我在我的解决方案中的类库项目中这样做,以便我可以在解决方案中的所有项目中使用它。 这是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace ExtensionMethods { public static class MyExtensions { public static void ResizeToPrimaryScreenWorkingArea(this System.Windows.Window w) { } } } 当我尝试编译它时,我收到以下错误: 命名空间’System.Windows’中不存在类型或命名空间名称’Window’ 是的,我在我的类库项目中添加了对System.Windows和System.Windows.Forms的引用,它们显示在解决方案资源管理器中项目的引用下。 我究竟做错了什么?

将简单绑定的解决方案扩展到’Text属性到多个控件以处理绑定到任何类型?

我的问题是 :如何超越编写一种技术的自定义实现, 以便为每种可能的数据类型将多个控件(没有内置DataSource属性的控件)数据绑定到简单属性…… 如下面的代码所述和演示 …以实现更强大的解决方案,该解决方案将独立于绑定是字符串,int还是其他类型。 我的猜测是:这将涉及反思; 但是,我被困在那一点上。 我正在寻找关于哪个“方向”下一步移动的战略建议,提示,线索,而不是完整的代码答案,但当然我感谢所有回复,如果你发布代码回复,我肯定会学习代码! Marc Clifton 2005年关于CodeProject 简单数据绑定的文章似乎展示了一种基于reflection的方法:但老实说,我并没有真正理解他的代码,而且就.NET来说,2005年是很久以前的。 背景:部分响应各种SO问题和答案,例如: 更新三个表单上的用户控件 :我已经发展出一种成功的技术,可以将各种控件的文本属性同时绑定到Public类中定义的一个源; 还能够使用定义一个扩展方法的静态类和两个公共方法“抽象”绑定过程的一些细节。 我已经validation了MainForm上的控件上的TextBoxes,MainForm上UserControl上的TextBoxes,以及第二个Form上的TextBox“独立”打开(即form2.Parent == null)所有更新都正确(即两个) -way binding实际上来自“DataSource等效”公共类。 改变一:改变一切。 代码:此类的实例将提供数据绑定的目标属性(theText): public class TextDataBinder { public event PropertyChangedEventHandler PropertyChanged; private string _theText; public string theText { get { return _theText; } // note : if ‘setter is declared ‘internal : blocks // auto-updating […]

我怎么知道枚举中的物品?

在这个问题中 ,我在enum与[Flags]属性之间使用xor运算符如下: [Flags] enum QueryFlag { None = 0x1, ByCustomer = 0x2, ByProduct = 0x4, ByDate = 0x8 } QueryFlag flags = QueryFlag.ByCustomer | QueryFlag.ByProduct; 要添加QueryFlag,我们当然应该使用| 运营商。 flags |= QueryFlag.ByDate; 为了删除一个,我对丹涛的回答有不同的看法 。 我正在使用: flags ^= QueryFlag.ByProduct; 他正在使用: flags &= ~QueryFlag.ByProduct; 显然他的回答是正确的,易于理解。 我以为我弄错了。 但经过深思熟虑,我得到了: a,ba^b a&(~b) 0,0 0 0 0,1 1 0 //the difference 1,0 1 […]