extension-methods

How to create a Between Extension Method

我的未来我决定 提交于 2019-12-21 17:01:24
问题 I have a variable whose value is populated at runtime. I want to check whether that value is between two same datatype values (say lowest and highest) or not using an Extension Method. I want to check like int a = 2; //here static but is can be changed at runtime if(a.Between(0,8)) DoSomething(); else DoNothing(); If a is 0 or 8 or any value between them, it should return true . If a is (-1 or less) or (9 or greater) then it should return false i want to create an extension method like public

How do I convert IEnumerable to a custom type in C#?

狂风中的少年 提交于 2019-12-21 16:21:46
问题 I am using extension methods OrderBy and ThenBy to sort my custom collection on multiple fields. This sort does not effect the collection but instead returns and IEnumberable. I am unable to cast the IEnumerable result to my custom collection. Is there anyway to change the order of my collection or convert the IEnumerable result to my custom collection? 回答1: If your collection type implements IList<T> (to be able to Add() to it) you could write an extension method: public static Extensions {

C#: Adding extension methods to a base class so that they appear in derived classes

喜欢而已 提交于 2019-12-21 10:12:08
问题 I currently have an extension method on System.Windows.Forms.Control like this: public static void ExampleMethod(this Control ctrl){ /* ... */ } However, this method doesn't appear on classes derived from Control, such as PictureBox. Can I make an extension method that appears not only in Control, but for classes derived from Control, without having to do an explicit cast? 回答1: You must include the using statement for the namespace in which your extensions class is defined or the extension

Delegate as first param to an Extension Method

懵懂的女人 提交于 2019-12-21 09:21:56
问题 Ladies and Gents, I recently tried this experiment: static class TryParseExtensions { public delegate bool TryParseMethod<T>(string s, out T maybeValue); public static T? OrNull<T>(this TryParseMethod<T> tryParser, string s) where T:struct { T result; return tryParser(s, out result) ? (T?)result : null; } } // compiler error "'int.TryParse(string, out int)' is a 'method', which is not valid in the given context" var result = int.TryParse.OrNull("1"); // int.TryParse.OrNull<int>("1"); doesnt

Delegate as first param to an Extension Method

我与影子孤独终老i 提交于 2019-12-21 09:20:14
问题 Ladies and Gents, I recently tried this experiment: static class TryParseExtensions { public delegate bool TryParseMethod<T>(string s, out T maybeValue); public static T? OrNull<T>(this TryParseMethod<T> tryParser, string s) where T:struct { T result; return tryParser(s, out result) ? (T?)result : null; } } // compiler error "'int.TryParse(string, out int)' is a 'method', which is not valid in the given context" var result = int.TryParse.OrNull("1"); // int.TryParse.OrNull<int>("1"); doesnt

Can you teach Entity Framework to recognize an expression?

Deadly 提交于 2019-12-21 06:25:33
问题 I have a search function that uses the Entity Framework. One of the things you can search by is a date range. You might say something like "where Start Date is between SearchStart and Search End". It isn't that difficult to write in linq syntax, but it can get pretty verbose when you have many different date parameters to search by. I have an extension method on DateTime that basically checks of the date is contained between a StartDate and an EndDate. I use this in other places where EF isn

With the advent of extension methods, are abstract classes less attractive?

这一生的挚爱 提交于 2019-12-21 04:18:16
问题 One interesting aspect of extension methods in .NET is the fact that you can apply them to interfaces. For me, it seems nice that I can define functionality near the interface without defining an abstract class that clutters the assembly. I know that abstract classes are not obsolete or anything, but how do you feel about utilizing this side effect in your code? Example: public static class IUserExtensions { public static bool IsCurrentUser(this IUser user) { return (HttpContext.Current.User

Why List(Of T) doesn't have Count()?

一曲冷凌霜 提交于 2019-12-21 04:14:53
问题 Public Class MyList Inherits List(Of MyObject) Public ReadOnly Property SelectedCount() As Integer Get Return Me.Count(Function(obj) obj.IsSelected) End Get End Property End Class The above code causes a compile-time error. As you can see, I'm trying to use extension method Count(<predicate>) . I guess the error is because there is a similarly-named property Count in List class itself too, hiding the extension member. My questions here are: Do I need to explicitly cast my class to something

How to call an extension method from own class without casting?

末鹿安然 提交于 2019-12-21 04:12:39
问题 I'm trying to call an extension method on my own class, but it fails to compile. Consider the following lines of code: public interface IHelloWorld { } public static class Extensions { public static string HelloWorld(this IHelloWorld ext) { return "Hello world!"; } } public class Test : IHelloWorld { public string SaySomething() { return HelloWorld(); } } Basically I'm extending on the interface. I keep getting this error: The name 'HelloWorld' does not exist in the current context Can

ActiveRecord - replace model validation error with warning

流过昼夜 提交于 2019-12-20 10:47:09
问题 I want to be able to replace a field error with a warning when saving/updating a model in rails. Basically I want to just write a wrapper around the validation methods that'll generate the error, save the model and perhaps be available in a warnings hash (which works just like the errors hash): class Person < ActiveRecord::Base # normal validation validates_presence_of :name # validation with warning validates_numericality_of :age, :only_integer => true, :warning => true # <-- only warn end >