2
голосов
4ответов
2407 просмотров

Controlling Output Indentation in ASP.Net MVC

My colleague is extremely 'hot' on properly formatted and indented html being delivered to the client browser. This is so that the page source is easily readable by a human. Firstly, if I have a partial view that is used in a number of different areas in my site, should the rendering engine be a...

1
голосов
2ответов
290 просмотров

extending a class that doesn't implement an interface

I'd like to override the Serialize methods of the ASP.NET JavaScriptSerializer class. Nothing too fancy, I just want to do some additional post processing to the serialized string returned from .NET. Unfortunately, none of the methods on this class are declared virtual and the class itself does...

1
голосов
2ответов
1441 просмотров

How do I write a generic extension method to convert a List to a HashSet using HashSet.AddRange()?

HashSet does not have an AddRange method, so I want to write an extension method for it. This is what I have: public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> list) { foreach (var item in list) { collection.Add(item); } } I hav...

7
голосов
4ответов
2242 просмотров

How to use extensions and utility methods in markup?

Ok. This is probably a really stupid question, but I'm going to ask it anyway... How can I use extensions and utility methods in my ASP.Net markup? For example, (say) I have a DateTime extension method called "ToExampleString()" (contained in the DateTimeExtensions class in my Common.Extensions ...

2
голосов
2ответов
2005 просмотров

Problem getting generic extension method to work correctly

I'm trying to create the extension method AddRange for HashSet so I can do something like this: var list = new List<Item>{ new Item(), new Item(), new Item() }; var hashset = new HashSet<Item>(); hashset.AddRange(list); This is what I have so far: public static void AddRange<T&...

0
голосов
1ответов
822 просмотров

Generic Concat Extension method for Paramarrays not working for IEnumerable(of String)

Inspired by Javascripts variable Arguments in Max()/Min() and list comprehension in functional languages I tried to get the same in VB.NET using Generic Extension methods given IEnumerable(of T) as resulttype. This works well excepts for strings. Why? These kind of extension methods may be cons...

2
голосов
3ответов
140 просмотров

Extension method on type

Is there a way to create an extension method for an type ? I only seem to be able to create them for instances. public static class MyExtensions { public static string Test(this string s) { return "test"; } } public class Test { static void TestIt() { string....

0
голосов
4ответов
1371 просмотров

Extension methods + Console apps

Ok, i'm well and truly stumped here. I've written extension methods before, and never had any problems. However, i've never had to use them in Console Apps. The following code won't compile and I have no idea why! I created a simple console app to try it out and it just won't work: using System...

2
голосов
3ответов
1384 просмотров

LINQ: Checking if a field (db) contains items in an ILIST?

I am trying to create an extension method that i can forward a IList of statuses and check weather they exist, I thought the best way to do this was an ILIST - but maybe i wrong? Is this the best way to pass multple items to a method - A List? Its a generic LIST so hence no conversion etc from Ob...

2
голосов
6ответов
267 просмотров

using generics in c# extension functions

I am using generics to translate Java code to C# and having trouble with containers of the sort: public static class MyExtensions { public static void add(this List<object> list, object obj) { list.Add(obj); } public static void add(this List<string> list, str...

3
голосов
4ответов
13533 просмотров

C# - Returning an Enum? from a static extension method

I've added some extension methods for strings to make working with some custom enums easier. public static Enum ToEnum<T>(this string s) { return (Enum)Enum.Parse(typeof(T), s); } public static bool IsEnum<T>(this string s) { return Enum.IsDefined(typeof(T), s); } Note -- ...

1
голосов
3ответов
1791 просмотров

Are C# extension methods only available for instance methods?

I have recently started to make useful use of C# extension methods. The SO examples and the documentation suggest that they are only used for instance methods (i.e. with the this keyword). It is possible to use them (or another approach) with static/class methods? (My particular requirement is ...

1
голосов
5ответов
2623 просмотров

Extensions library in C#

I am looking for a good Extensions library (library with a great number of extensions methods) in C# for standard type. I have found some libraries on Codeplex, but they are considered to be very weak by me. Edit: I need a library which implements different mathematical functions for different (...

71
голосов
7ответов
29694 просмотров

Can C# extension methods access private variables?

Is it possible to access an object's private variables using an extension method?

4
голосов
2ответов
2041 просмотров

ICollection / ICollection<t> ambiguity problem</t>

Just want to make simple extension for syntactic sygar : public static bool IsNotEmpty(this ICollection obj) { return ((obj != null) &amp;&amp; (obj.Count &gt; 0)); } public static bool IsNotEmpty&lt;T&gt;(this ICollection&lt;T&gt; obj) { return ((obj != null) &amp;&amp;...

5
голосов
2ответов
1964 просмотров

c# - Why can't my Dictionary class see the ToArray() method?

I see in the API Dictionary has a ToArray() method (in the extension classes area), but when I try to use this from my Dictionary instance it can't see it??? How do I "enable" ToArray() for my Dictionary instance? Thanks

34
голосов
5ответов
10270 просмотров

FindAll vs Where extension-method

I just want know if a "FindAll" will be faster than a "Where" extentionMethod and why? Example : myList.FindAll(item=&gt; item.category == 5); or myList.Where(item=&gt; item.category == 5); Which is better ?

10
голосов
4ответов
1445 просмотров

Extension methods for specific generic types

I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable&lt;int&gt; with member this.foo = this.ToString...

8
голосов
2ответов
2452 просмотров

Operators as method parameters in C#

I don't think it's possible to use operators as a parameters to methods in C# 3.0 but is there a way to emulate that or some syntactic sugar that makes it seem like that's what's going on? I ask because I recently implemented the thrush combinator in C# but while translating Raganwald's Ruby exa...

26
голосов
2ответов
5502 просмотров

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

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); }

32
голосов
1ответов
13853 просмотров

Using extension methods in .NET 2.0?

I want to do this, but getting this error: Error 1 Cannot define a new extension method because the compiler required type 'System.Runtime.CompilerServices.ExtensionAttribute' cannot be found. Are you missing a reference to System.Core.dll? [snipped some path stuff] I have seen some a...

2
голосов
2ответов
1407 просмотров

C#, WinForms and Extension Methods

The Question Aside from all the obvious answers, what would cause extension methods to generate compiler errors like this one: 'DataType' does not contain a definition for 'YourExtensionMethodName' I've got a real stumper here, and it's spelled out for you in detail below. I've exhausted all t...

7
голосов
2ответов
1471 просмотров

Extending F# List Module

I've been adding a few handy methods to some of the F# modules such as List. type Microsoft.FSharp.Collections.FSharpList&lt;'a&gt; with //' static member iterWhile (f:'a -&gt; bool) (ls:'a list) = let rec iterLoop f ls = match ls with | head :: tai...

0
голосов
1ответов
1059 просмотров

How to ensure HTTPContext has value?

I am building a version of the MVC Storefront. I have two themes, one is the standard ui, and the other is the admin ui. I have some HTML Helper methods, which deliver .ascx files to the .aspx files just like the example, but without plug-ins. All the stuff works on the default theme, but none of...

11
голосов
9ответов
8226 просмотров

Extension Methods - IsNull and IsNotNull, good or bad use?

I like readability. So, I came up with an extension mothod a few minutes ago for the (x =! null) type syntax, called IsNotNull. Inversly, I also created a IsNull extension method, thus if(x == null) becomes if(x.IsNull()) and if(x != null) becomes if(x.IsNotNull()) However, I'm worried I ...

1
голосов
2ответов
1670 просмотров

LINQ - Distinct() returning a different value if extension method used

I have a LINQ query which is attempting to get all of the distinct months of all of the dates in a table. I had this working using the Distinct() extension method. I then made it more readable by using an extension method to extract the month. And then it stopped returning Distinct results. Can...

0
голосов
2ответов
989 просмотров

Why is System.Windows.Shape.Path sealed?

I was trying to extend the shape class to contain an additional variable but have found the class is sealed. How can I achive this simply, using an alternate implementation approach? Is creating a new class and storing a shape an passing all the method calls through the easiest approach; I'm sur...

112
голосов
8ответов
112254 просмотров

AddRange to a Collection

A coworker asked me today how to add a range to a collection. He has a class that inherits from Collection&lt;T&gt;. 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-fri...

0
голосов
3ответов
746 просмотров

IronRuby calling C# Extension Methods - Error - Compatibility in .NET 3.5

I have written an Extension Method off of DataGridView called HideColumns. public static class Extensions { public static void HideColumns(this DataGridView dataGridView, params string[] columnNames) { foreach (string str in columnNames) { if (dataGridView.Col...

1
голосов
5ответов
206 просмотров

php extending classes

I'm using a salesforce class called SforceEnterpriseClient. I've referenced that class many places in my application. I want to extend that class to give it the ability to return a single array from a 1 row recordset, right now the record set is about 3 levels deep. There's a few other things ...