extension-methods

How to extend MVC3 Label and LabelFor HTML helpers?

时光毁灭记忆、已成空白 提交于 2019-11-27 15:07:03
Html.Label and Html.LabelFor helper methods do not support the htmlAttributes parameter like most of the other helpers do. I would like to set the class however. Something like this: Html.LabelFor(model => model.Name, new { @class = "control-label" }) Is there an easy way of extending Label/LabelFor without resorting to copying and extending the ILSpy disasm output of those methods? Iridio You can easily extend the label by creating your own LabelFor: Something like this should do what you need public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func

Doesn't C# Extension Methods allow passing parameters by reference?

♀尐吖头ヾ 提交于 2019-11-27 14:58:51
Is it really impossible to create an extension method in C# where the instance is passed as a reference? Here’s a sample VB.NET console app: Imports System.Runtime.CompilerServices Module Module1 Sub Main() Dim workDays As Weekdays workDays.Add(Weekdays.Monday) workDays.Add(Weekdays.Tuesday) Console.WriteLine("Tuesday is a workday: {0}", _ CBool(workDays And Weekdays.Tuesday)) Console.ReadKey() End Sub End Module <Flags()> _ Public Enum Weekdays Monday = 1 Tuesday = 2 Wednesday = 4 Thursday = 8 Friday = 16 Saturday = 32 Sunday = 64 End Enum Module Ext <Extension()> _ Public Sub Add(ByRef Value

Why is it impossible to declare extension methods in a generic static class?

。_饼干妹妹 提交于 2019-11-27 14:20:18
I'd like to create a lot of extension methods for some generic class, e.g. for public class SimpleLinkedList<T> where T:IComparable And I've started creating methods like this: public static class LinkedListExtensions { public static T[] ToArray<T>(this SimpleLinkedList<T> simpleLinkedList) where T:IComparable { //// code } } But when I tried to make LinkedListExtensions class generic like this: public static class LinkedListExtensions<T> where T:IComparable { public static T[] ToArray(this SimpleLinkedList<T> simpleLinkedList) { ////code } } I get "Extension methods can only be declared in

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

戏子无情 提交于 2019-11-27 14:16:50
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. 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 namespace="System.Collections.Generic" /> <add namespace="MyProject.Extensions" /> </namespaces> </pages> For

VB.NET: impossible to use Extension method on System.Object instance

自古美人都是妖i 提交于 2019-11-27 14:16:42
Can I make an Extension method for all the subclasses of System.Object (everything)? Example: <Extension> Public Function MyExtension(value As Object) As Object Return value End Function The above functions won't work for object instance: Dim myObj1 As New Object() Dim myObj2 = myObj1.MyExtension() The compiler does not accept it, is the problem in my computer? :) UPDATE The problem seems to occur only in VB, where members of object are looked-up by reflection ( late-bound ). UPDATE AFTER ANSWERED FYI, as vb has an advantage that C# lacks that is, members of imported Modules are imported to

Is there a performance hit for creating Extension methods that operate off the type 'object'?

空扰寡人 提交于 2019-11-27 13:55:31
I have a set of extension methods that I regularly use for various UI tasks. I typically define them to run off of type object , even though inside of them I'm typically converting them to string types. public static string FormatSomething(this object o) { if( o != null ) { string s = o.ToString(); /// do the work and return something. } // return something else or empty string. } The main reason I use type object and not string is to save myself in the UI from having to do <%#Eval("Phone").ToString().FormatSomething()%> when I can do <%#Eval("Phone").FormatSomething()%> instead. So, is it

AddRange to a Collection

Deadly 提交于 2019-11-27 13:50:31
A coworker asked me today how to add a range to a collection. He has a class that inherits from Collection<T> . There's a get-only property of that type that already contains some items. He wants to add the items in another collection to the property collection. How can he do so in a C#3-friendly fashion? (Note the constraint about the get-only property, which prevents solutions like doing Union and reassigning.) Sure, a foreach with Property. Add will work. But a List<T> -style AddRange would be far more elegant. It's easy enough to write an extension method: public static class

Overriding ToString() of List<MyClass>

喜夏-厌秋 提交于 2019-11-27 13:39:47
问题 I have a class MyClass, and I would like to override the method ToString() of instances of List: class MyClass { public string Property1 { get; set; } public int Property2 { get; set; } /* ... */ public override string ToString() { return Property1.ToString() + "-" + Property2.ToString(); } } I would like to have the following: var list = new List<MyClass> { new MyClass { Property1 = "A", Property2 = 1 }, new MyClass { Property1 = "Z", Property2 = 2 }, }; Console.WriteLine(list.ToString()); /

Pass a lambda expression in place of IComparer or IEqualityComparer or any single-method interface?

不想你离开。 提交于 2019-11-27 11:52:48
问题 I happened to have seen some code where this guy passed a lambda expression to a ArrayList.Sort(IComparer here) or a IEnumerable.SequenceEqual(IEnumerable list, IEqualityComparer here) where an IComparer or an IEqualityComparer was expected. I can't be sure if I saw it though, or I am just dreaming. And I can't seem to find an extension on any of these collections that accepts a Func<> or a delegate in their method signatures. Is there such an overload/extension method? Or, if not, is it

Why use TagBuilder instead of StringBuilder?

这一生的挚爱 提交于 2019-11-27 11:28:50
问题 what's the difference in using tag builder and string builder to create a table in a htmlhelper class, or using the HtmlTable? aren't they generating the same thing?? 回答1: TagBuilder is a class that specially designed for creating html tags and their content. You are right saying that result will be anyway a string and of course you still can use StringBuilder and the result will be the same, but you can do things easier with TagBuilder . Lets say you need to generate a tag: <a href='http:/