extension-methods

PowerShell, Extension Methods, and Monkey Patching

泪湿孤枕 提交于 2019-12-06 22:20:12
问题 Is it possible to write extension method in PowerShell? or to bolt a new method on top of an existing type like [string] live at runtime? 回答1: I don't know of a way to patch a type with an extension method. But it's certainly possible to patch an object via the add-member cmdlet PS> $a = "foo" PS> $a = add-member -in $a -memberType ScriptMethod -name Bar -value { $this + "bar" } -passthru PS> $a.Foo() foobar EDIT Explain the completely and totally readable PowerShell Syntax :) I love

Is it possible to create Extension Methods with 2.0 Framework?

拟墨画扇 提交于 2019-12-06 19:46:03
问题 I was wondering if there is a way to create extension methods using Visual Studio 2005 and the 2.0 framework? public static class StringExtensions { public static void SomeExtension(this String targetString) { } } If there is no way to do this, what would the equivalent be? Just create static methods in some sort of library class? 回答1: You can create extension methods using .Net framework 2.0, if you use the C# 3.0 compiler and Visual Studio 2008 or greater. The catch is that you have to add

How do you manage the namespaces of your extension methods?

孤街浪徒 提交于 2019-12-06 18:58:16
问题 Do you use a global, catchall namespace for all of your extension methods, or do you put the extension methods in the same namespace as the class(es) they extend? Or do you use some other method, like an application or library-specific namespace? I ask because I have a need to extend System.Security.Principal.IIdentity , and putting the extension method in the System.Security.Principal namespace seems to make sense, but I've never seen it done this way. 回答1: I would recommend putting all your

When do Extension Methods break?

我的梦境 提交于 2019-12-06 18:47:07
问题 We are currently discussing whether Extension methods in .NET are bad or not. Or under what circumstances Extension methods can introduce hard to find bugs or in any other way behave unexpectedly. We came up with: Writing an extension method for types that are not under your control (e.g. extending DirectoryInfo with GetTotalSize(), etc...) is bad, because the owner of the API could introduce a method that hides our extension - and might have different edge cases. For example testing for null

why allow extension methods on null objects?

喜你入骨 提交于 2019-12-06 17:14:53
问题 what is the point of allowing invocation of extension methods on null objects? this is making me unnecessarily check for a null object in the extension method. AFAIK,i can't understand this? Please explain. 回答1: Extension methods are syntactic sugar of the C# language, they get compiled to normal static method calls in ILCode. A static method doesn't know anything about the parameters at compile time. 回答2: Simply put, why not? You can sometimes skip the test if the first method you call

Pivoting a collection of arrays

╄→尐↘猪︶ㄣ 提交于 2019-12-06 14:34:42
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.First().Template.Frequency; int c = items.Count; for (int n = 0; n < s; n++) { IValueCollection v = new

Select the maximal item from collection, by some criterion

对着背影说爱祢 提交于 2019-12-06 14:28:59
i am new to .net 3.5. I have a collection of items: IList<Model> models; where class Model { public string Name { get; private set; } } I would like to get the element, which has the longest name's length. I tried string maxItem = models.Max<Model>(model => model.Name.Length); but it of course returns the maximum length (and I need a Model object). I know there is a way of doing this using the extension methods but I don't know how. This is how I got it to work. Maybe there's a better way, I'm not sure: decimal de = d.Max(p => p.Name.Length); Model a = d.First(p => p.Name.Length == de); There

GroupBy and Select extension method assistance

时光总嘲笑我的痴心妄想 提交于 2019-12-06 14:28:37
I am trying to GroupBy a few fields using the following code: var cars = tmp.Select(a => new { a.Make, a.Model, a.Year }); cars = cars.Distinct() .OrderBy(a => a.Make) .ThenBy(a => a.Model); var groupedCars = cars.GroupBy(a => new { a.Make, a.Model }) .Select(b => new { b.Make, b.Model, b.Year }); Unfortunately, the very last line of code is giving me errors as is does not recognize the three fields. 'System.Linq.IGrouping(AnonymousType#1,AnonymousType#2)' does not contain a definition for 'Make' and no extension method 'Make' accepting a first argument of type 'System.Linq.IGrouping

Expression of type 'MyEnum' cannot be used for parameter of type

℡╲_俬逩灬. 提交于 2019-12-06 13:52:47
I created Enum ToFrendlyString function for my enums, but i cant use in Linq. public enum MyEnum { Queued = 0, [Description("In progress")] In_progress = 2, [Description("No answer")] No_answer = 6, } public static class EnumToFrendlyString { public static string ToFrendlyString(this Enum value) { return value.GetEnumDescription(); } public static string GetEnumDescription(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi.GetCustomAttributes( typeof(DescriptionAttribute), false); if (attributes.Length > 0) return

Mocking of extension method result in System.NotSupportedException

本秂侑毒 提交于 2019-12-06 13:41:45
问题 I'm unit testing a ClientService that uses the IMemoryCache interface: ClientService.cs: public string Foo() { //... code _memoryCache.Set("MyKey", "SomeValue", new TimeSpan(0, 0, 60)); } When I try to mock the IMemoryCache 's Set extension with: AutoMock mock = AutoMock.GetLoose(); var memoryCacheMock = _mock.Mock<IMemoryCache>(); string value = string.Empty; // Attempt #1: memoryCacheMock .Setup(x => x.Set<string>(It.IsAny<object>(), It.IsAny<string>(), It.IsAny<TimeSpan>())) .Returns("");