extension-methods

Extension method, SumIf on generic List<T>

社会主义新天地 提交于 2019-12-22 11:37:35
问题 I need to write an generic extension method for List(T) that conditionally considers each string property of T, then sums a corresponding decimal property of T if a condition is met. My effort thus far: // foreach(p in Persons) { if(p.Name == "mort"){sum p.Amount;} } public static double SumIf<T>(this T o, List<T> ListItems, string targetStr, ?strVals?, ?dblVals?) { double sum = 0; foreach(T item in ListItems) { if(item.?strVal? == targetStr){ sum += item.?dblVal? ; } } return sum; } Thanks

Please explain System.Linq.Enumerable.Where(Func<T, int, bool> predicate)

浪子不回头ぞ 提交于 2019-12-22 08:33:54
问题 I can't make any sense of the MSDN documentation for this overload of the Where method that accepts a predicate that has two arguments where the int, supposedly, represents the index of the source element, whatever that means (I thought an enumerable was a sequence and you couldn't see further than the next item, much less do any indexing on it). Can someone please explain how to use this overload and specifically what that int in the Func is for and how it is used? 回答1: The int parameter

Use system namespaces for class libraries: good or bad

时光毁灭记忆、已成空白 提交于 2019-12-22 08:30:13
问题 Is it a good idea to use "system namespaces" in my class libraries? Sample: namespace System.Web { public static class RequestExtensions { public static bool IsPost(this HttpRequest r) { return string.Compare(r.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase) == 0; } } } The advantage: no need to include additional uses-clauses (especially for extension methods), so all becomes available straight after adding reference to the library. The best sample is NUnitEx project (which uses

Extension method call does not compile, but static method call to same code does compile

爱⌒轻易说出口 提交于 2019-12-22 07:52:38
问题 There is library A calling library B using a C# extension method. I got a weird error from the C# compiler: The type 'System.Windows.Forms.Control' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows.Forms, Version=4.0.0.0 None of the libraries A or B are dependent on System.Windows.Forms.Control , nor has any dependency of A a dependency on System.Windows.Forms.Control . System.Windows.Forms.Control is only referenced from another project in

HtmlAttributes in Extension Method

梦想的初衷 提交于 2019-12-22 07:12:40
问题 I'm using MVC 5 and I'm trying to write some Bootstrap extention methods. My goal is to 'overwrite' the Html.ActionLink method with Html.BootstrapLinkButton . The BootstrapLinkButton method should generate a link with the css classes "btn btn-default" automatically attached. My code so far: public static MvcHtmlString BootstrapLinkButton(this HtmlHelper htmlHelper, string linkText,string actionName, string controllerName, object routeValues = null, object htmlAttributes = null) { var

Overriding LINQ extension methods

落花浮王杯 提交于 2019-12-22 06:46:32
问题 Is there a way to override extension methods (provide a better implementation), without explicitly having to cast to them? I'm implementing a data type that is able to handle certain operations more efficiently than the default extension methods, but I'd like to keep the generality of IEnumerable. That way any IEnumerable can be passed, but when my class is passed in, it should be more efficient. As a toy example, consider the following: // Compile: dmcs -out:test.exe test.cs using System;

How can I return <TEnumerable, T> : where TEnumerable:IEnumerable<T>

自作多情 提交于 2019-12-22 05:34:16
问题 Goal: Generic enumerated type to be the same type when returned. Note: This works when the types are entered but I don't understand why they can't be inferred. List<T> then return List<T> IOrderedEnumerable<T> then return IOrderedEnumerable<T> ETC Current method (works only if all types are entered) public static TEnumerable WithEach<TEnumerable, T>(this TEnumerable items, Action<T> action) where TEnumerable : IEnumerable<T> { foreach (var item in items) action.Invoke(item); return items; }

Deserialize Stream to List<T> or any other type

蹲街弑〆低调 提交于 2019-12-22 04:01:19
问题 Attempting to deserialize a stream to List<T> (or any other type) and am failing with the error: The type arguments for method Foo.Deserialize<T>(System.IO.Stream) cannot be inferred from the usage. Try specifying the type arguments explicitly. This fails: public static T Deserialize<T>(this Stream stream) { BinaryFormatter bin = new BinaryFormatter(); return (T)bin.Deserialize(stream); } But this works: public static List<MyClass.MyStruct> Deserialize(this Stream stream) { BinaryFormatter

How to check if one C++ class extends another (like if that another one was an interface)?

泪湿孤枕 提交于 2019-12-21 21:28:24
问题 So generally having class A { ... }; class B { ... }; class C: public A, public B {}; // C inherits from A and B. when we create an instance of C and want to pass it into some function ho do we check if class we pass to a function is extending A? 回答1: C is defined as inheriting from A so there is no need to check: It is mandatory that an instance of C is also a A (and a B ). However, if you have a function taking a A as a parameter, you can use dynamic_cast<> to check if the instance is

Translate a List<TypeA> to List<TypeB>

这一生的挚爱 提交于 2019-12-21 21:25:05
问题 Understood the concept of translate. Used it in converting a DataModel Type to DTO type for presentation layer like this and worked fine. objTypeB = objTypeA.TranslateTo<clsTypeB>(); Discrepancy between TypeA and TypeB was just the datatype of few properties and I converted them in the Property Set method. But in the above implementation if the source is List<TypeA> , I have loop through each to translate to TypeB and add it another List<TypeB> instance. Is it possible to do something like