extension-methods

Why Standard Extension Method on IEnumerables

人盡茶涼 提交于 2019-12-11 08:31:37
问题 When i use a standard Extension Method on a List such as Where (...) the result is always IEnumerable , and when you decide to do a list operation such as Foreach() we need to Cast (not pretty) or use a ToList() extension method that (maybe) uses a new List that consumes more memory (is that right?): List<string> myList=new List<string>(){//some data}; ( Edit : this cast on't Work) myList.Where(p=>p.Length>5).Tolist().Foreach(...); or (myList.Where(p=>p.Length>5) as List<string>).Foreach(...)

How to set Expect to a Extension method in Rhino Mocks 3.6

Deadly 提交于 2019-12-11 08:15:56
问题 Good afternoon I have a class and it has an associated extension method. public class Person{ public int ID {get;set} public string Name {get;set} } Extension method: public static class Helper { public static int GetToken(this Person person){ int id = 0; //Something here is done to read token from another service... return id; } } Now I am trying to use Rhino and test this method public void readPersonToken(int personId) { var person = Person.GetPersonInfo(personId);//we consume a service

Calling an extension method on a member variable of type T of class<T>

半城伤御伤魂 提交于 2019-12-11 08:05:20
问题 Given extension method below: public static class Ext { public static void Serialize(this Guid guid_, StringWriter sw_) { sw_.Write(guid_.ToString("B")); } } and the class: public class Field<T> { public T value; public void Serialize(StringWriter sw_) { value.Serialize(sw_); } } I would like to do the following, but can't quite figure it out: public class Foo { public Field<Guid> uid; public Foo() { // assume StringWriter object sw; uid.Serialize(sw); } } Obviously real-life situation is

How to get MethodInfo of a generic method?

杀马特。学长 韩版系。学妹 提交于 2019-12-11 07:49:59
问题 I am trying to get a MethodInfo object for the method: Any<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>) The problem I'm having is working out how you specify the type parameter for the Func<TSource, Boolean> bit... MethodInfo method = typeof(Enumerable).GetMethod("Any", new[] { typeof(Func<what goes here?, Boolean>) }); Help appreciated. 回答1: There's no way of getting it in a single call, as you would need to make a generic type constructed of the generic parameter of the method

Typescript - How do I add an extension method

妖精的绣舞 提交于 2019-12-11 07:21:57
问题 I read that you can create extension methods in Typescript and I looked up some code And put that code in my extension methods.ts but I get an error saying that toNumber doesn't exist. How can I fix this? 回答1: You can extend String interface by augmenting global scope: export { }; declare global { interface String { toNumber(): number; } } String.prototype.toNumber = function (this: string) { return parseFloat(this) }; Playground 回答2: You could extend the String interface, like this:

Problem with Extension method: IXmlLineInfo

女生的网名这么多〃 提交于 2019-12-11 06:49:02
问题 When I try to use any extension method to my class in ascx-control: <%@ Import Namespace="VfmElita.Page.Stat" %> <%=new MyTestClass().ExtMethod() %> and here is the simplest method in the world: namespace VfmElita.Page.Stat { public static class TestExtention { public static string ExtMethod(this MyTestClass test) { return "Hope for result"; } } } (it is located in ascx.cs-file of the control I got the following error: error CS0012: The type 'System.Xml.IXmlLineInfo' is defined in an assembly

Performance of swift extensions on primitive types

≯℡__Kan透↙ 提交于 2019-12-11 06:25:36
问题 I am wondering if there would be any performance difference between the following in swift : let x = 42.42 print(floor(x)) and let x = 42.42 extension Double { func myFloor() -> Double { return floor(self) } } print(x.myFloor()) 回答1: The compiler inlines the code in the second case and produces the same machine code. You can see for yourself, here. 来源: https://stackoverflow.com/questions/45558276/performance-of-swift-extensions-on-primitive-types

LINQ - Distinct() returning a different value if extension method used

只愿长相守 提交于 2019-12-11 05:41:55
问题 I have a LINQ query which is attempting to get all of the distinct months of all of the dates in a table. I had this working using the Distinct() extension method. I then made it more readable by using an extension method to extract the month. And then it stopped returning Distinct results. Can anyone help me work out what happened here? As an aside, if someone can tell me the best way to get the distinct months, that would be nice too. But it's more important that I understand why this is

Reasons to specify generic types in LINQ extension methods

一曲冷凌霜 提交于 2019-12-11 05:29:55
问题 Just out of curiosity: Many LINQ extension methods exist as both generic and non-generic variants, for example Any and Any<> , Where and Where<> etc. Writing my queries I usually use the non-generic variants and it works fine. What would be the cases when one has to use generic methods? --- edit --- P.S.: I am aware of the fact that internally only generic methods are called and the compiler tries to resolve the content of the generic brackets <> during compilation. My question is rather what

Is there a way to constrain `Self` to a generic type?

时光毁灭记忆、已成空白 提交于 2019-12-11 05:18:27
问题 https://www.raywenderlich.com/148448/introducing-protocol-oriented-programming protocol Bird { var name: String { get } var canFly: Bool { get } func doSomething() } protocol Flyable { var airspeedVelocity: Double { get } } extension Bird { // Flyable birds can fly! var canFly: Bool { return self is Flyable } func doSomething() { print("default Bird: \(name)") } } class FlappyBird: Bird, Flyable { let name: String let canFly = true var airspeedVelocity: Double = 5.0 init(name: String) { self