extension-methods

How can I render custom XSL controls in my XSLT file with C#?

I'm using C# to translate a XML file to HTML with the use of XSLT. I use an Extension object to render my own code: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" xmlns:widget="urn:ser...

Why are some HtmlHelper methods implemented as extension methods

I'm currently creating some custom helper classes, similar to ASP.NET MVC's standard HtmlHelper. When I looked at the implementation of HtmlHelper, I noticed that most/all of the HTML generating methods (such as ActionLink(), BeginForm(), TextBox(), and so on) are not implemented directly inside the HtmlHelper class, but as extension met...

How to convert this piece of code to Generics?

I have the following extension method that takes a List and converts it to a comma separated string: static public string ToCsv(this List<string> lst) { const string SEPARATOR = ", "; string csv = string.Empty; foreach (var item in lst) csv += item + SEPARATOR; // remove the trailing...

C# Extension methods on "members"

I have some extension methods which could be used like this: MyType myObject; string displayName = myObject.GetDisplayName(x => x.Property); The problem here is that it needs an instance, even if the extension method only needs the type MyType. So if there is no instance, it needs to be called like this: string displayName = BlahBla...

Can I use Extension Methods inline in an ASPX page?

Is it possible to do something like this inline in an ASPX page? <%= Me.SomeExtensionMethod() %> I can't seem to figure out how to get this to work properly. I'm receiving an error saying that "SomeExtensionMethod" is not a member of the current Page object. I've added the necessary <%@ Import Namespace="..." %> directive at the top o...

Intersect extension method, case sensitivity not working

Hello, I am trying to compare the contents of 2 collections in a unit test in .NET using MSTEST. To make things simple, instead of having to .Sort() and then loop through the collection and compare items one at a time, I've found the new and very cool .Intersect Extension method. It seems to work great by doing: Assert.AreEqu...

Linq To SQL problem - has no supported translation to SQL (problem with C# property)

I'm extending some Linq to SQL classes. I've got 2 similar statements, the 1st one works, the 2nd does not ("has no supported translation to SQL" error). var reg2 = rs.ProductRegistrations().SingleOrDefault(p => p.Product.product_name == "ACE") var reg5 = rs.ProductRegistrations().SingleOrDefault(p => p.product_name == "ACE"); After...

Does Array.ToArray<>() return the original array if it is the same type?

Hi! I deal with a framework on a daily basis where we sometimes provide methods that accept IEnumerable<MyBusinessObject> as a parameter in order to show user interfaces, perform calculations etc. If I pass in an array of MyBusinessObject like so: MyBusinessObject[] myArray = new MyBusinessObject { obj1, obj2, ..., objN }; frameworkCl...

Is using shared Dictionaries a good solution to the lack of "extension properties"?

Suppose I have some extension methods but also need to extend the object's state. Seeing as there is no support for extension properties in C#, would using shared static Dictionary be a good solution? For example something like this: class Foo { // 3rd party class } static class Helper { private static Dictionary<Foo, Gui...

MVC view can't find my extension method

I've created an extension method: namespace MyComp.Web.MVC.Html { public static class LinkExtensions { public static MvcHtmlString ActionImageLink(this HtmlHelper htmlHelper, string linkText, string imageSource, string actionName) { ... } } } I've referenced the assembly from my mvc app,...

Is there a way to use Linq projections with extension methods

I'm trying to use AutoMapper and a repository pattern along with a fluent interface, and running into difficulty with the Linq projection. For what it's worth, this code works fine when simply using in-memory objects. When using a database provider, however, it breaks when constructing the query graph. I've tried both SubSonic and Lin...

Extension methods overloading in C#, does it work?

Having a class that has a method, like this: class Window { public void Display(Button button) { // ... } } is it possible to overload the method with another one that is more broad, like this: class WindowExtensions { public void Display(this Window window, object o) { Button button = BlahBlah(o); ...

Why can't I call OrderBy in a class that extends List?

I have a class, Deck, that contains a method called Shuffle. I'm working on refactoring Deck to extend List<Card>, rather than having List<Card> Cards as a property. However, while Cards.OrderBy (a => Guid.NewGuid ()) worked, OrderBy (a => Guid.NewGuid ()) does not: Error CS0103: The name 'OrderBy' does not exist in the current context...

Recursive Linq Function and Yielding

public static IEnumerable<UIElement> Traverse(this UIElementCollection source) { source.OfType<Grid>().SelectMany(v => Traverse(v.Children)); //This is the top level. foreach (UIElement item in source) { yield return item; } } This never returns anything recursively. I have b...

Generic Sorting on List<T>

Hello, I have the following code: public class OMyObject { public int Id { get; set; } public string Value { get; set; } public DateTime? MyDate { get; set; } } I also have this code: public static class ObjectExtension { public static List<OMyObject> Sort<T>(this List<OMyObject> o, Func<OMyObject,...

Rewrite lambda extension method

Hi, I've created an extension method that works just like I wanted. I've noticed that somehow the party and property parameters are 'copied' into the lambda expression. This way I do not need to maintain a custom list of editor/party/property associations. However, I need to reset the ButtonEdit's ButtonClick event. Since this one is a...

Java subclass method returns zero value

Hi! I'm trying to to get a subclass method to return the variable from the superclass, however the return value keeps giving me empty returns. My guess would be that i'm missing some kind of reference to the super class. It's the value weight that returns value 0(zero) from the subclass method returnweight(). abstract class Vehicle{ ...

Why can I not call the DropDownList extensions methods from another extension method?

This extension method does not work on two separate development machines: public static string DdlTest(this HtmlHelper helper) { var si = new List<SelectListItem>(); si.Add(new SelectListItem() { Text = "1", Value = "1" }); si.Add(new SelectListItem() { Text = "2", Value = "2" }); return helper.DropDownList("test", si, n...

What is the collection version of SingleOrDefault for a Dictionary<T>?

Title kind of says it all. I just can't seem to find a DictionaryOrDefault \ ListOrDefault \ CollectionOrDefault option. Is there such a method? If not how do I do this: MyClass myObject = MyDictionary .SingleOrDefault(x => { if (x.Value != null) ...

Bitfield enum extension method to return dictionary of included values

Enums can sure be confusing. I am trying to create an extension method on the Enum type that will take a value and return the names of all the bits that match. Given: [Flags] public enum PlanetsEnum { Mercury=1, Venus=2, Earth=4, Mars=8, Jupiter=16, //etc.... } I would like to create an extension method that return a dic...