extension-methods

Extension method resolution

拥有回忆 提交于 2019-11-30 20:41:05
I wrote an extension method for String to get a char argument, string.Remove(char) . But when I used this, it instead called the default string.Remove(int) method. Shouldn't the presence of an actual method have higher priority than an implicit conversion? Instance methods have priority over extension methods. Your observation is proof of the same. When resolving which method to call, it will always pick a matching instance method over an extension method... which is intuitive in a way. Paraphrased from C# in depth, When the compiler sees that you're trying to call a method which looks like an

IQueryable (non generic) : missing Count and Skip ? it works with IQueryable<T>

被刻印的时光 ゝ 提交于 2019-11-30 20:40:00
i have an extension method which a person was really helpful to give me... it does an orderby on IQueryable ... but i wanted one to do a normal IQueryable (non generic) Here is the code, The count and Skip and i think Take are missing . public static IQueryable GetPage(this IQueryable query, int page, int pageSize, out int count) { int skip = (int)((page - 1) * pageSize); count = query.Count(); //COUNT DOESN'T EXIST return query.Skip(skip).Take((int)pageSize); // NEITHER SKIP } Here is the and it works perfectly no errors. public static IQueryable<T> GetPage<T>(this IQueryable<T> query, int

How does VB.NET compiler choose which extension overload to run?

試著忘記壹切 提交于 2019-11-30 20:13:36
Got an interesting oddity - thought someone might be able to help. This came out of some fun with nullable types from this question: How to check if an object is nullable? Option Strict On Module Test ' Call this overload 1 <Extension()> Function IsNullable(obj As ValueType) As Boolean Return False End Function ' Call this overload 2 <Extension()> Function IsNullable(Of T As {Structure})(obj As Nullable(Of T)) As Boolean Return True End Function Sub Test() ' a is an integer! Dim a As Integer = 123 ' calling IsNullable as an extension method calls overload 1 and returns false Dim result1 As

Custom HtmlHelper extension method not available in View?

 ̄綄美尐妖づ 提交于 2019-11-30 17:51:50
问题 I have translated Jeremiah Clark's CheckBoxList Helper for MVC into my VB.Net project but when I try to use the method in my view I get the error 'CheckBoxList' is not a member of 'System.Web.Mvc.HtmlHelper(Of Attenda.Stargate.Web.UserRolesViewModel)'. Can anyone tell me where I have gone wrong? Helper module: Imports System.Runtime.CompilerServices Public Module InputExtensions <Extension()> _ Public Function CheckBoxList(ByVal htmlHelper As HtmlHelper, ByVal name As String, ByVal listInfo

How to Mock (with Moq) Unity methods

被刻印的时光 ゝ 提交于 2019-11-30 17:38:12
Extension methods are not good for testing (that's described here: Mocking Extension Methods with Moq , http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx ). But probably there are some solutions for mocking of Unity methods? In my case I have the following function: public class MyManager { public MyManager(IUnityContainer container) : base(container) { } public IResult DoJob(IData data) { IMyLog log = MyContainer.Resolve<IMyLog>(); ... use log.Id ... MyContainer.Resolve<...>();//usage for other purposes... } I want to be sure that 'DoJob' method will

How to define an extension method in a scriptcs csx script

不问归期 提交于 2019-11-30 17:29:47
I'm playing with ScriptCS (which is awesome!) but I couldn't figure out how to define an extension method within a .csx script file . Take this example: using System.IO; public static class Extensions { public static string Remove(this string source, params string[] toRemove) { foreach(var r in toRemove) { source = source.Replace(r,""); } return source; } } string[] entries = Directory .GetFiles( @"C:\Users\blah\blah", "*.mp4", SearchOption.AllDirectories) .Select( p => p.Remove("Users")) .ToArray(); foreach(var e in entries) { Console.WriteLine(e); } This yields the error: error CS1109:

Extension methods in referenced assemblies?

我与影子孤独终老i 提交于 2019-11-30 14:56:07
问题 If I try to call my extension method which is defined like this: Module LinqExtensions <System.Runtime.CompilerServices.Extension()> _ Public Function ToSortableBindingList(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IBindingList If (source Is Nothing) Then Throw New ArgumentNullException("source") End If Return New SortableBindingList(Of TSource)(New List(Of TSource)(source)) End Function End Module by calling Dim sss As String() sss.AsEnumerable.ToSortableBindingList() it gives

Where do I put my extension method?

隐身守侯 提交于 2019-11-30 14:30:52
问题 A senior member here gave me this code: public static string Truncate(this string value, int maxChars) { return value.Length <= maxChars ? value : value.Substring(0, maxChars) + " .."; } He said to use it as an extension method. But where do I put this method? It looks like it adds something to .Net 回答1: Consider a class named StringExtensions like so: static class StringExtensions { public static string Truncate(this string value, int maxChars) { return value.Length <= maxChars ? value :

rolling my own @Html.BeginfBrm()

六月ゝ 毕业季﹏ 提交于 2019-11-30 14:08:53
问题 I am writing a custom validation set that will display all missing elements on a div. I'd like to be able to use a custom @Html.BeginForm() method that will write out that div but I'm really not sure where to even begin as this nut is a little tougher to crack than just a html extension that writes out a Tag or String (the form encapsulates data/controls and is closed by } at the end). I looked at the metadata version of the built in BeginForm() method and it wasn't much help to me.

evaluating cost/benefits of using extension methods in C# => 3.0 [closed]

ⅰ亾dé卋堺 提交于 2019-11-30 13:56:45
In what circumstances (usage scenarios) would you choose to write an extension rather than sub-classing an object ? < full disclosure : I am not an MS employee; I do not know Mitsu Furota personally; I do know the author of the open-source Componax library mentioned here, but I have no business dealings with him whatsoever; I am not creating, or planning to create any commercial product using extensions : in sum : this post is from pure intellectal curiousity related to my trying to (continually) become aware of "best practices" > I find the idea of extension methods "cool," and obviously you