extension-methods

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

大城市里の小女人 提交于 2019-12-01 03:04:32
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)); } } } It said: "The event ' System.ComponentModel.INotifyPropertyChanged.PropertyChanged' can only appear on

C#: implicit operator and extension methods

痞子三分冷 提交于 2019-12-01 02:59:23
I am trying to create a PredicateBuilder<T> class which wraps an Expression<Func<T, bool>> and provides some methods to easily build up an expression with various And and Or methods. I thought it would be cool if I could use this PredicateBuilder<T> as an Expression<Func<T, bool>> directly, and thought this could be done by having an implicit operator method thing. Stripped down version of the class looks like this: class PredicateBuilder<T> { public Expression<Func<T, bool>> Predicate { get; protected set; } public PredicateBuilder(bool initialPredicate) { Predicate = initialPredicate ?

C# Extension method precedence

扶醉桌前 提交于 2019-12-01 02:54:00
I'm a bit confused about how extension methods work. If I'm reading this correctly http://msdn.microsoft.com/en-us/library/bb383977.aspx and this If an extension method has the same signature as a method in the sealed class, what is the call precedence? . Then the following should write out "Instance", but instead it writes "Extension method". 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

RichTextBox BeginUpdate() EndUpdate() Extension Methods Not Working

感情迁移 提交于 2019-12-01 02:44:14
问题 I have a richTextBox I am using to perform some syntax highlighting. This is a small editing facility so I have not written a custom syntax highlighter - instead I am using Regex s and updating upon the detection of an input delay using an event handler for the Application.Idle event: Application.Idle += new EventHandler(Application_Idle); in the event handler I check for the time the text box has been inactive: private void Application_Idle(object sender, EventArgs e) { // Get time since

How to call an extension method of a dynamic type?

╄→гoц情女王★ 提交于 2019-12-01 02:43:38
I'm reading the book 'C# in Depth, 2nd Edition' of Jon Skeet. He said that we can call extension methods with dynamic arguments using two workarounds, just as dynamic size = 5; var numbers = Enumerable.Range(10, 10); var error = numbers.Take(size); var workaround1 = numbers.Take((int) size); var workaround2 = Enumerable.Take(numbers, size); Then he said "Both approaches will work if you want to call the extension method with the dynamic value as the implicit this value". I don't know how to achieve it. Thanks a lot. Like this: dynamic numbers = Enumerable.Range(10, 10); var firstFive =

Can There Be Private Extension Methods?

不羁岁月 提交于 2019-12-01 02:03:08
Let's say I have a need for a simple private helper method, and intuitively in the code it would make sense as an extension method. Is there any way to encapsulate that helper to the only class that actually needs to use it? For example, I try this: class Program { static void Main(string[] args) { var value = 0; value = value.GetNext(); // Compiler error } static int GetNext(this int i) { return i + 1; } } The compiler doesn't "see" the GetNext() extension method. The error is: Extension method must be defined in a non-generic static class Fair enough, so I wrap it in its own class, but still

NVelocity extension method ASP.NET webform

a 夏天 提交于 2019-12-01 01:45:45
I was wondering if it's possible to use an extension method with asp.net webforms and nvelocity. I would like to set some defaults if the string value is null or empty. Example of .vm file: Example of my email body... Billable Status: $billableStatus.Evaluate() rest of my email body... Attempted extension method: public static class Helper { public static string Evaluate(this string value) { if (String.IsNullOrEmpty(value)) return "Not Provided"; else return value; } } Or is there an alternative to what I'm tryting to accomplish? I don't think NVelocity can resolve extension methods with C#/VB

Correctly making an ActionLink extension with htmlAttributes

时光怂恿深爱的人放手 提交于 2019-12-01 01:38:35
I use a custom extension for my ActionLinks. I have added an attribute data_url which is meant to be translated to an attribute of data-url . This is, replacing the underscaore with a dash. Here is link 1 using my custom extension: @Ajax.ActionLink("Add", MyRoutes.GetAdd(), new AjaxOptions() , new { data_url = Url.Action(...)}) Result: data_url Here is link 2 using the framework ActionLink: @Ajax.ActionLink("Add 2", "x", "x", null, new AjaxOptions() , new { data_url = Url.Action(...) }) Result: data-url Here is the extension, simple enough, except that the only way to pass the htmlAttributes

Injecting DI service on a extension method

柔情痞子 提交于 2019-12-01 01:13:58
问题 I'm trying to get the IStringLocalizer service instance inside a extension method, is it possible? Any suggestions on how should I inject it? My goal here is to translate a type using its name as convention. public static class I18nExtensions { private IStringLocalizer _localizer; // <<< How to inject it? public static string GetName(this Type type) { return _localizer[type.Name].Value; } } 回答1: Following @NightOwl888 comment I was in the wrong path, I ended up creating the following service:

How to add LanguagePrimitives.GenericZero / get_Zero to System.String?

ぐ巨炮叔叔 提交于 2019-12-01 00:51:43
问题 Note: I added a lot of Of interest comments at the end. These are not mean to suggest that one should use inline and static type parameters willy nilly, they are there so that one does not have to spend hours searching lots of SO questions related to this question to better understand these concepts. I know that when one needs to make a function generic and needs a zero (0) value F# provides GenericZero. Resolves to the zero value for any primitive numeric type or any type with a static