extension-methods

ASP.NET repeater alternate row highlighting without full blown <alternatingitemtemplate/>

*爱你&永不变心* 提交于 2019-11-28 16:48:31
I'm trying to accomplish simply adding a css class to a div on alternate rows in my <itemtemplate/> without going to the overhead of including a full blown <alternatingitemtemplate/> which will force me to keep a lot of markup in sync in the future. I've seen a solution such as http://blog.net-tutorials.com/2009/04/02/how-to-alternate-row-color-with-the-aspnet-repeater-control/ which I'm tempted to use but this still doesn't "smell" right to me. Has anyone else got a more maintainable and straightforward solution? Ideally I'd like to be able to do something like: <asp:repeater id=

Extension methods versus inheritance

天涯浪子 提交于 2019-11-28 16:40:16
Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times? Thanks! Extension methods are useful, but they are harder to discover through the IDE than regular methods, since they are not attached to the original class and there are no clues as to where the code for them might reside. There are some best practice suggestions as to where to put them and how to name them, but these are only guidelines and there is no guarantee that someone will follow them. Usually you would use extension methods if you are only adding functionality to a

Generic Map/Reduce List Extensions in C#

安稳与你 提交于 2019-11-28 16:01:33
问题 I am writing a few extensions to mimic the map and reduce functions in Lisp. public delegate R ReduceFunction<T,R>(T t, R previous); public delegate void TransformFunction<T>(T t, params object[] args); public static R Reduce<T,R>(this List<T> list, ReduceFunction<T,R> r, R initial) { var aggregate = initial; foreach(var t in list) aggregate = r(t,aggregate); return aggregate; } public static void Transform<T>(this List<T> list, TransformFunction<T> f, params object [] args) { foreach(var t

Is calling an extension method on a “null” reference (i.e. event with no subscribers) evil?

点点圈 提交于 2019-11-28 15:52:28
Evil or not evil? public static void Raise(this EventHandler handler, object sender, EventArgs args) { if (handler != null) { handler(sender, args); } } // Usage: MyButtonClicked.Raise(this, EventArgs.Empty); // This works too! Evil? EventHandler handler = null; handler.Raise(this, EVentArgs.Empty); Note that due to the nature of extension methods, MyButtonClicked.Raise will not throw a NullReferenceException if MyButtonClicked is null. (E.g. there are no listeners to MyButtonClicked event). Evil or not? Not evil. I wish events worked this way by default. Can someone explain why an event with

How can I write an extension method that converts a System.Drawing.Bitmap to a byte array?

依然范特西╮ 提交于 2019-11-28 14:14:16
How can I write an extension method that converts a System.Drawing.Bitmap to a byte array? Why not: <Extension()> _ Public Function ToByteArray(ByVal image As System.Drawing.Bitmap) As Byte() Using ms = New MemoryStream() image.Save(ms, image.RawFormat) Return ms.ToArray() End Using End Function Yet when I use that, I get "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+" thrown from the Save() operation. What am I doing wrong? As someone else state, this is a known GDI+ bug. However, it usually appear when you've closed the source stream of the image before

Extension method to get property name

两盒软妹~` 提交于 2019-11-28 13:56:49
I have an extension method to get property name as public static string Name<T>(this Expression<Func<T>> expression) { MemberExpression body = (MemberExpression)expression.Body; return body.Member.Name; } I am calling it as string Name = ((Expression<Func<DateTime>>)(() => this.PublishDateTime)).Name(); This is working fine and returns me PublishDateTime as string. However I have an issue with the calling statement, it is looking too complex and I want some thing like this. this.PublishDateTime.Name() Can some one modify my extension method? Try this: public static string Name<T,TProp>(this T

Error: Extension methods must be defined in a top level static class (CS1109)

左心房为你撑大大i 提交于 2019-11-28 13:43:42
I'm trying to make a countdown program, which I can start and stop and set the value of the countdown to 10 minutes if needed. But I'm getting an error I don't quite understand. I'm not that into C#, so here's the code: Can some one help me a bit here ? Think I run on framework 3.0 or something ? using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Timers; namespace PauseMaster { public partial class MainForm : Form { public MainForm() {

How do I call extension methods from outside the class they are defined in?

感情迁移 提交于 2019-11-28 13:32:14
Here is a minimal example that demonstrates the problem: abstract class Base { abstract fun String.extension(x: Char) } class Derived : Base() { override fun String.extension(x: Char) { // Calling lots of methods on String, hence extension method println("${first()} $length ${last()} ${firstOrNull { it == x }} ...") } } Calling the extension method from Java is trivial: Base o = new Derived(); o.extension("hello world", 'l'); But I can't figure out how to do it in pure Kotlin. Neither String nor Base seems to have the extension method. hotkey First, note that an extension function defined as a

EF Code First Delete Batch From IQueryable<T>?

[亡魂溺海] 提交于 2019-11-28 12:26:21
I know this is possible in LINQ-to-SQL, and I've seen bits and pieces that lead me to believe it's possible in EF. Is there an extension out there that can do something like this: var peopleQuery = Context.People.Where(p => p.Name == "Jim"); peopleQuery.DeleteBatch(); Where DeleteBatch just picks apart the peopleQuery and creates a single SQL statement to delete all the appropriate records, then executes the query directly instead of marking all those entities for deletion and having it do them one by one. I thought I found something like that in the code below, but it fails immediately

C++ Class Extension

独自空忆成欢 提交于 2019-11-28 12:13:08
Is there a way to add new methods to a class, without modifying original class definition (i.e. compiled .lib containing class and corresponding .h file) like C#'s class extension methods? No. C++ has no such capability. As mentioned in other answers, the common workarounds are: Define a derived class, perhaps with a factory to hide the actual implementation class Define a decorator class Define non-member functions that operate on instances of the class No, you can't do this in C++. If you want to achieve something like this you have 2 options, You could inherit from the class (if this is an