extension-methods

Add method to a class from another project

让人想犯罪 __ 提交于 2019-12-11 04:47:51
问题 Is it possible to add a method to a class from another project? I have a class: namespace ProjectLibrary { public class ClassA { // some methods } } I want to add a method to save my object in a file, but I don't want to add this method in my project ProjectLibrary because I don't want to add reference to System.IO (I want to use this project for android application and PC application). Is there a possibility to add a method SaveToFile() usable only in another project? Or create an abstract

Extension methods for converting IQueryable<T> and IEnumerable<T> to ReadOnlyCollection<T>

和自甴很熟 提交于 2019-12-11 04:29:25
问题 How to write extension methods for converting IQueryable<T> and IEnumerable<T> to ReadOnlyCollection<T> ? Thanks 回答1: public static ReadOnlyCollection<T> AsReadOnlyCollection<T>(this IEnumerable<T> source) { if(source == null) throw new ArgumentNulLException("source"); IList<T> list = source as IList<T> ?? source.ToList(); return new ReadOnlyCollection<T>(list); } Note that there is no such thing as "converting" an IEnumerable<T> in this case (as with all other methods in the LINQ stack), you

Can I extend the operators that LINQ-to-SQL supports?

允我心安 提交于 2019-12-11 03:52:40
问题 If I wanted to badly enough, could I add additional LINQ constructs to LINQ-to-SQL (as I would be able to if creating my own LINQ provider)? For instance, many of the built-in LINQ operators ( XYZ.Any() ) get directly translated to SQL (e.g. IF EXISTS(XYZ) ). If I needed a specialized construct, would I even be able to augment the set, or does this have to be baked into the actual LINQ-to-SQL provider? How would I go about adding a new operator implementation? Are C# extension methods enough

Creating Between Extension Method checking value between two datatypes having same base class

走远了吗. 提交于 2019-12-11 03:46:11
问题 I have a variable whose value is populated at runtime. I want to check whether that value is between two different datatype values (say lowest and highest) or not using an Extension Method. These two values (lowest and highest) can be of same datatypes (No problem). Then its like public static bool Between<T>(this T actual, T lower, T upper) where T : IComparable<T> { return actual.CompareTo(lower) >= 0 && actual.CompareTo(upper) <= 0; } courtesy my earlier asked question How to create a

UrlHelper extension method call encoded not executed

孤街浪徒 提交于 2019-12-11 03:36:46
问题 I created a simple extension method for the ASP.NET MVC UrlHelper. It takes no arguments as its job is to look up the name of a stylesheet file from the configuration and return a url to the stylesheet. The extension method looks roughly like this: public static string SiteStylesheet(this UrlHelper urlHelper) { var scriptFilename = UserInterfaceConfiguration.GetSection() .Mvc.SiteStylesheet; return urlHelper.Content(string.Format("~/Assets/Scripts/{0}", scriptFilename)); } And I use it like

Overloaded extension method is not being called

拟墨画扇 提交于 2019-12-11 03:22:58
问题 If we have a simple class like this one: class MyClass { public void DoSomething(object anObj) { Console.WriteLine("I am an Object: {0}", anObj); } public void DoSomething(string aStr) { Console.WriteLine("I am a String: {0}", aStr); } } And now we do this: var myClass = new MyClass(); myClass.DoSomething("A message"); The output is "I am a String: some text" Ok. That is fine. But if we have a "similar" overload with a extension method, for instance: class MyClass { public void DoSomething

Can I null an object with its own extension method?

末鹿安然 提交于 2019-12-11 02:31:47
问题 I wrote extension method to Timer class to destroy it after certain amount of time. seems like it actually only set false in timer.enable fields but don't really set the whole things to false. Is there a way to nullify an object from it own extension method? and another thing - is it good practice to implement it in that way or I should expect sync issues and more surprises? timer.DestroyAfter(1.Hours()) : public static void DestroyAfter(this Timer timer, TimeSpan timeSpan) { var killingTimer

UrlHelper extension method not working

爷,独闯天下 提交于 2019-12-11 02:05:33
问题 I'm trying to add an extension method to my MVC 2 project without success and after several hours of googling and looking here I'm at a loss. I've created a brand new MVC 2 project to make sure there was not anything weird about my existing project and I'm still facing the same problem. I'm sure this is a situation of I "can't see the forest for the trees" so any help would be greatly appreciated. Here is the code for the extension method. using System.Web.Mvc; namespace ExtensionTest.Helper

How do I use Active Support core extensions?

余生长醉 提交于 2019-12-11 01:54:31
问题 I have Active Support 3.0.3 installed and Rails 3.0.3 with Ruby 1.8.7. When I try to use 1.week.ago I get NoMethodError: undefined method 'week' for 1:Fixnum from (irb):2 The other core extensions seem to work. I tried it on a friend's computer (same install specs and legacy versions are on his) with the same results. What gives? All of this is in IRB. 回答1: Since using Rails should handle this automatically I'm going to assume you're trying to add Active Support to a non-Rails script. Read

Problem creating my own extension to HtmlHelper

我只是一个虾纸丫 提交于 2019-12-10 19:23:42
问题 I have an extension method to HtmlHelper: <%= Html.MyMethod( params )%> It works in visual studio, but throws (at runtime): Compiler Error Message: CS0117: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'MyMethod' The odd bit is that this does work: <%= HtmlHelperExtensions.MyMethod( Html, params ) %> Why does my method not work as an extension, but does as a normal static call? 回答1: I found the answer in the web.config - there's a section that tells it how to compile C#