extension-methods

Extension methods in referenced assemblies?

别来无恙 提交于 2019-11-30 12:56:21
If I try to call my extension method which is defined like this: Module LinqExtensions <System.Runtime.CompilerServices.Extension()> _ Public Function ToSortableBindingList(Of TSource)(ByVal source As IEnumerable(Of TSource)) As IBindingList If (source Is Nothing) Then Throw New ArgumentNullException("source") End If Return New SortableBindingList(Of TSource)(New List(Of TSource)(source)) End Function End Module by calling Dim sss As String() sss.AsEnumerable.ToSortableBindingList() it gives an error "ToSortableBindingList is not a member of System.Collections.Generic.IEnumerable(Of String)".

Disadvantages of extension methods?

不问归期 提交于 2019-11-30 12:42:59
问题 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? 回答1: 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

Where is the ToList() method? (IQueryable)

六眼飞鱼酱① 提交于 2019-11-30 11:48:01
If I try this, it will work: var query = myContextObject.Users.Where(u=>u.Name == "John"); query.ToList(); I'm able to call ToList and a lot of other extension methods. But if I try this: public List ConvertQueryToList(IQueryable query) { return query.ToList(); } ToList won't be accessible, I'm guessing this is because ToList is an extension method, but then how is that ToList is attached in the first example? Is it possible to access ToList in the second case? You need to write it as: public List<T> ConvertQueryToList<T>(IQueryable<T> query) { return query.ToList(); } This will cause the

Using Attributes for Generic Constraints [duplicate]

我与影子孤独终老i 提交于 2019-11-30 11:23:40
This question already has an answer here: Can you use “where” to require an attribute in c#? 5 answers Given an example such as .. public interface IInterface { } public static void Insert<T>(this IList<T> list, IList<T> items) where T : IInterface { // ... logic } This works fine, but I was wondering if it is possible to use an Attribute as a constraint. Such as ... class InsertableAttribute : Attribute public static void Insert<T>(this IList<T> list, IList<T> items) where T : [Insertable] { // ... logic } Obviously this syntax doesn't work, or I wouldn't be posting the question. But I'm just

What's the Best Way to Add One Item to an IEnumerable<T>?

六眼飞鱼酱① 提交于 2019-11-30 10:54:50
Here's how I would add one item to an IEnumerable object: //Some IEnumerable<T> object IEnumerable<string> arr = new string[] { "ABC", "DEF", "GHI" }; //Add one item arr = arr.Concat(new string[] { "JKL" }); This is awkward. I don't see a method called something like ConcatSingle() however. Is there a cleaner way to add a single item to an IEnumerable object? Nope, that's about as concise as you'll get using built-in language/framework features. You could always create an extension method if you prefer: arr = arr.Append("JKL"); // or arr = arr.Append("123", "456"); // or arr = arr.Append("MNO"

Where do I put my extension method?

柔情痞子 提交于 2019-11-30 10:52:17
A senior member here gave me this code: public static string Truncate(this string value, int maxChars) { return value.Length <= maxChars ? value : value.Substring(0, maxChars) + " .."; } He said to use it as an extension method. But where do I put this method? It looks like it adds something to .Net Consider a class named StringExtensions like so: static class StringExtensions { public static string Truncate(this string value, int maxChars) { return value.Length <= maxChars ? value : value.Substring(0, maxChars) + " .."; } } Be sure that whatever namespace you put this class in, you include a

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

故事扮演 提交于 2019-11-30 10:20:08
问题 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

The type or namespace name 'Window' does not exist in the namespace 'System.Windows'

可紊 提交于 2019-11-30 09:34:17
问题 I am trying to write an extension method for the WPF Window class. I am doing it in a class library project in my solution, so that I can use it in all my projects in the solution. Here is my code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace ExtensionMethods { public static class MyExtensions { public static void ResizeToPrimaryScreenWorkingArea(this System.Windows.Window w) { } } } When I

rolling my own @Html.BeginfBrm()

扶醉桌前 提交于 2019-11-30 09:34:01
I am writing a custom validation set that will display all missing elements on a div. I'd like to be able to use a custom @Html.BeginForm() method that will write out that div but I'm really not sure where to even begin as this nut is a little tougher to crack than just a html extension that writes out a Tag or String (the form encapsulates data/controls and is closed by } at the end). I looked at the metadata version of the built in BeginForm() method and it wasn't much help to me. Essentially, I just want to extend that method if possible and have it write out a MvcHtmlString of a div that

C#, Linq2SQL: Creating a predicate to find elements within a number of ranges

谁说胖子不能爱 提交于 2019-11-30 09:16:28
Lets say I have something called Stuff in my database, with a property called Id. From the user I get a sequence of selected Range objects (or rather I create them from their input) with the Ids they want. A stripped down version of that struct looks like this: public struct Range<T> : IEquatable<Range<T>>, IEqualityComparer<Range<T>> { public T A; public T B; public Range(T a, T b) { A = a; B = b; } ... } So one could for example have gotten: var selectedRange = new List<Range<int>> { new Range(1, 4), new Range(7,11), }; I then want to use that to create a predicate to select only things