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...
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...
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...
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...
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...
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...
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...
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...
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...
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,...
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...
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);
...
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...
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...
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,...
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...
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{
...
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...
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)
...
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...