extension-methods

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

天大地大妈咪最大 提交于 2019-12-18 13:24:38
问题 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

Best method to use IDataReader as IEnumerable<T>?

佐手、 提交于 2019-12-18 12:39:03
问题 I need to use Linq on any IDataReader implementations like this var c = sqlDataReader.AsEnumerable().Count(); Example: public abstract class Test { public abstract SqlDataReader GetSqlDataReader(); public void Foo() { SqlDataReader sqlDataReader = GetSqlDataReader(); IEnumerable<SqlDataReader> sqlEnumerable = sqlDataReader.AsEnumerable(); var c = sqlEnumerable.Count(); var s = sqlEnumerable.Sum(); SqlDataReader first = sqlEnumerable.First(); var t = first.GetSqlXml(10); } } What is the best

Create Extension Method to Produce Open & Closing Tags like Html.BeginForm()

戏子无情 提交于 2019-12-18 12:29:11
问题 I wonder if it's possible to create an extension method which has functionality & behaviour similar to Html.BeginForm(), in that it would generate a complete Html tag, and I could specificy its contents inside <% { & } %> tags. For example, I could have a view like: <% using(Html.BeginDiv("divId")) %> <% { %> <!-- Form content goes here --> <% } %> This capability would be very useful in the context of the functionality I'm trying to produce with the example in this question This would give

IEnumerable Extension Methods on an Enum

时光毁灭记忆、已成空白 提交于 2019-12-18 11:43:18
问题 I have an enum(below) that I want to be able to use a LINQ extension method on. enum Suit{ Hearts = 0, Diamonds = 1, Clubs = 2, Spades = 3 } Enum.GetValues(...) is of return type System.Array, but I can't seem to get access to a ToList() extension or anything else of that sort. I'm just looking to write something like... foreach(Suit s in Enum.GetValues(typeof(Suit)).Select(x=>x).Where(x=> x != param)){} Is there something I'm missing, or can someone explain to me why this isn't possible?

How to call extension method “ElementAt”of List<T> with reflection?

删除回忆录丶 提交于 2019-12-18 05:57:13
问题 I have problem that after creating object "oListType01" of type List < MyClass01 > and after assigning it to the another objet "oObjectType " of type "object" I can not access any more function "ElementAt(1)". I tried by using reflection but I am always getting exception(parameter conflict) in "Invoke" method. Does anyone knows why ? Milan MyClass01 oMy1 = new MyClass01(); oMy1._ID = "1"; MyClass01 oMy2 = new MyClass01(); oMy2._ID = "3"; IList<MyClass01> oListType01 = new List<MyClass01>();

Extending Enums, Overkill?

拟墨画扇 提交于 2019-12-18 05:10:54
问题 I have an object that needs to be serialized to an EDI format. For this example we'll say it's a car. A car might not be the best example b/c options change over time, but for the real object the Enums will never change. I have many Enums like the following with custom attributes applied. public enum RoofStyle { [DisplayText("Glass Top")] [StringValue("GTR")] Glass, [DisplayText("Convertible Soft Top")] [StringValue("CST")] ConvertibleSoft, [DisplayText("Hard Top")] [StringValue("HT ")]

Extension interface patterns

孤者浪人 提交于 2019-12-17 22:58:37
问题 The new extensions in .Net 3.5 allow functionality to be split out from interfaces. For instance in .Net 2.0 public interface IHaveChildren { string ParentType { get; } int ParentId { get; } List<IChild> GetChildren() } Can (in 3.5) become: public interface IHaveChildren { string ParentType { get; } int ParentId { get; } } public static class HaveChildrenExtension { public static List<IChild> GetChildren( this IHaveChildren ) { //logic to get children by parent type and id //shared for all

Extension method to get property name

喜夏-厌秋 提交于 2019-12-17 20:51:20
问题 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

Escape Catch-22 with extension attributes in .NET 2.0

好久不见. 提交于 2019-12-17 19:38:54
问题 How can a single .NET assembly, targeting 2.0, 3.0, 3.5, 4.0, and 4.5 concurrently, support extension methods for both C# and VB.NET consumers? The standard suggestion is to add this: namespace System.Runtime.CompilerServices { public sealed class ExtensionAttribute : Attribute { } } This the approach suggested by more than one Microsoft employee and was even featured in MSDN magazine. It's widely hailed by many bloggers as having 'no ill effects'. Oh, except it will cause a compiler error

Evil use of Maybe monad and extension methods in C#?

北慕城南 提交于 2019-12-17 17:29:45
问题 edit 2015 This question and its answers are no longer relevant. It was asked before the advent of C# 6, which has the null propagating opertor (?.), which obviates the hacky-workarounds discussed in this question and subsequent answers. As of 2015, in C# you should now use Form.ActiveForm?.ActiveControl?.Name. I've been thinking about the null propagation problem in .NET, which often leads to ugly, repeated code like this: Attempt #1 usual code: string activeControlName = null; var activeForm