extension-methods

C#: Best practice for validating “this” argument in extension methods

最后都变了- 提交于 2019-11-30 04:55:26
问题 Let's say I have an extension method public static T TakeRandom<T>(this IEnumerable<T> e) { ... To validate the argument e, should I: A) if (e == null) throw new NullReferenceException() B) if (e == null) throw new ArgumentNullException("e") C) Not check e What's the consensus? My first thought is to always validate arguments, so thrown an ArgumentNullException. Then again, since TakeRandom() becomes a method of e, perhaps it should be a NullReferenceException. But if it's a

What is difference between extension method and static method?

我们两清 提交于 2019-11-30 04:55:07
问题 What is the difference between an extension method and a static method ? I have two classes like this : public static class AClass { public static int AMethod(string ....) { } } and public static class BClass { public static int BMethod(this string ....) { } } I can use these like AClass.AMethod('...'); or '...'.BMethod(); Which is proposed ? 回答1: An extension method is still a static method. You can use it exactly as you'd use a normal static method. The only difference is that an extension

How does VB.NET compiler choose which extension overload to run?

限于喜欢 提交于 2019-11-30 04:19:02
问题 Got an interesting oddity - thought someone might be able to help. This came out of some fun with nullable types from this question: How to check if an object is nullable? Option Strict On Module Test ' Call this overload 1 <Extension()> Function IsNullable(obj As ValueType) As Boolean Return False End Function ' Call this overload 2 <Extension()> Function IsNullable(Of T As {Structure})(obj As Nullable(Of T)) As Boolean Return True End Function Sub Test() ' a is an integer! Dim a As Integer

IQueryable (non generic) : missing Count and Skip ? it works with IQueryable<T>

别来无恙 提交于 2019-11-30 04:17:33
问题 i have an extension method which a person was really helpful to give me... it does an orderby on IQueryable ... but i wanted one to do a normal IQueryable (non generic) Here is the code, The count and Skip and i think Take are missing . public static IQueryable GetPage(this IQueryable query, int page, int pageSize, out int count) { int skip = (int)((page - 1) * pageSize); count = query.Count(); //COUNT DOESN'T EXIST return query.Skip(skip).Take((int)pageSize); // NEITHER SKIP } Here is the

Disadvantages of extension methods?

自作多情 提交于 2019-11-30 02:55:52
Extension method is a really helpful feature that you can add a lot of functions you want in any class. But I am wondering if there is any disadvantage that might bring troubles to me. Any comments or suggestions? The way that extension methods are imported (i.e. a whole namespace at a time) isn't granular. You can't import one extension from a namespace without getting all the rest. It's not immediately obvious from the source code where the method is defined. This is also an advantage - it means you can make your code look consistent with the rest of the methods on the type, even if you can

How to Mock (with Moq) Unity methods

大憨熊 提交于 2019-11-30 01:38:51
问题 Extension methods are not good for testing (that's described here: Mocking Extension Methods with Moq, http://www.clariusconsulting.net/blogs/kzu/archive/2009/12/22/Howtomockextensionmethods.aspx). But probably there are some solutions for mocking of Unity methods? In my case I have the following function: public class MyManager { public MyManager(IUnityContainer container) : base(container) { } public IResult DoJob(IData data) { IMyLog log = MyContainer.Resolve<IMyLog>(); ... use log.Id ...

Is it possible define an extension operator method?

随声附和 提交于 2019-11-30 01:28:30
问题 is it possible to define an extension method that at the same time is an operator? I want for a fixed class add the possibility to use a known operator that actually can't be applied. For this particular case i want to do this: somestring++; //i really know that this string contains a numeric value And i don't want to spread types conversions for all the code. I know that i could create wrapper class over an string and define that operator but i want to know if this kind of thing is possible

Which mechanism is a better way to extend Dictionary to deal with missing keys and why?

我的梦境 提交于 2019-11-30 00:50:04
问题 There is a minor annoyance I find myself with a lot - I have a Dictionary<TKey, TValue> that contains values that may or may not be there. So normal behaviour would be to use the indexer, like this: object result = myDictionary["key"]; However, if "key" is not in the dictionary this throws a KeyNotFoundException , so you do this instead: object val; if (!myDictionary.TryGetValue("key", out val)) { val = ifNotFound; } Which is fine, except that I can have a load of these in a row - TryGetValue

Generic Map/Reduce List Extensions in C#

荒凉一梦 提交于 2019-11-29 20:11:24
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 in list) f(t,args); } The transform function will cut down on cruft like: foreach(var t in list) if

What is the easiest way to get the property value from a passed lambda expression in an extension method for HtmlHelper?

喜你入骨 提交于 2019-11-29 19:51:28
I am writing a dirty little extension method for HtmlHelper so that I can say something like HtmlHelper.WysiwygFor(lambda) and display the CKEditor. I have this working currently but it seems a bit more cumbersome than I would prefer. I am hoping that there is a more straight forward way of doing this. Here is what I have so far. public static MvcHtmlString WysiwygFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) { return MvcHtmlString.Create(string.Concat("<textarea class=\"ckeditor\" cols=\"80\" id=\"", expression.MemberName(), "\" name=\