extension-methods

Error with rounding extension on decimal - cannot be accessed with an instance reference; qualify it with a type name instead

*爱你&永不变心* 提交于 2019-12-10 12:55:50
问题 I've used extension methods numerous times, and haven't run into this issue. Anyone have any ideas why this is throwing an error? /// <summary> /// Rounds the specified value. /// </summary> /// <param name="value">The value.</param> /// <param name="decimals">The decimals.</param> /// <returns></returns> public static decimal Round (this decimal value, int decimals) { return Math.Round(value, decimals); } Usage: decimal newAmount = decimal.Parse("3.33333333333434343434"); this.rtbAmount.Text

Is “null this” an acceptable use of extension methods? [closed]

≡放荡痞女 提交于 2019-12-10 12:47:01
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . So, I really enjoy using extension methods.. maybe a bit too much. So, I'm going to ask about my latest enjoyment to ensure that I'm

Extend Enum with flag methods?

允我心安 提交于 2019-12-10 11:11:11
问题 I have found good examples on how to create extension methods to read out single values from bitwise enums. But now that C# 4 has added the HasFlag method they are really not needed. What I think would be really helpful though is an extension to SET a single flag! I have many situations where I need to set the flag values individually. I want an extension method with this signature: enumVariable.SetFlag(EnumType.SingleFlag, true); OR possibly: enumVariable.SetFlag<EnumType>(EnumType

Extension methods in Mono 2.4 and RhinoMocks 3.5

一笑奈何 提交于 2019-12-10 11:04:24
问题 I am playing around with MonoDevelop 2.0 and Mono 2.4 in Ubuntu. I have run into problems with extension methods not being available (eg mockView.Stub(...)) in RhinoMocks 3.5 for AAA style tests. I downloaded the RhinoMocks dll from Ayende's site rather than compiled from source. My project in MonoDevelop is setup to target framework 3.5 Using the RhinoMocks c#2.0 syntax with static methods on the RhinoMocksExtensions class works. (e.g RhinoMocksExtensions.Stub(authSvc, delegate(IAuthService

Pivoting a collection of arrays

微笑、不失礼 提交于 2019-12-10 10:49:01
问题 Basically, I have a collection of objects each implement a member of Type IValueCollection . public interface IValueCollection : IEnumerable<decimal> { decimal this[int index] { get; set; } } MeasurementCollection.Values is of type IValueCollection . With the logic below I want to pivot a collection of IValueCollection and wrote the extension method below. public static IEnumerable<IValueCollection> PivotValues(this MeasurementCollection items) { if(items.IsQuantized()) { int s = (int)items

How to merge htmlAttributes in Custom Helper

Deadly 提交于 2019-12-10 04:29:40
问题 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 =

Design choice: Do I want extension methods to throw exceptions on null? [duplicate]

我是研究僧i 提交于 2019-12-10 04:15:08
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C#: Best practice for validating “this” argument in extension methods I'm ambivalent about a design choice, and would like to hear the opinions of the SO community. The example I bring up here is just one possible case where this design choice has to be made - in reality, there might be many more cases. Answers are welcome both to this specific case and to a more general approach, and guidelines on how to make

ruby - override method and then revert

梦想的初衷 提交于 2019-12-10 03:47:16
问题 I am trying to find a way that I can override a method, do something, and then revert without leaving any artifacts around. I have implemented this using mocha but obviously this is not going to fly in a production app. Notice the new method has parameters and the old one does not. Example as follows require 'rubygems' require 'mocha' class Example def to_something self.stubs(:attribs => other(1)) r = attribs_caller self.unstub(:attribs) r end def other(int) {"other" => int } end def attribs

Intellisense for extension methods?

ε祈祈猫儿з 提交于 2019-12-10 03:26:29
问题 Is there a way to get intellisense for extension methods from classes that are not currently within the usings, but referenced in the solution. It would be great convenience to be able to just type and not have to add the using when using an extension method the first time in a class. 回答1: I believe what you are looking for is the Import Symbol Creation with Re#. I use this all the time when accessing members that are not part of the current using statements. In essence, Re# will look through

resharper intellisense problem with extension methods

泪湿孤枕 提交于 2019-12-10 03:12:48
问题 so, I have a repository defined with a method like this: IQueryable<Customer> Customers{...} and elsewhere an extension method to filter the customers like so: public static IQueryable<Customer> WithID(this IQueryable<Customer> customers, int ID){...} and this woks nicely, letting me use the repository like this: var c = repo.Customers().WithID(5).Single(); but the problem is, ReSharper messes up the Auto-Completion on this big time. When I type var c = repo.Customers().Wi I get nice