extension-methods

How can I write an extension method that converts a System.Drawing.Bitmap to a byte array?

眉间皱痕 提交于 2019-11-27 08:21:26
问题 How can I write an extension method that converts a System.Drawing.Bitmap to a byte array? Why not: <Extension()> _ Public Function ToByteArray(ByVal image As System.Drawing.Bitmap) As Byte() Using ms = New MemoryStream() image.Save(ms, image.RawFormat) Return ms.ToArray() End Using End Function Yet when I use that, I get "System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+" thrown from the Save() operation. What am I doing wrong? 回答1: As someone else state,

Enumeration extension methods

只愿长相守 提交于 2019-11-27 07:32:14
In vs2008, is it possible to write an extension methods which would apply to any enumeration. I know you can write extension methods against a specific enumeration, but I want to be able to every enumeration using a single extension method. Is this possible? Yes, just code against the base Enum type, e.g. public static void Something(this Enum e) { // code here } The down-side is you'll probably end up doing some quite nasty stuff like finding the real base type using Enum.GetUnderlyingType , casting, and going down different branches depending on what the base type of the enum is, but you can

C# Extension Method for String Data Type

半城伤御伤魂 提交于 2019-11-27 07:24:14
问题 My web application deals with strings that need to be converted to numbers a lot - users often put commas, units (like cm, m, g, kg) and currency symbols in these fields so what I want to do is create a string extension method that cleans the field up and converts it to a decimal. For example: decimal myNumber = "15 cm".ToDecimal(); 回答1: Are you expecting users of different 'cultures' to use your application? If so it's better to factor in the user's regional settings: static decimal

EF Code First Delete Batch From IQueryable<T>?

折月煮酒 提交于 2019-11-27 07:02:39
问题 I know this is possible in LINQ-to-SQL, and I've seen bits and pieces that lead me to believe it's possible in EF. Is there an extension out there that can do something like this: var peopleQuery = Context.People.Where(p => p.Name == "Jim"); peopleQuery.DeleteBatch(); Where DeleteBatch just picks apart the peopleQuery and creates a single SQL statement to delete all the appropriate records, then executes the query directly instead of marking all those entities for deletion and having it do

C++ Class Extension

丶灬走出姿态 提交于 2019-11-27 06:48:35
问题 Is there a way to add new methods to a class, without modifying original class definition (i.e. compiled .lib containing class and corresponding .h file) like C#'s class extension methods? 回答1: No. C++ has no such capability. As mentioned in other answers, the common workarounds are: Define a derived class, perhaps with a factory to hide the actual implementation class Define a decorator class Define non-member functions that operate on instances of the class 回答2: No, you can't do this in C++

Extension methods on a struct

扶醉桌前 提交于 2019-11-27 06:33:49
问题 Can you add extension methods to a struct? 回答1: Yes, you can add extension methods on structs. As per the definition of extension method, you can easily achieve it. Below is example of extension method on int namespace ExtensionMethods { public static class IntExtensions { public static bool IsGreaterEqualThan(this int i, int value) { return i >= value; } } } 回答2: It is possible to add extension methods to structures, but there is an important caveat. Normal struct methods methods accept this

How do I use Active Support core extensions?

跟風遠走 提交于 2019-11-27 06:28:26
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. 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 " How to Load Core Extensions ". Active Support's methods got broken into smaller groups in Rails 3, so we don

Why doesn't VS 2008 display extension methods in Intellisense for String class

偶尔善良 提交于 2019-11-27 05:25:13
Since String implements IEnumerable<char> , I was expecting to see the Enumerable extension methods in Intellisense, for example, when typing the period in String s = "asdf"; s. I was expecting to see .Select<char>(...) , .ToList<char>() , etc. I was then suprised to see that the extension methods do in fact work on the string class, they just don't show up in Intellisense. Does anyone know why this is? This may be related to this question. It's by explicit design. The problem is that while String most definitely implements IEnumerable<T> , most people don't think of it, or more importantly

Warm-up when calling methods in C#

随声附和 提交于 2019-11-27 05:18:15
I just came across this post that talks about time measuring. I remember (I hope I'm not misremembering) it's an unfair competition, if this method is never called before. That is: // At the beginning of the application MyClass instance = new MyClass(); instance.MyMethod(); instance.MyMethod(); // Faster than the first call, because now it's warmed up. Do we really have such warming-up theory in C#? If yes, why (what will the CLR do when warming-up)? And is everything the same if this method is an extension one (a static one)? BrokenGlass If by "warm up" you refer to JIT'ing then yes - if a

Error when using extension methods in C#

只愿长相守 提交于 2019-11-27 05:18:08
问题 I came across an issue that makes me think there is bug in the 3.0 framework. When I try to use extension methods I get the following error: Missing compiler required member 'System.Runtime.CompilerServices.ExtensionAttribute..ctor' When using this simple code: public static class StringUtils { static void TestExtension(this String targetString) { } } The only way to make this compile error go away is to add the following code: namespace System.Runtime.CompilerServices { public class