extension-methods

How to write an extension method for DataTable.Rows[i][j]? [closed]

余生长醉 提交于 2019-12-08 12:56:32
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 6 years ago . I would like to create an extension method to be implemented like below (if it is even possible) which would be similar to the .ToString() extension method. Can someone please point me in the right direction. I attempted to search it in Google and cannot locate anything. DataTable table = db

How to get rid of event firing boiler plate code?

喜欢而已 提交于 2019-12-08 12:39:43
问题 I am developing an application which is event driven and runs mulit-threaded and so I have a lot of events getting fired and to fire them "in a save way" I do this the following way: public static void OnEVENT(EventArgs args) { var theEvent = EVENT; if (theEvent != null) theEvent(this, args); } This pattern is repeated multiple times all over my application. Is there a way to get rid of the repetion of this pattern and implent this in a generic way? 回答1: I created a Helper class containing

IronRuby calling C# Extension Methods - Error - Compatibility in .NET 3.5

…衆ロ難τιáo~ 提交于 2019-12-08 10:08:55
问题 I have written an Extension Method off of DataGridView called HideColumns. public static class Extensions { public static void HideColumns(this DataGridView dataGridView, params string[] columnNames) { foreach (string str in columnNames) { if (dataGridView.Columns[str] != null) { dataGridView.Columns[str].Visible = false; } } } } I pass my grid into an IronRuby script as a variable called main_grid When my script calls main_grid.HideColumns("FirstName","LastName") the script blows up with

PHP file extension issue [duplicate]

一曲冷凌霜 提交于 2019-12-08 09:43:19
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to extract a file extension in PHP? How can we get file extension with any php function? Whether there is some built-in function in php. I am using some explode function but any other exists 回答1: Yes, you can ....... Just Use the following code....... Pass path of file to this PHP function and get extension. function extension($path) { $qpos = strpos($path, "?"); if ($qpos!==false) $path = substr($path, 0,

Select the maximal item from collection, by some criterion

假装没事ソ 提交于 2019-12-08 03:08:08
问题 i am new to .net 3.5. I have a collection of items: IList<Model> models; where class Model { public string Name { get; private set; } } I would like to get the element, which has the longest name's length. I tried string maxItem = models.Max<Model>(model => model.Name.Length); but it of course returns the maximum length (and I need a Model object). I know there is a way of doing this using the extension methods but I don't know how. 回答1: This is how I got it to work. Maybe there's a better

Expression of type 'MyEnum' cannot be used for parameter of type

我怕爱的太早我们不能终老 提交于 2019-12-07 22:07:24
问题 I created Enum ToFrendlyString function for my enums, but i cant use in Linq. public enum MyEnum { Queued = 0, [Description("In progress")] In_progress = 2, [Description("No answer")] No_answer = 6, } public static class EnumToFrendlyString { public static string ToFrendlyString(this Enum value) { return value.GetEnumDescription(); } public static string GetEnumDescription(this Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); var attributes = (DescriptionAttribute[])fi

GroupBy and Select extension method assistance

人走茶凉 提交于 2019-12-07 19:46:27
问题 I am trying to GroupBy a few fields using the following code: var cars = tmp.Select(a => new { a.Make, a.Model, a.Year }); cars = cars.Distinct() .OrderBy(a => a.Make) .ThenBy(a => a.Model); var groupedCars = cars.GroupBy(a => new { a.Make, a.Model }) .Select(b => new { b.Make, b.Model, b.Year }); Unfortunately, the very last line of code is giving me errors as is does not recognize the three fields. 'System.Linq.IGrouping(AnonymousType#1,AnonymousType#2)' does not contain a definition for

ArgumentNullException or NullReferenceException from extension method?

六眼飞鱼酱① 提交于 2019-12-07 18:06:38
问题 What would you consider to be the best exception type to throw when an extension method is called on a null instance (where the extension method does not allow it)? Since extension methods are nothing but static methods you could argue that it should be ArgumentNullException, but on the other hand they're used like instance methods so it might be more natural to use the NullReferenceException. Let's take the following example: public static string ToInvariantString(this IFormattable value,

How to write correct static methods - multithread safe

 ̄綄美尐妖づ 提交于 2019-12-07 17:29:52
问题 As I assume static methods shouldn't be writen like the first snippet , or am I wrong ? public static class ExtensionClass { private static SomeClass object1; private static StringBuilder sb; private static string DoSomething() { sb.AppendLine(object1.SomeValue); } public static string ExtensionMethod(this HtmlHelper helper, SomeClass _object1) { object1 = _object1; sb = new StringBuilder(); DoSomething(); return sb.ToString(); } } So I come up with this: public static class ExtensionClass {

Problem with Extension Methods and Generic Constraints

这一生的挚爱 提交于 2019-12-07 16:14:19
问题 I have a base interface and several inherited interfaces. There are extension methods for the base interface that modify the object and return a new instance of the base class ( IChildA.Touch() => IBase , IBase.Touch() => IBase ). For one inheritance path ( IChildB and descendants) i want to implement extension methods that return an object of the same type as the calling object ( IGrandChildB.Touch() => IGrandChild ). For this i would like to specify a single generic extension method that is