extension-methods

@Html.EditorFor(m => m) lambda syntax in MVC

依然范特西╮ 提交于 2019-12-03 11:48:54
问题 I'm just learning C# and MVC, and trying to understand some examples. @Html.EditorFor(m => m) Eventually I figured out that '=>' is the lambda operator, and that it means something like "m such that m". That doesn't really make any sense to me. Why not just pass in m? Also, I don't see m defined in any view that I'm working with. Model is defined, and allegedly that's what this method is picking up. How does that work? Finally, I looked at the definition for Html.EditorFor, and don't see any

Get attribute values from matching XML nodes using XPath query

与世无争的帅哥 提交于 2019-12-03 11:47:46
This doesn't seem like it should be difficult, but I'm stuck currently. I'm trying to get the attribute values for a particular attribute from nodes that match a given XPath query string. Here's what I have so far: public static IEnumerable<string> GetAttributes(this XmlDocument xml, string xpathQuery, string attributeName) { var doc = new XPathDocument(new XmlNodeReader(xml)); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr = nav.Compile(xpathQuery); XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()) { XPathNavigator curNav = iterator.Current; if

Adding an extension method to the string class - C#

不问归期 提交于 2019-12-03 11:42:57
问题 Not sure what I'm doing wrong here. The extension method is not recognized. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using StringExtensions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { RunTests(); } static void RunTests() { try { ///SafeFormat SafeFormat("Hi There"); SafeFormat("test {0}", "value"); SafeFormat("test missing second value {0} - {1}", "test1"); SafeFormat("{0}

How to call extension methods using Eval in a databound control

混江龙づ霸主 提交于 2019-12-03 11:08:44
I have a simple extension method on the int type so I can do the following: string timeLength = 61.ToTime() // timeLength will be "1:01" This works great in code, but I want to use this extension method in a Repeater Template. When databinding I want to do the following: <%# Eval("LengthInSeconds").ToTime() %> That didn't work so I tried: <%# ((int) Eval("LengthInSeconds")).ToTime() %> and it still didn't work. The JIT compiler is not seeing my extension method and I do have the proper import statement in the page. My only idea for solving this is to replace the Eval with a Literal control and

How do extension methods work under-the-hood?

时光怂恿深爱的人放手 提交于 2019-12-03 10:59:47
A contractor where I work is using extension methods to implement CRUD on well-known internal classes that we own . I say it is better to use normal inheritance over extension methods for the following reasons. Using extension methods obfuscates, hides & confuses the source of the CRUD methods. I assume extension methods make heavy use of reflection (which is slower). His logic is, "It's compiled, so it's fast." Maybe I'm wrong...but just because it is compiled doesn't mean it doesn't use reflection, nor does it mean it is faster than normal inheritance. So my questions are: How do extension

How I can find all extension methods in solution?

≯℡__Kan透↙ 提交于 2019-12-03 09:38:18
问题 How I can find all extension methods in solution ? 回答1: 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: "*

Python extension methods

你说的曾经没有我的故事 提交于 2019-12-03 08:26:53
问题 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 =

Is there any way to overload extension methods in C#?

蓝咒 提交于 2019-12-03 08:03:53
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS : PARENTCLASS{...} public static class EXTENSION{ public static METHOD(this PARENTCLASS parent){...} public static METHOD(this CHILD_A_CLASS child) {...} } Something like above, of course there will be more child (and grandchild) classes but I just put one of them. The problem is, when I called the extension method like the following: PARENTCLASS cc = new CHILD_A_CLASS(); cc.METHOD(); It will execute the PARENT Extension Method instead of my-expected CHILD extension method. Anyone have idea

C# Extension method for checking attributes

偶尔善良 提交于 2019-12-03 07:24:57
问题 Sorry if this is a stupid noob question please be gentle with me I'm trying to learn... I want to test against the attribute methods of things like models and controllers. Mostly to make sure they have the right attrbute ie Required. But i'm also using this as an experiment with extension methods and Lambdas. What I'd like is a method that when implimented looks some thing like Controller controller = new Controller(); controller.MethodName(params).HasAttribute<AttributeName>(); Iveused

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

一世执手 提交于 2019-12-03 07:11:59
问题 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