extension-methods

Gridview with Ajax Toolkit CalendarExtender not displaying in Edit Template

I have the folling Gridview column <asp:TemplateField HeaderText="Installed Date" SortExpression="install_date"> <EditItemTemplate> <asp:TextBox ID="gvtxtInstalledDate" runat="server" Text='<%# Bind("install_date") %>'></asp:TextBox> <asp:CalendarExtender ID="gvtxtInstalledDate_CalendarExtender" runat="s...

Generic extension methods in C#: what will happen in this edge case?

In a recent question of mine I learned that if there are more than one extension methods with constraints that match the given type, the most specific one will be chosen. This got me thinking - how does the compiler determine which one is "more specific"? And what will the outcome be? Let's say I have the following classes: public MyCl...

How do you use an extension method in an InvokeMethod workflow?

I have a System.Activities.Statements.InvokeMethod activity. I'm trying to activate it with TargetType: (null) TargetObject: licxFiles //an ICollection<string> MethodName: StoreIfHasValue The StoreIfHasValue: public static void StoreIfHasValue(this ICollection<string> collection, string value) { if (value.IsNullOrEmpty( )...

ActiveRecord - replace model validation error with warning

Hi, I want to be able to replace a field error with a warning when saving/updating a model in rails. Basically I want to just write a wrapper around the validation methods that'll generate the error, save the model and perhaps be available in a warnings hash (which works just like the errors hash): class Person < ActiveRecord::Base #...

Shorthand for instantiation and initialization using an extension method

Is there any shorthand for using an extension method to instantiate and initialise an object? My aim is to abstract-away and encapsulate the code required to instantiate and initialise an instance of MyType suitable for unit testing. Example: //... //convoluted client code - I'd like to avoid the null instance creation MyType t = null...

How does NHibernate 3 solve the QueryOver<> syntax?

I'm curious on how the NHibernate team has solved the QueryOver syntax, so that it works with intellisense and validation at compile time? According to http://nhforge.org/blogs/nhibernate/archive/2009/12/17/queryover-in-nh-3-0.aspx they make use of extension methods and lambda expressions, but I've tried looking through the source but i...

Use Linq and lambda to flatten a list

Hi there I have a class. public class MedicalRequest { private int id private IList<MedicalDays> Days private string MedicalUser ... } and another public class MedicalDays { private int id; private DateTime? day private MedicalRequest request ... } I have the MedicalUser so I'm able to select an I...

ASP.NET MVC: Why is `ToMvcHtmlString` not public?

I'm trying to write my own little HTML helper which acts a lot like DropDownListFor but which doesn't suffer from the same problems that I've encountered before. Let's not discuss whether or not DropDownListFor is flawedthat is not what this question is about. Anyways, what is the reason that the MVC guys make ToMvcHtmlString internal a...

Is there any way to add method to JSON object?

Is there any way to add method to JSON object? ...

How to wrap return type of ajax call in success with function

I have a Web Service and it always brings back one object from my class that i defined on the server side. in success method of $.ajax function i always get the this object. And on the client side i want to add couple of functions to display its properties quickier. Is there a way do that ? (JSON object will return, i am worrying about...

Casting and Linq Cast<T>()

When trying to answer this question, I discovered the following: string s = "test"; var result1 = s.Select(c => (ushort)c); // works fine var result2 = s.Cast<ushort>(); // throws an invalid cast exception Why does Cast<T>() fail here? Whats the difference? ...

Nullable types in strongly-typed datatables/datasets - workarounds?

Strongly-typed DataTables support "nullable" field types, except that the designer will not allow you change the setting to "allow nulls" for any value type fields. (ie: String types allow nullable, but int's do not). The workaround is to call IsMyFieldNull() any time you want to get Myfield. If you access MyField when it does contain ...

UrlHelper extension method not working

I'm trying to add an extension method to my MVC 2 project without success and after several hours of googling and looking here I'm at a loss. I've created a brand new MVC 2 project to make sure there was not anything weird about my existing project and I'm still facing the same problem. I'm sure this is a situation of I "can't see the ...

Get the object instance which contains a specified property

This question will probably take a while to explain, and I'll need to provide background... This is just something I'm playing about with and isn't for production, but at the moment I have some code which looks like this: var myDataModel = new DataModel(); myDataModel.PropertyChanged += myDataModel_PropertyChanged; myDataModel.Change...

Is it possible to define a generic extension method in F#?

I was trying to use Rhino Mocks with F# code, and the following code was a problematic: let service = MockRepository.GenerateMock<IMyService>() service.Stub(s => s.Name).Return("Service"); This was no surprise, since Stub is not part of IMyService interface, it's a C# extension method that Rhino Mocks defines. Slighltly modified code...

How to get the sum of list of shorts using the extension method Sum()?

I was trying to do something like this - List<short> listofshorts= new List<short>(); int s = listofshorts.Sum(); //this does not work...but same code works for a list of ints.. I got this compilation error - 'System.Collections.Generic.List' does not contain a definition for 'Sum' and the best extension method overload 'System.L...

Specifying constructor constraint for Generic Parameter

I have a collection of objects which I pass as parameter to create objects of another type (one for one). I am doing this in many places (basically converting from data objects to business objects). I want to write a generic extension method to accomplish this. But I am stuck because I don't know how I can specify constraint that busines...

Complexity of Java 7's current Lambda proposal? (August 2010)

Some people say that every programming language has its "complexity budget" which it can use to accomplish its purpose. But if the complexity budget is depleted, every minor change becomes increasingly complicated and hard to implement in a backward-compatible way. After reading the current provisional syntax for Lambda (≙ Lambda expres...

Returning a list of items from Sitecore XSLT helper function

I'd like to return a list of items from an XSLT helper function in Sitecore. The items could conceivably be from multiple different places inside the content tree, so a simple XPath expression can't be used. I suspect that I need to do something with the XPathNodeIterator class, however I can't quite figure out how to create one that e...

My extension method isn't registering.

I'm following Pro ASP.Net MVC2 book and literally 90% of everything is new to me. I feel like a kid in a candy store! :) Unit testing, dependancy injection, and other things are really new and very foreign to the typical CRUD applications I create. Now I'm having trouble with a test the book asks us to design. [Test] public vo...