extension-methods

Extension Method for Generic Class [duplicate]

扶醉桌前 提交于 2019-12-04 00:09:33
Possible Duplicates: C# -Generic Extension Method How do you write a C# Extension Method for a Generically Typed Class Is it possible to declare extension methods for generic classes? public class NeedsExtension<T> { public NeedsExtension<T> DoSomething(T obj) { // .... } } Stan R. To extend any class public static class Extensions { public static T DoSomething<T>(this T obj) { //... } } To extend a specific generic class public static NeedExtension<T> DoSomething<T>(this NeedExtension<T> obj) { //... } Yes, but you forgot the this keyword. Look at Queryable that provides all the LINQ

Why can't I invoke PropertyChanged event from an Extension Method?

蓝咒 提交于 2019-12-03 23:45:03
问题 I've tried to code a class to avoid a method like "RaisePropertyChanged". I know that I can inherit from a class that has that implementation but in some cases I can't. I've tried with a Extension Method but Visual Studio complain. public static class Extension { public static void RaisePropertyChanged(this INotifyPropertyChanged predicate, string propertyName) { if (predicate.PropertyChanged != null) { predicate.PropertyChanged(propertyName, new PropertyChangedEventArgs(propertyName)); } } }

How to make dictionary extension-methods?

心不动则不痛 提交于 2019-12-03 23:31:47
I'm trying to write a Dictionary extension that works independently of the data types of Key/Value. I tried pass it by using the object data type, assuming that it will works with any type. My code: public static class DictionaryExtensionsClass { public static void AddFormat(this Dictionary< ?? unknow type/*object*/, ??unknow type/*object*/> Dic, ??unknow type/*object*/ Key, string str, params object[] arglist) { Dic.Add(Key, String.Format(str, arglist)); } } You just make the method generic: public static void AddFormat<TKey>(this Dictionary<TKey, string> dictionary, TKey key, string

How to conditionally remove items from a .NET collection

穿精又带淫゛_ 提交于 2019-12-03 23:12:02
I'm trying to write an extension method in .NET that will operate on a generic collection, and remove all items from the collection that match a given criteria. This was my first attempt: public static void RemoveWhere<T>(this ICollection<T> Coll, Func<T, bool> Criteria){ foreach (T obj in Coll.Where(Criteria)) Coll.Remove(obj); } However this throws an InvalidOperationException, "Collection was modified; enumeration operation may not execute". Which does make sense, so I made a second attempt with a second collection variable to hold the items that need to be removed and iterate through that

Why does foreach fail to find my GetEnumerator extension method?

若如初见. 提交于 2019-12-03 22:26:50
I'm trying to make some code more readable. For Example foreach(var row in table) {...} rather than foreach(DataRow row in table.Rows) {...} . To do this I created an extension method: namespace System.Data { public static class MyExtensions { public static IEnumerable<DataRow> GetEnumerator( this DataTable tbl ) { foreach ( DataRow r in tbl.Rows ) yield return r; } } } But the compiler still throws foreach statement cannot operate on variables of type 'System.Data.DataTable' because 'System.Data.DataTable' does not contain a public definition for 'GetEnumerator' . To confirm that I

Where is the “Fold” LINQ Extension Method?

a 夏天 提交于 2019-12-03 22:01:20
I found in MSDN's Linq samples a neat method called Fold() that I want to use. Their example: double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor); Unfortunately, I can't get this to compile, either in their example or in my own code, and I can't find anywhere else in MSDN (like Enumerable or Array extension methods) that mention this method. The error I get is a plain old "don't know anything about that" error: error CS1061: 'System.Array' does not contain a definition for 'Fold' and no extension method

Warm-up when calling methods in C#

 ̄綄美尐妖づ 提交于 2019-12-03 21:55:20
I just came across this post that talks about time measuring. I remember (I hope I'm not misremembering) it's an unfair competition, if this method is never called before. That is: // At the beginning of the application MyClass instance = new MyClass(); instance.MyMethod(); instance.MyMethod(); // Faster than the first call, because now it's warmed up. Do we really have such warming-up theory in C#? If yes, why (what will the CLR do when warming-up)? And is everything the same if this method is an extension one (a static one)? BrokenGlass If by "warm up" you refer to JIT'ing then yes - if a

Using Extensions: Weighing The Pros vs Cons

我与影子孤独终老i 提交于 2019-12-03 21:30:24
问题 Recently I asked a question about how to clean up what I considered ugly code. One recommendation was to create an Extension Method that would perform the desired function and return back what I wanted. My first thought was 'Great! How cool are Extensions...' but after a little more thinking I am starting to have second thoughts about using Extensions... My main concern is that it seems like Extensions are a custom 'shortcut' that can make it hard for other developers to follow. I understand

Can you teach Entity Framework to recognize an expression?

谁说我不能喝 提交于 2019-12-03 21:05:27
I have a search function that uses the Entity Framework. One of the things you can search by is a date range. You might say something like "where Start Date is between SearchStart and Search End". It isn't that difficult to write in linq syntax, but it can get pretty verbose when you have many different date parameters to search by. I have an extension method on DateTime that basically checks of the date is contained between a StartDate and an EndDate. I use this in other places where EF isn't an issue, but I would also like to use it with EF queries. I am creating the query dynamically by

Linq To SQL problem - has no supported translation to SQL (problem with C# property)

假如想象 提交于 2019-12-03 17:09:25
问题 I'm extending some Linq to SQL classes. I've got 2 similar statements, the 1st one works, the 2nd does not ("has no supported translation to SQL" error). var reg2 = rs.ProductRegistrations().SingleOrDefault(p => p.Product.product_name == "ACE") var reg5 = rs.ProductRegistrations().SingleOrDefault(p => p.product_name == "ACE"); After reading this link LINQ: No Translation to SQL I understand (I think), that basically everything needs to be "inline", otherwise the expression tree can not be