extension-methods

How to create an extension method in TypeScript for 'Date' data type

倾然丶 夕夏残阳落幕 提交于 2019-11-29 07:12:34
问题 I have tried to create an extension method in TypeScript based on this discussion ( https://github.com/Microsoft/TypeScript/issues/9 ), but I couldn't create a working one. Here is my code, namespace Mynamespace { interface Date { ConvertToDateFromTS(msg: string): Date; } Date.ConvertToDateFromTS(msg: string): Date { //conversion code here } export class MyClass {} } but its not working. 回答1: You need to change the prototype: interface Date { ConvertToDateFromTS(msg: string): Date; } Date

Using Extension Methods with .NET Framework 2.0

懵懂的女人 提交于 2019-11-29 07:11:34
Under Visual Studio 2008 Can I create an Extension Method to work under a .NET Framework 2.0 project? There is an ugly hack that gets Extension methods working in .Net 2.0; but it would better just to upgrade your framework to 3.5. Alternate Sources: 1 , 2 . In short (from link #2): Extension methods are just normal static methods tagged with the [Extension] attribute. This attribute is actually just added by the compiler behind the scenes. In .NET 3.5, it lives in System.Core, so just define your own attribute like this: namespace System.Runtime.CompilerServices { [AttributeUsage

Using ms: xpath functions inside XPathExpression

早过忘川 提交于 2019-11-29 06:54:22
I am trying to use Microsoft XPath Extension Functions (such as ms:string-compare http://msdn.microsoft.com/en-us/library/ms256114.aspx ) inside an XPathExpression object. These functions are extensions inside the MSXML library, and if I use them in an XslCompiledTransform (simply adding the "ms" namespace) they work like a charm: var xsl = @" <?xml version=""1.0"" encoding=""UTF-8""?> <xsl:stylesheet version=""2.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:fn=""http://www.w3.org/2005/xpath-functions"" xmlns:ms=""urn:schemas

How can I create .Net extension methods by C++/CLI?

不打扰是莪最后的温柔 提交于 2019-11-29 06:38:02
问题 In C#, extension methods can be created by public static class MyExtensions { public static ReturnType MyExt(this ExtType ext) { ... } } Since all of my library are written in C++/CLI, I would like to create the .net extension methods also in C++/CLI (in order to have one DLL instead of two). I've tried the following code static public ref class MyExtensions { public: static ReturnType^ MyExt(this ExtType ^ext) { ... } }; But the compiler can not recognize keyword 'this' in the first argument

Why can't static method in non-static class be an extension method? [duplicate]

。_饼干妹妹 提交于 2019-11-29 06:03:36
问题 Possible Duplicate: extension method requires class to be static In .NET: Why can't static method in non-static class be an extension method? 回答1: Eric Lippert will probably weigh in with a really good answer on this one, but the gist of it will probably be: We decided it would be easier on both programmers and the compiler if we limit the number of places that you have to look for extension methods. This policy tends to force users to put all of their extension methods into a few specific

Why doesn't the Controls collection provide all of the IEnumerable methods?

二次信任 提交于 2019-11-29 05:58:59
问题 I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me. I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax var c=Controls.Where(x => x.ID=="Some ID").SingleOrDefault(); However from what I can tell, Controls does implement the IEnumerable interface which provides such methods, so what gives? Why doesn't that just work? I have found a decent work around for this

If an extension method has the same signature as a method in the sealed class, what is the call precedence?

╄→гoц情女王★ 提交于 2019-11-29 05:51:37
I was reading about extension methods in C# 3.0. The text I'm reading implies that an extension method with the same signature as a method in the class being extended would be second in order of execution - that is, the method in the sealed class gets called. If this is the case, how can you extend the sealed class ? Indeed, the actual method takes precedence over the extension method. And just to make it clear - "order of execution" suggests both might be called; only the original method will be invoked. Perhaps pick another name / signature; you can't use extension methods to monkey-patch,

Do Extension Methods Hide Dependencies?

谁说胖子不能爱 提交于 2019-11-29 05:43:57
All, Wanted to get a few thoughts on this. Lately I am becoming more and more of a subscriber of "purist" DI/IOC principles when designing/developing. Part of this (a big part) involves making sure there is little coupling between my classes, and that their dependencies are resolved via the constructor (there are certainly other ways of managing this, but you get the idea). My basic premise is that extension methods violate the principles of DI/IOC. I created the following extension method that I use to ensure that the strings inserted into database tables are truncated to the right size:

Can I add an implicit conversion for two classes which I don't directly control?

岁酱吖の 提交于 2019-11-29 05:40:58
I'd like to be able to implicitly convert between two classes which are otherwise incompatible. One of the classes is Microsoft.Xna.Framework.Vector3 , and the other is just a Vector class used in an F# project. I'm writing a 3d game in C# with XNA, and -- although it's drawn in 3D, the gameplay takes place in only two dimensions (it's a birds-eye-view). The F# class takes care of the physics, using a 2D vector: type Vector<'t when 't :> SuperUnit<'t>> = | Cartesian of 't * 't | Polar of 't * float member this.magnitude = match this with | Cartesian(x, y) -> x.newValue(sqrt (x.units ** 2.0 + y

Extension method for List<T> AddToFront(T object) how to?

北慕城南 提交于 2019-11-29 05:28:00
I want to write an extension method for the List class that takes an object and adds it to the front instead of the back. Extension methods really confuse me. Can someone help me out with this? myList.AddToFront(T object); List<T> already has an Insert method that accepts the index you wish to insert the object. In this case, it is 0. Do you really intend to reinvent that wheel? If you did, you'd do it like this public static class MyExtensions { public static void AddToFront<T>(this List<T> list, T item) { // omits validation, etc. list.Insert(0, item); } } // elsewhere List<int> list = new