extension-methods

Why can't I call an extension method from a base class of the extended type‏?

寵の児 提交于 2019-11-27 05:02:14
I'm trying add the ability to lookup elements in a List<KeyValuePair<string,int>> by overriding the indexer. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { public class MyList : List<KeyValuePair<string, int>> { public int this[string key] { get { return base.Single(item => item.Key == key).Value; } } } } For some reason, the compiler is throwing this error: ' System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,int>> ' does not contain a definition for ' Single '.

What is the performance of the Last() extension method for List<T>?

百般思念 提交于 2019-11-27 04:57:34
I really like Last() and would use it all the time for List<T> s. But since it seems to be defined for IEnumerable<T> , I guess it enumerates the enumeration first - this should be O(n) as opposed to O(1) for directly indexing the last element of a List<T> . Are the standard (Linq) extension methods aware of this? The STL in C++ is aware of this by virtue of a whole "inheritance tree" for iterators and whatnot. Martin Harris I just used the Reference Source to look into the code for Last and it checks to see if it is a IList<T> first and performs the appropriate O(1) call: public static

How to call a generic extension method with reflection?

瘦欲@ 提交于 2019-11-27 04:49:42
问题 I wrote the extension method GenericExtension . Now I want to call the extension method Extension . But the value of methodInfo is always null. public static class MyClass { public static void GenericExtension<T>(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 });

Generic extension method resolution fails

青春壹個敷衍的年華 提交于 2019-11-27 04:43:14
问题 The following program does not compile, because in the line with the error, the compiler chooses the method with a single T parameter as the resolution, which fails because the List<T> does not fit the generic constraints of a single T . The compiler does not recognize that there is another method that could be used. If I remove the single- T method, the compiler will correctly find the method for many objects. I've read two blog posts about generic method resolution, one from JonSkeet here

Can't await async extension method

南笙酒味 提交于 2019-11-27 04:43:11
问题 Situation is pretty simple - I wrote an extension method and made it async with return type Task<T> . But when I try to call it using await, compiler throws an error which suggests that the extension method wasn't recognized as async at all. Here's my code: public static async Task<NodeReference<T>> CreateWithLabel<T>(this GraphClient client, T source, String label) where T: class { var node = client.Create(source); var url = string.Format(ConfigurationManager.AppSettings[configKey] + "/node/

How can I get an extension method to change the original object?

痞子三分冷 提交于 2019-11-27 04:29:56
I want to be able to write extension methods so that I can say: lines.ForceSpaceGroupsToBeTabs(); instead of: lines = lines.ForceSpaceGroupsToBeTabs(); However, the following code currently outputs: ....one ........two instead of: Tone TTtwo What do I have to change in the following code to make it output: Tone TTtwo (note that for visibility, . = space, T = \t ): using System; using System.Collections.Generic; namespace TestExtended82343 { class Program { static void Main(string[] args) { List<string> lines = new List<string>(); lines.Add("....one"); lines.Add("........two"); lines

Using extension methods in .NET 2.0?

社会主义新天地 提交于 2019-11-27 03:56:52
I want to do this, but getting this error: Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? [snipped some path stuff] I have seen some answers here that says, you have to define this attribute yourself. How do I do that? EDIT : This is what I have: [AttributeUsage ( AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method )] public sealed class ExtensionAttribute : Attribute { public static int MeasureDisplayStringWidth ( this

How to implement left join in JOIN Extension method

早过忘川 提交于 2019-11-27 03:50:29
I am trying to implement an outer join on this kind of query for the p.Person table. How would I do this? This example is taken from http://ashishware.com/DSLinqExample.shtml var onlyinfo = p.Person .Where(n => n.FirstName.Contains('a')) .Join(p.PersonInfo, n => n.PersonId, m => m.PersonId, (n, m) => m) .ToArray<Persons.PersonInfoRow>(); Jon Skeet Normally left joins in LINQ are modelled with group joins, sometimes in conjunction with DefaultIfEmpty and SelectMany : var leftJoin = p.Person.Where(n => n.FirstName.Contains("a")) .GroupJoin(p.PersonInfo, n => n.PersonId, m => m.PersonId, (n, ms)

Why are “static” and “this” needed for extension methods, and how is their memory allocated?

走远了吗. 提交于 2019-11-27 03:29:10
问题 A few questions about Extension Methods: Why are Extension Methods static? Why do they need to be declared in a static class? What does the this keyword indicate in the parameter list of extension method? As it is a static class how does the "this" keyword work in this context? How does memory allocation happen for these type of methods? 回答1: The only difference between a static and a non-static method is that a non-static method receives an implicit parameter - this . Extension methods are

How to define a type extension for T[] in F#?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 03:15:25
问题 In C#, I can define an extension method for a generic array of type T like this: public static T GetOrDefault<T>(this T[] arr, int n) { if (arr.Length > n) { return arr[n]; } return default(T); } but for the life of me I can't figure out how to do the same in F#! I tried type 'a array with , type array<'a> with and type 'a[] with and the compiler wasn't happy with any of them. Can anyone tell me what's the right to do this in F#? Sure, I can do this by overshadowing the Array module and add a