extension-methods

Can I write generic extension method to run task?

若如初见. 提交于 2019-12-02 17:53:18
问题 I am calling this method: Windows.Foundation.IAsyncOperation<AppServiceResponse> AppServiceConnection.SendMessageAsync(ValueSet message) e.g. await connection.SendMessageAsync(initialStatus); It shows an error: 'IAsyncOperation' does not contain a definition for "GetAwaiter" and no extension method "GetAwaiter" accepting a first argument of type 'IAsyncOperation'could be found (are you missing a using directive for 'System'?) Then I change it to: await Task.Run(() => connection

Define an Extension Method for IEnumerable<T> which returns IEnumerable<T>?

ぃ、小莉子 提交于 2019-12-02 17:09:16
How do I define an Extension Method for IEnumerable<T> which returns IEnumerable<T> ? The goal is to make the Extension Method available for all IEnumerable and IEnumerable<T> where T can be an anonymous type. The easiest way to write any iterator is with an iterator block, for example: static IEnumerable<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate) { foreach(T value in data) { if(predicate(value)) yield return value; } } The key here is the " yield return ", which turns the method into an iterator block, with the compiler generating an enumerator ( IEnumerator<T> ) that does

How can I create extension methods with static return type?

我的未来我决定 提交于 2019-12-02 16:11:27
问题 I was trying to write a simple extension method for Color static class which return Black and White equivalent of that color. The problem is that extention methods can't return Static types... So, how can I do this?! please help me. 回答1: The problem is that NO method can return a static type. Static classes are stateless (or have only static state), and thus have only one "instance" that is globally accessible from any code referencing the namespace. You can return a Color; the Color class

Interface + Extension (mixin) vs Base Class

馋奶兔 提交于 2019-12-02 15:35:42
Is an interface + extension methods (mixin) preferable to an abstract class? If your answer is "it depends", what does it depend upon? I see two possible advantages to the interface + extension approach. Interfaces are multiply inheritable and classes are not. You can use extension methods to extend interfaces in a non-breaking way. (Clients that implement your interface will gain your new base implementation but still be able to override it.) I have not yet thought of a downside to this approach. There may be a glaringly simple reason that the interface + extension approach will fail. Two

nUnit testing a controller extension method

对着背影说爱祢 提交于 2019-12-02 13:24:55
UX controls framework I'm using requires an extension method on MVC controllers. A null object reference is thrown when nUnit tries to call that method (used in order to package a partial view into Json). The author of the framework suggested calling that method through an interface, however that just postpones the null error. Is there a way to test the ActionResult from a controller that uses an extension method? The Controller Create() method returns the resulting partial from the extension method: return Json(new { Content = viewRender.RenderPartialView(this, "ListItems/SimpleSyllabi", new[

Mysterious “extra argument in call” error in Array extension method [duplicate]

我只是一个虾纸丫 提交于 2019-12-02 13:03:17
This question already has an answer here: Swift 3.0: compiler error when calling global func min<T>(T,T) in Array or Dictionary extension 1 answer This compiles: extension Array { func chunked(by chunkSize:Int) -> [[Element]] { return stride(from: 0, to: self.count, by: chunkSize).map { Array(self[$0..<[$0 + chunkSize, self.count].min()!]) } } } This doesn't (substituting the global min() function for the array min() method): extension Array { func chunked(by chunkSize:Int) -> [[Element]] { return stride(from: 0, to: self.count, by: chunkSize).map { Array(self[$0..<min($0 + chunkSize, self

How to apply an Extension Method on the object having the type of ExpandoObject?

无人久伴 提交于 2019-12-02 07:45:37
问题 Here is my code: public static class DynamicExtensions public static void Add(this ExpandoObject obj, string path){ dynamic _obj = obj; if (_obj == null) throw new ArgumentNullException("obj"); _obj.path = path; } } But I got the error of "'System.Dynamic.ExpandoObject' does not contain a definition for 'Add'", when I call it in this way: dynamic obj = new ExpandoObject(); obj.Add("p1"); How to fix it? Thanks in advance! 回答1: The problem is using dynamic with extension methods - the two just

Upgrading Swift 3 to 4, swift extension no longer in objective c

左心房为你撑大大i 提交于 2019-12-02 04:27:02
问题 I just finished upgrading a mixed language project (objective-c and Swift) from Swift 3 to Swift 4. Everything seemed to go well except all of my Swift extensions are no longer accessible in objective-c. I can't figure out how to get any Swift extension to show up in objective-c. I've tried searching, but I can't find any mention of changes to extensions in Swift 4 except for loosening up the private scope. All of these extensions were accessible from Objective-c in Swift 3, so there are no

Upgrading Swift 3 to 4, swift extension no longer in objective c

梦想与她 提交于 2019-12-02 03:35:51
I just finished upgrading a mixed language project (objective-c and Swift) from Swift 3 to Swift 4. Everything seemed to go well except all of my Swift extensions are no longer accessible in objective-c. I can't figure out how to get any Swift extension to show up in objective-c. I've tried searching, but I can't find any mention of changes to extensions in Swift 4 except for loosening up the private scope. All of these extensions were accessible from Objective-c in Swift 3, so there are no incompatible types (structs or non-raw enums). The extensions are marked public. The extensions are part

Does _ArrayType or _ArrayProtocol not available in Swift 3.1?

懵懂的女人 提交于 2019-12-02 03:19:52
I was using _ArrayType in my project when I was running on swift 2.1. I upgraded to swift 3.0.2 (Xcode 8.2.1) last week and I found here that _ArrayType is changed to _ArrayProtocol and it was working well. Today I upgraded my Xcode to 8.3.1, and it gives me error: Use of undeclared type '_ArrayProtocol' . Here is my code: extension _ArrayProtocol where Iterator.Element == UInt8 { static func stringValue(_ array: [UInt8]) -> String { return String(cString: array) } } What's wrong now? Why _ArrayProtocol is undeclared in swift 3.1 while it was working in swift 3.0.2. Also when I look here in