extension-methods

What does 'this' keyword mean in a method parameter? [duplicate]

强颜欢笑 提交于 2019-11-27 03:10:40
问题 This question already has an answer here: What are Extension Methods? 11 answers namespace System.Web.Mvc.Html { // Summary: // Represents support for HTML in an application. public static class FormExtensions { public static MvcForm BeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName); ... } } I have noticed that 'this' object in front of the first parameter in BeginForm method doesn't seem to be accepted as a parameter. Looks like in real BeginForm methods

Extension Methods not Recognized

情到浓时终转凉″ 提交于 2019-11-27 03:01:53
问题 What is necessary to have an extension method honored when it exists in an imported assembly? I built one in a class library project but it is not recognized in my web project which references the library. All the other classes and methods in the library are honored and visible but this extension method is not. The extension method is visible when used within the library. 回答1: Referencing an assembly containing a class with extension methods is not enough. You need to import the namespace

Is it possible to implement mixins in C#?

北城余情 提交于 2019-11-27 02:58:07
I've heard that it's possible with extension methods, but I can't quite figure it out myself. I'd like to see a specific example if possible. Thanks! It really depends on what you mean by "mixin" - everyone seems to have a slightly different idea. The kind of mixin I'd like to see (but which isn't available in C#) is making implementation-through-composition simple: public class Mixin : ISomeInterface { private SomeImplementation impl implements ISomeInterface; public void OneMethod() { // Specialise just this method } } The compiler would implement ISomeInterface just by proxying every member

Workarounds for using custom methods/extension methods in LINQ to Entities

会有一股神秘感。 提交于 2019-11-27 02:49:13
问题 I have defined a GenericRepository class which does the db interaction. protected GenericRepository rep = new GenericRepository(); And in my BLL classes, I can query the db like: public List<Album> GetVisibleAlbums(int accessLevel) { return rep.Find<Album>(a => a.AccessLevel.BinaryAnd(accessLevel)).ToList(); } BinaryAnd is an extension method which checks two int values bit by bit. e.g. AccessLevel=5 => AccessLevel.BinaryAnd(5) and AccessLevel.binaryAnd(1) both return true. However I cannot

Using reflection to check if a method is “Extension Method”

烈酒焚心 提交于 2019-11-27 02:39:07
问题 As part of my application I have a function that receives a MethodInfo and need to do specific operations on it depending if that method is "Extension Method". I've checked the MethodInfo class and I could not find any IsExtension property or flag that shows that the method is extension. Does anyone knows how can I find that from the method's MethodInfo? 回答1: Based on F# extension methods in C# it seems there is an attribute on the compiled form. So see if the method has this attribute: http:

Extension method on enumeration, not instance of enumeration

我的梦境 提交于 2019-11-27 00:58:46
I have an enumeration for my Things like so: public enum Things { OneThing, AnotherThing } I would like to write an extension method for this enumeration (similar to Prise's answer here ) but while that method works on an instance of the enumeration , ala Things thing; var list = thing.ToSelectList(); I would like it to work on the actual enumeration instead: var list = Things.ToSelectList(); I could just do var list = default(Things).ToSelectList(); But I don't like the look of that :) I have gotten closer with the following extension method: public static SelectList ToSelectList(this Type

Try-with-resources in Kotlin

僤鯓⒐⒋嵵緔 提交于 2019-11-27 00:39:54
问题 When I tried to write an equivalent of a Java try -with-resources code in Kotlin, it didn't work for me. I tried different variations of the following: try (writer = OutputStreamWriter(r.getOutputStream())) { // ... } But neither works. Does anyone know what should be used instead? Apparently Kotlin grammar doesn't have definition for such a construct, but maybe I'm missing something. It defines grammar for try block as follows: try : "try" block catchBlock* finallyBlock?; 回答1: There is use

Scala equivalent of C#’s extension methods?

偶尔善良 提交于 2019-11-27 00:00:11
问题 In C# you can write: using System.Numerics; namespace ExtensionTest { public static class MyExtensions { public static BigInteger Square(this BigInteger n) { return n * n; } static void Main(string[] args) { BigInteger two = new BigInteger(2); System.Console.WriteLine("The square of 2 is " + two.Square()); } }} How would this simple extension method look like in Scala? 回答1: The Pimp My Library pattern is the analogous construction: object MyExtensions { implicit def richInt(i: Int) = new {

Is extending String class with IsNullOrEmpty confusing?

≯℡__Kan透↙ 提交于 2019-11-26 23:10:00
问题 Everyone knows and love String.IsNullOrEmpty(yourString) method. I was wondering if it's going to confuse developers or make code better if we extend String class to have method like this: yourString.IsNullOrEmpty(); Pro: More readable. Less typing. Cons: Can be confusing because yourString variable can be null and it looks like you're executing method on a null variable. What do you think? The same question we can ask about myObject.IsNull() method. That how I would write it: public static

How do I use an extension method in an ASP.NET MVC View?

跟風遠走 提交于 2019-11-26 22:23:44
问题 How do I access an extension method in an ASP.Net MVC View? In C# I do using MyProject.Extensions; and I remember seeing an XML equivalent to put in a view, but I can't find it anymore. 回答1: In View: <%@ Import Namespace="MyProject.Extensions" %> Or in web.config (for all Views): <pages> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="System.Linq" /> <add