extension-methods

How to write strongly typed generic extension function in Kotlin?

淺唱寂寞╮ 提交于 2020-01-22 10:15:09
问题 Focus on the strong and generic parts. Let's say I have this extension function: fun <E> Collection<E>.myContains(item: E) : Boolean { // quite pointless, I know, but a simple example return item in this } the intention is to write a function that only accepts types of the collection elements ( E ), but this is not validated by the compiler?! val isItInside: Boolean = listOf(1, 2).myContains("1") happily compiles. My guess is that E is inferred to be Any . How can I enforce this restriction

Extension methods in Python

会有一股神秘感。 提交于 2020-01-22 04:48:06
问题 Does Python have extension methods like C#? Is it possible to call a method like: MyRandomMethod() on existing types like int ? myInt.MyRandomMethod() 回答1: You can add whatever methods you like on class objects defined in Python code (AKA monkey patching): >>> class A(object): >>> pass >>> def stuff(self): >>> print self >>> A.test = stuff >>> A().test() This does not work on builtin types, because their __dict__ is not writable (it's a dictproxy ). So no, there is no "real" extension method

C# - Cannot call a HttpConfiguration Extension Methods

大憨熊 提交于 2020-01-16 04:49:06
问题 I cannot call an HttpConfiguration extension method: using System.Configuration; using System.Web.Http; ... var config = new HttpConfiguration(); config.MapHttpAttributeRoutes(); // <-- error Error: 'System.Web.Http.HttpConfiguration' does not contain a definition for 'MapHttpAttributeRoutes' and no extension method 'MapHttpAttributeRoutes' accepting a first argument of type 'System.Web.Http.HttpConfiguration' could be found (are you missing a using directive or an assembly reference?) I

Modify slice from string to string function to return last index if to string not found

别来无恙 提交于 2020-01-15 09:41:30
问题 I have a slice function which I got here. I was wondering how I can modify it so that if the to string is not found, but it found from it will return the end index of the entire string ( .count-1 ). Right now it's obviously crashing if I call .slice and there is no to string found. extension String { func slice(from: String, to: String) -> String? { return (range(of: from)?.upperBound).flatMap { substringFrom in (range(of: to, range: substringFrom..<endIndex)?.lowerBound).map { substringTo in

Why use an extension method if you are able to modify the source? [duplicate]

回眸只為那壹抹淺笑 提交于 2020-01-14 22:49:15
问题 This question already has answers here : When do you use extension methods, ext. methods vs. inheritance? (6 answers) Closed 8 days ago . Studying extension methods using the book as an example of expanding the functional structure of an Int32 ( int keyword) in C# (we cannot inherit structures and change the source code in the case of Int32 ), I realized that extension methods are needed for three things: If inheritance is not possible, as in the case with sealed classes; In case of the

Dynamics of HtmlHelper Extensions

霸气de小男生 提交于 2020-01-14 18:51:03
问题 I'm accustomed to extension methods being presented by intellisense, after the 'this' type is keyed-in. But when I try this with HtmlHelper, the extension methods don't show - even though the 'using' statement is present. Why is this? To clarify, I'm doing this test from within a regular .cs file, rather than a .cshtml file. No good reason, I'm simply playing with the MVC namespace and language to "see how it ticks." I still don't know why the intellisense doesn't pick up all 4000 extensions

Dynamics of HtmlHelper Extensions

两盒软妹~` 提交于 2020-01-14 18:50:10
问题 I'm accustomed to extension methods being presented by intellisense, after the 'this' type is keyed-in. But when I try this with HtmlHelper, the extension methods don't show - even though the 'using' statement is present. Why is this? To clarify, I'm doing this test from within a regular .cs file, rather than a .cshtml file. No good reason, I'm simply playing with the MVC namespace and language to "see how it ticks." I still don't know why the intellisense doesn't pick up all 4000 extensions

Adding generic extension methods to interfaces like IEnumerable

走远了吗. 提交于 2020-01-14 08:45:47
问题 I've been trying and trying to make my generic extension methods work, but they just refuse to and I can't figure out why . This thread didn't help me, although it should. Of course I've looked up how to, everywhere I see they say it's simple and it should be in this syntax: (On some places I read that I need to add "where T: [type]" after the parameter decleration, but my VS2010 just says that's a syntax error.) using System.Collections.Generic; using System.ComponentModel; public static

How to hide extension methods from derived classes?

北慕城南 提交于 2020-01-13 11:52:52
问题 I have a base abstract class to implement template method pattern. public abstract class OptionalParameter { //Template Method Pattern public string GenerateQueryString() { return this.GenerateQueryStringWithParameters(); } } And I have an extension method for all OptionalParameter type. public static class OptionalParameterExtensions { public static string GenerateQueryStringWithParameters(this OptionalParameter optionalParameters) { } } I inherited a class from OptionalParameter named

How to hide extension methods from derived classes?

爱⌒轻易说出口 提交于 2020-01-13 11:51:40
问题 I have a base abstract class to implement template method pattern. public abstract class OptionalParameter { //Template Method Pattern public string GenerateQueryString() { return this.GenerateQueryStringWithParameters(); } } And I have an extension method for all OptionalParameter type. public static class OptionalParameterExtensions { public static string GenerateQueryStringWithParameters(this OptionalParameter optionalParameters) { } } I inherited a class from OptionalParameter named