extension-methods

How I can find all extension methods in solution?

我们两清 提交于 2019-12-03 00:07:55
How I can find all extension methods in solution ? If I were doing it I would search all files for the string "( this " -- your search string may differ based on your formatting options. EDIT : After a little bit of experimentation, the following seems to work for me with high precision using "Find in Files" (Ctrl-Shift-F) Search string: " \( this [A-Za-z] " (minus quotes, of course) Match case: unchecked Match whole word: unchecked Use: Regular Expressions Look at these file types: "*.cs" I'd look at the generated assemblies using reflection; iterate through the static types looking for

python extension methods

主宰稳场 提交于 2019-12-03 00:00:05
OK, in c# we have something like: public static string Destroy(this string s) { return ""; } So basically, when you have a string you can do: str = "This is my string to be destroyed"; newstr = str.Destroy() # instead of newstr = Destroy(str) Now this is cool because in my opinion it's more readable. Does python have something similar? I mean instead of writing like this: x = SomeClass() div = x.getMyDiv() span = x.FirstChild(x.FirstChild(div)) # so instead of this I'd like to write: span = div.FirstChild().FirstChild() # which is more readable to me Any suggestion? You can just modify the

What is the best way to extend null check?

删除回忆录丶 提交于 2019-12-02 22:50:36
You all do this: public void Proc(object parameter) { if (parameter == null) throw new ArgumentNullException("parameter"); // Main code. } Jon Skeet once mentioned that he sometimes uses the extension to do this check so you can do just: parameter.ThrowIfNull("parameter"); So I come of with two implementations of this extension and I don't know which one is the best. First: internal static void ThrowIfNull<T>(this T o, string paramName) where T : class { if (o == null) throw new ArgumentNullException(paramName); } Second: internal static void ThrowIfNull(this object o, string paramName) { if

I have two Kotlin extension methods for the same class, but with a different generic signatures and the compiler complains

大城市里の小女人 提交于 2019-12-02 20:46:54
I am writing two extension functions for the same class: class Something<T:Any> { ... } They look like: fun Something<Int>.toJson(): String = ... fun Something<Double>.toJson(): String = ... And results in the compiler error: Kotlin: Platform declaration clash: The following declarations have the same JVM signature How can I create two extension functions with only the generics signature differing? or is it not possible? Note: this question is intentionally written and answered by the author ( Self-Answered Questions ), so that the answers to commonly asked Kotlin topics are present in SO. It

Extension methods overridden by class gives no warning

Deadly 提交于 2019-12-02 20:36:10
I had a discussion in another thread, and found out that class methods takes precedence over extension methods with the same name and parameters. This is good as extension methods won't hijack methods, but assume you have added some extension methods to a third party library: public class ThirdParty { } public static class ThirdPartyExtensions { public static void MyMethod(this ThirdParty test) { Console.WriteLine("My extension method"); } } Works as expected: ThirdParty.MyMethod -> "My extension method" But then ThirdParty updates it's library and adds a method exactly like your extension

nUnit testing a controller extension method

萝らか妹 提交于 2019-12-02 20:08:47
问题 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

Best practices: C# Extension methods namespace and promoting extension methods

 ̄綄美尐妖づ 提交于 2019-12-02 20:03:52
I know there exists already a post , describing nearly the same, but I think mine is a bit different. What I would like to know is how you organize your extension methods in terms of assigning the namespace. Currently - for the extension methods in our framework - I use the following namespace pattern MyCompany.Web.Utils and inside I have the extension method classes. This is fine for me with the disadvantage that the extenders are not immediately visible to our software developers. Consider the case where I have a StringExtender class which provides a quite handy extension method "In" that

How to compare nullable types?

谁都会走 提交于 2019-12-02 18:58:38
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 easier way to see if two nullable types are the same? Update: Turns out that the reason this method existed

Can C# extension methods access private variables?

半世苍凉 提交于 2019-12-02 18:40:52
Is it possible to access an object's private variables using an extension method? No. You can do the same in an extension method as in a "normal" static method in some utility class. So this extension method public static void SomeMethod(this string s) { // do something with 's' } is equivalent to some static helper method like this (at least regarding what you can access): public static void SomeStringMethod(string s) { // do something with 's' } (Of course you could use some reflection in either method to access private members. But I guess that's not the point of this question.) Zaid Masud

extension function screenshot not capturing the same area in iphone and ipad

牧云@^-^@ 提交于 2019-12-02 17:59:53
问题 I am using a extension function to take a screenshot of a uiview. The problem is that the results look very different. I want the photo to look the same regardless of weather its a ipad or iphone. I am manually entering the constraints so I expect the images to be the same. I want to using the function to go to the orgin or center of the viewcontroller and be 200 height and 200 width. So the image should be the same regardless of the device because it starts in the center and is the same size