extension-methods

Proper way of testing ASP.NET Core IMemoryCache

十年热恋 提交于 2020-01-01 08:06:12
问题 I'm writing a simple test case that tests that my controller calls the cache before calling my service. I'm using xUnit and Moq for the task. I'm facing an issue because GetOrCreateAsync<T> is an extension method, and those can't be mocked by the framework. I relied on internal details to figure out I can mock TryGetValue instead and get away with my test (see https://github.com/aspnet/Caching/blob/c432e5827e4505c05ac7ad8ef1e3bc6bf784520b/src/Microsoft.Extensions.Caching.Abstractions

C# - Sorting using Extension Method

為{幸葍}努か 提交于 2020-01-01 04:35:13
问题 I want to sort a list of person say List<Person> persons=new List<Person>(); persons.Add(new Person("Jon","Bernald",45000.89)); persons.Add(new Person("Mark","Drake",346.89)); persons.Add(new Person("Bill","Watts",456.899)); based on public enum CompareOptions { ByFirstName, ByLastName, BySalary } public enum SortOrder { Ascending, Descending } using lambda expression what is the way to go for sorting? public static List<Person> SortPeople(this List<Person> lst, CompareOptions opt1,SortOrder

Visual Studio's “auto-resolve” feature doesn't work for extension methods - what now?

有些话、适合烂在心里 提交于 2020-01-01 02:40:14
问题 I love the "Resolve" feature in visual studio. Typical scenario: Type in Debug Type . Notice that no intellisense appears Right-click Select Resolve Choose using System.Diagnostics or System.Diagnostics.Debug Beautiful. Use it all the time. Extension method scenario: Type in var maxNumber = new int[] {1, 2, 3, 4} Type . Notice that intellisense brings up array methods but no LINQ extension methods Manually type Max() Right-click Max() No Resolve to be found Right click on int[] Still no

Linq extension method, how to find child in collection recursive

佐手、 提交于 2020-01-01 02:17:13
问题 I'm already familiar with Linq but have little understanding of extension methods I'm hoping someone can help me out. So I have this hierarchical collection pseudo code ie: class Product prop name prop type prop id prop List<Product> children And I have a list of products List products. Is there any way I can look for product in this collection by the id with a extension method ? In other words I need one item somewhere within the hierarchy. 回答1: Here is a generic solution that will short

How to compare nullable types?

梦想与她 提交于 2019-12-31 08:54:13
问题 I have a few places where I need to compare 2 (nullable) values, to see if they're the same. I think there should be something in the framework to support this, but can't find anything, so instead have the following: public static bool IsDifferentTo(this bool? x, bool? y) { return (x.HasValue != y.HasValue) ? true : x.HasValue && x.Value != y.Value; } Then, within code I have if (x.IsDifferentTo(y)) ... I then have similar methods for nullable ints, nullable doubles etc. Is there not an

Using Generics to Create HtmlHelper Extension Methods

痞子三分冷 提交于 2019-12-31 04:50:09
问题 I'm not really familiar with creating generic methods, so I thought I'd put this question to the community & see what comes back. It might not even be a valid use of generics! I'd like to create a HtmlHelper extension method where I can specify that the method is of a certain type. I pass into the method an instance of that type, and an instance of a TagBuilder object. I then specify the class attribute of the tag to be the type of object I passed in, with all the object's properties

How to convert type int[] to int?[]

点点圈 提交于 2019-12-31 04:34:25
问题 I'm using a linq query to output an int array. But I need to pass this into a method that only accepts int?[]. So after searching on ways to convert int[] to int?[] I found something that seemed might work here The following code is a simplified example that shows what is working and not working. using System; using System.Collections.Generic; using System.Web; using System.Linq; namespace ConsoleApp { class Program { static void Main(string[] args) { // working... int[] vids1 = new[] { "",

Optimized JSON serialiser / deserialiser as an extension method?

最后都变了- 提交于 2019-12-31 02:12:05
问题 I'd like to serialize any object as easily as possible to JSON, and then convert it back to the type=safe object simply. Can anyone tell me what I'm doing wrong in the "FromJSONString" extension method? Edit For your convenience, a complete and functional extension method is below. Do let me know if you see errors. public static string ToJSONString(this object obj) { using (var stream = new MemoryStream()) { var ser = new DataContractJsonSerializer(obj.GetType()); ser.WriteObject(stream, obj)

Is it necessary to export base method extensions in an R package? Documentation implications?

旧街凉风 提交于 2019-12-31 00:57:07
问题 In principle, I could keep these extensions not-exported, and this would also allow me to not-add redundant documentation for these already well-documented methods, while still also passing R CMD check myPackage without any reported WARNING s. What are some of the drawbacks, if any? Is this possibly recommended to keep extensions of basic methods compartmentalized within the package that defines them? Alternatively, will this make it more difficult for another package to depend on mine, if

Extension method resolution with nullable value type params

喜你入骨 提交于 2019-12-31 00:03:29
问题 public static class Extension { public static void Test(this DateTime? dt) { } } void Main() { var now = DateTime.Now; Extension.Test(now); // ok now.Test(); // compile time error } I'm just curious, why is the compiler not able to resolve the same method when called as an extension? 回答1: A DateTime is not convertible to Nullable<DateTime> explicitly. The C# specification, 7.6.5.2 Extension method invocations: An extension method is eligible if: Mj is accessible and applicable when applied to