extension-methods

Complement of Two Lists?

旧城冷巷雨未停 提交于 2019-12-06 01:56:58
问题 lets say I have a list of strings: A,B,C,D Then another list of strings B,C,D I want to know what elements are in the first list that aren't in the second list, so the result would be A I don't know the name of the extension method to do this is. I know I can use concat, union, intersect for similar list comparisons, but just don't know the name to accomplish this particular task. Addendum, I am interested in duplicates, so if the first list is: A,A,A,B,C,D and the second list is B,C,D i want

Object deep clone implementation

夙愿已清 提交于 2019-12-06 01:39:11
I have to implement generic extention deepclone method which can be used with any reference type instance to get its deep copy. I implement it as the following static class ClassCopy { static public T DeepClone<T> (this T instance) { if (instance == null) return null; var type = instance.GetType(); T copy; var flags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var fields = type.GetFields(flags); // If type is serializable - create instance copy using BinaryFormatter if (type.IsSerializable) { using (var stream = new MemoryStream()) {

Add An Anchor To RedirectToAction's Result?

元气小坏坏 提交于 2019-12-06 00:18:23
I'm trying to create an extension method similar to MVCContrib's RedirectToAction method by creating a method that will take an #anchor argument and add it to the Url. I am familiar with this question but it's not strongly typed. It's also possible to add values to the query-string, but that won't work for an anchor. public static RedirectToRouteResult RedirectToAction<T>(this Controller controller, Expression<Action<T>> action, string anchor) where T : Controller { var result = new RedirectToRouteResult<T>(action); // how can I add the #anchor to the result's url? return result; } 来源: https:/

Extension methods with .NET Micro Framework

℡╲_俬逩灬. 提交于 2019-12-05 21:37:45
It seems that extensions methods are not supported/working with the .NET Micro Framework. Is there any way to get this usefull language feature working? When you add the ExtensionAttribute class to your project you can use extension methods also in the .NET Micro Framework. namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] public sealed class ExtensionAttribute : Attribute { } } 来源: https://stackoverflow.com/questions/14344668/extension-methods-with-net-micro-framework

ArgumentNullException or NullReferenceException from extension method?

若如初见. 提交于 2019-12-05 19:47:38
What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it should be ArgumentNullException, but on the other hand they're used like instance methods so it might be more natural to use the NullReferenceException. Let's take the following example: public static string ToInvariantString(this IFormattable value, string format) { return value.ToString(format, CultureInfo.InvariantCulture); } This way a

Process every pair in a sequence

独自空忆成欢 提交于 2019-12-05 19:04:41
I'm looking for a concise way to process every (unordered) pair of elements in a sequence in .NET. I know I can do it with nested for loops, but I was looking for something a little more readable. I was imagining something like a modified Any() extension method: IEnumerable<Transaction> transactions = ... if (transactions.AnyPair( (first, second) => first.UniqueID == second.UniqueID)) throw ... Or maybe a foreach -style one: IEnumerable<JigsawPiece> pieces = ... pieces.ForEachPair( (first, second) => { TryFit(first, second); }); This question has been asked for other languages (e.g. see

c# Extension methods - design patterns

放肆的年华 提交于 2019-12-05 17:53:28
问题 I would like to know if C# extension method is based on any existing design pattern. 回答1: A design pattern is simply a well known paradigm, i.e. "when you want to achieve X, do Y". A well known paradigm in object-oriented languages such as C# is "when you want to act on the state of an object, call a method on an instance of it". However, before extension methods were created, you could not call your own method on an instance of an object that you could not add an implementation to (e.g.

A different take on FirstOrDefault

╄→гoц情女王★ 提交于 2019-12-05 17:50:46
问题 The IEnumerable extension method FirstOrDefault didn't exactly do as I wanted so I created FirstOrValue. Is this a good way to go about this or is there a better way? public static T FirstOrValue<T>(this IEnumerable<T> source, Func<T, bool> predicate, T value) { T first = source.FirstOrDefault(predicate); return Equals(first, default(T)) ? value : first; } 回答1: Your code is probably incorrect; you probably haven't considered all of the cases. Of course, we cannot know if any code is correct

Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

元气小坏坏 提交于 2019-12-05 17:31:50
I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexing on it). Can someone please explain how to use this overload and specifically what that int in the Func is for and how it is used? The int parameter represents the index of the current item within the current iteration . Each time you call one of the LINQ

Overload resolution, extension methods and genericity in C#

你说的曾经没有我的故事 提交于 2019-12-05 17:27:50
I have the following scenario in my C# source: class A{} class Dispatch<T>{} static class DispatchExt { public static void D<T>(this Dispatch<T> d, int a) { Console.WriteLine("Generic D chosen with a = " + a.ToString()); } public static void D(this Dispatch<A> d, int a) { Console.WriteLine("D<A> chosen with a = " + a.ToString()); } } class Program { static void D<T>(Dispatch<T> d, int a) { d.D(a); } static void Main(string[] args) { int a = 5; var dispatch = new Dispatch<A>(); dispatch.D(a); D(dispatch, a); } } When I run this code the output is: " D<A> chosen with a = 5" "Generic D chosen