extension-methods

Best performance for ObservableCollection.AddRange

霸气de小男生 提交于 2019-12-05 12:19:07
I'm writing an extension method for ObservableCollection and have read that the .Add function raises 3 property changed events per call, So that something like this is a bad idea: public static void AddRange<T>(this ObservableCollection<T> oc, IEnumerable<T> collection) { if (collection == null) { throw new ArgumentNullException("collection"); } foreach (var i in collection) { oc.Add(i); } } Are there any other solutions to this? Given that Concat<T> is an extension method it is almost certainly just calling .Add() under the covers, it can't have internal knowledge of the class. You could use

using extension methods on int

狂风中的少年 提交于 2019-12-05 11:35:31
问题 I'm reading about extension methods, and monkeying around with them to see how they work, and I tried this: namespace clunk { public static class oog { public static int doubleMe(this int x) { return 2 * x; } } class Program { static void Main() { Console.WriteLine(5.doubleMe()); } } } and it worked as expected, successfully extending int with the doubleMe method, printing 10. Next, being an old C guy, I wondered if I could do this: namespace clunk { public static class BoolLikeC { public

HtmlAttributes in Extension Method

折月煮酒 提交于 2019-12-05 11:17:26
I'm using MVC 5 and I'm trying to write some Bootstrap extention methods. My goal is to 'overwrite' the Html.ActionLink method with Html.BootstrapLinkButton . The BootstrapLinkButton method should generate a link with the css classes "btn btn-default" automatically attached. My code so far: public static MvcHtmlString BootstrapLinkButton(this HtmlHelper htmlHelper, string linkText,string actionName, string controllerName, object routeValues = null, object htmlAttributes = null) { var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes); if (attributes.ContainsKey("class")) {

How to override functions from String class in C#

拜拜、爱过 提交于 2019-12-05 10:43:57
For example, I need to see if a string contains a substring, so I just do: String helloworld = "Hello World"; if(helloworld.Contains("ello"){ //do something } but if I have an array of items String helloworld = "Hello World"; String items = { "He", "el", "lo" }; I needed to create a function inside the String class that would return true if either of the items inside the array is contained in the string, for example. I would like to override the function Contains(string) with Contains(IEnumerable) for this scenario, instead of creating a function in another class. Is it possible to do this,

Overriding LINQ extension methods

眉间皱痕 提交于 2019-12-05 10:30:47
Is there a way to override extension methods (provide a better implementation), without explicitly having to cast to them? I'm implementing a data type that is able to handle certain operations more efficiently than the default extension methods, but I'd like to keep the generality of IEnumerable. That way any IEnumerable can be passed, but when my class is passed in, it should be more efficient. As a toy example, consider the following: // Compile: dmcs -out:test.exe test.cs using System; namespace Test { public interface IBoat { void Float (); } public class NiceBoat : IBoat { public void

Extension method must be defined in non-generic static class

丶灬走出姿态 提交于 2019-12-05 09:53:04
问题 Error at: public partial class Form2 : Form Probable cause: public static IChromosome To<T>(this string text) { return (IChromosome)Convert.ChangeType(text, typeof(T)); } Attempted (without static keyword): public IChromosome To<T>(this string text) { return (IChromosome)Convert.ChangeType(text, typeof(T)); } 回答1: If you remove "this" from your parameters it should work. public static IChromosome To<T>(this string text) should be: public static IChromosome To<T>(string text) 回答2: The class

How to merge htmlAttributes in Custom Helper

前提是你 提交于 2019-12-05 09:30:33
I have a Custom Helper where I receive a htmlAttributes as parameter: public static MvcHtmlString Campo<TModel, TValue>( this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, dynamic htmlAttributes = null) { var attr = MergeAnonymous(new { @class = "form-control"}, htmlAttributes); var editor = helper.EditorFor(expression, new { htmlAttributes = attr }); ... } The MergeAnonymous method must return the merged htmlAttributes received in parameter with "new { @class = "form-control"}": static dynamic MergeAnonymous(dynamic obj1, dynamic obj2) { var dict1 = new

Static method and extension method with same name

你。 提交于 2019-12-05 09:25:37
I created extension method: public static class XDecimal { public static decimal Floor( this decimal value, int precision) { decimal step = (decimal)Math.Pow(10, precision); return decimal.Floor(step * value) / step; } } Now I try to use it: (10.1234m).Floor(2) But compiler says Member 'decimal.Floor(decimal)' cannot be accessed with an instance reference; qualify it with a type name instead . I understand there is static decimal.Floor(decimal) method. But it has different signature. Why compiler is unable to choose correct method? You have two good and correct answers here, but I understand

Mixing generic methods and extension methods

僤鯓⒐⒋嵵緔 提交于 2019-12-05 08:32:55
I created the Class1.GetChild<T>() where T : DependencyObject extension method in lib1.dll assembly. After that, all assemblies that depends on lib1.dll failed to compile with error: The type 'System.Windows.DependencyObject' is defined in an assemebly that is not referenced. You must add a reference to assembly 'WindowsBase' etc... Why dependent assemblies requires WindowsBase even if they don't use GetChild ? . To reproduce (vs2010 .net4): lib1.dll (references WindowsBase ) namespace lib1 { public static class Class1 { public static T GetChild<T>(this DependencyObject src) where T :

Type inference problem when writing a generic extension method with more than one type

点点圈 提交于 2019-12-05 08:28:36
I am writing a generic extension method for IEnumerable for mapping a list of objects to another list of mapped objects. This is how I would like the method to work: IList<Article> articles = GetArticles(); return articles.Map<ArticleViewModel>(_mappingEngine); This is the method: public static IEnumerable<T2> Map<T1, T2>(this IEnumerable<T1> list, IMappingEngine engine) { return list.Select(engine.Map<T1, T2>); } However articles.Map<ArticleViewModel>(_mappingEngine); gives a compile error. The problem is that the type inference for T1 doesn't work. I have to explicitly call it like this