extension-methods

How do I create an extension method (F#)? [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-04 16:25:06
问题 This question already has answers here : F# extension methods in C# (4 answers) Closed 6 years ago . How do I create an extension method in F#, for example, like this C# extension: public static string Right(this string host, int index) { return host.Substring(host.Length - index); } 回答1: For an F# extension that can be called from F#: type System.String with member x.Right(index) = x.Substring(x.Length - index) Note that as of Beta 1, this doesn't result in a C#-compatible extension method.

How to check if one C++ class extends another (like if that another one was an interface)?

ぃ、小莉子 提交于 2019-12-04 16:20:00
So generally having class A { ... }; class B { ... }; class C: public A, public B {}; // C inherits from A and B. when we create an instance of C and want to pass it into some function ho do we check if class we pass to a function is extending A? C is defined as inheriting from A so there is no need to check: It is mandatory that an instance of C is also a A (and a B ). However, if you have a function taking a A as a parameter, you can use dynamic_cast<> to check if the instance is actually a C : void function(const A& a) { const C* c = dynamic_cast<const C*>(&a); if (c) { // a is an instance

Create a helper function that returns Razor @ tags?

耗尽温柔 提交于 2019-12-04 15:01:06
Struggling to find an answer to my question, as I'm not exactly sure what 'type' a razor tag is. Essentially I want to create a helper that does something along these lines: public static xxxxxxx ScriptTag(this HtmlHelper htmlHelper, string url) { return @<script type="text/javascript" src="@Url.Content("~/" + url)" />; } The reason I want this is that I am implementing the extension methods that are outlined in this post . Basically rather than have to do: @Html.Resource(@<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>, "js")` I want to be able to

Is there a way I can dynamically define a Predicate body from a string containing the code?

情到浓时终转凉″ 提交于 2019-12-04 13:48:26
This is probably a stupid question, but here goes. I would like to be able to dynamically construct a predicate < T > from a string parsed from a database VARCHAR column, or any string, for that matter. For example, say the column in the database contained the following string: return e.SomeStringProperty.Contains("foo"); These code/string values would be stored in the database knowing what the possible properties of the generic "e" is, and knowing that they had to return a boolean. Then, in a magical, wonderful, fantasy world, the code could execute without knowing what the predicate was,

Can a Website detect what browser extensions are being used?

梦想的初衷 提交于 2019-12-04 13:45:38
问题 Can a website detect what browser extensions are being used? Specifically in this case, the author of the extension wishes to prevent websites from identifying when users are using the extension. So, for a generic example, could the programmers at Yahoo! write code so that www.yahoo.com could tell when it's users were using Firebug? And if Yahoo! could do this, is there anything that the makers of Firebug could do to prevent this? 回答1: Looks like you can detect some of them in Firefox using

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

孤人 提交于 2019-12-04 12:53:20
问题 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

Experience with fluent interfaces? I need your opinion!

≯℡__Kan透↙ 提交于 2019-12-04 12:47:28
Sorry for this long question, it is flagged wiki since I'm asking for something that might not have a very concrete answer. If it is closed, so be it. My main question is this: How would you write a fluent interface that isn't fully defined in the base classes, so that programs that uses the fluent interfaces can tack on new words inside the existing structure, and still maintain a guiding interface so that after a dot, the intellisense only lists the keywords that actually apply at this point. I'm on my 3rd iteration of rewriting my IoC container. The 2nd iteration was to improve performance,

Passing interface as a parameter to an extension method

给你一囗甜甜゛ 提交于 2019-12-04 11:33:31
I have used extension methods to extend html helpers to make an RSS repeater: public static string RSSRepeater(this HtmlHelper html, IEnumerable<IRSSable> rss) { string result=""; foreach (IRSSable item in rss) { result += "<item>" + item.GetRSSItem().InnerXml + "</item>"; } return result; } So I make one of my business objects implement IRSSable, and try to pass this to the HTML helper. But I just cannot seem to make it work, I have tried: <%=Html.RSSRepeater(ViewData.Model.GetIssues(null, null, "") as IEnumerable<IRSSable>) %> Compiles fine, but null is passed <%=Html.RSSRepeater(ViewData

C# Extension Method for Object

旧城冷巷雨未停 提交于 2019-12-04 09:53:32
问题 Is it a good idea to use an extension method on the Object class? I was wondering if by registering this method if you were incurring a performance penalty as it would be loaded on every object that was loaded in the context. 回答1: In addition to another answers: there would be no performance penalty because extension methods is compiler feature. Consider following code: public static class MyExtensions { public static void MyMethod(this object) { ... } } object obj = new object(); obj

What is the motivation behind “Use Extension Methods Sparingly?”

爷,独闯天下 提交于 2019-12-04 09:20:44
问题 I find them a very natural way to extend existing classes, especially when you just need to "spot-weld" some functionality onto an existing class. Microsoft says, "In general, we recommend that you implement extension methods sparingly and only when you have to." And yet extension methods form the foundation of Linq; in fact, Linq was the reason extension methods were created. Are there specific design criteria where using extension methods are perferred over inheritance or composition? Under