extension-methods

How to create extension methods for Types

僤鯓⒐⒋嵵緔 提交于 2019-11-29 13:26:54
I am writing an extension method for parsing JSON string for any given type. I wanted to use the method on types instead of instances like many examples we already know, but I somewhat feel it is not supported by Visual Studio. Can someone enlighten me here? The following is the method: public static T ParseJson<T>(this T t, string str) where T: Type { if (string.IsNullOrEmpty(str)) return null; var serializer = new JavaScriptSerializer(); var obj = serializer.Deserialize<T>(str); return obj; } I want to call the method in this fashion: var instance = MyClass.ParseJson(text); Thanks The short

String.IsNullOrBlank Extension Method

匆匆过客 提交于 2019-11-29 11:51:45
问题 I continuously check string fields to check if they are null or blank. if(myString == null || myString.Trim().Length == 0) { throw new ArgumentException("Blank strings cannot be handled."); } To save myself a bit of typing is it possible to create an extension method for the String class that would have the same effect? I understand how extension methods can be added for a class instance but what about adding a static extension method to a class? if(String.IsNullOrBlank(myString)) { throw new

Is it necessary to throw a NullReferenceException from an extension method? [duplicate]

柔情痞子 提交于 2019-11-29 11:17:22
This question already has an answer here: ArgumentNullException or NullReferenceException from extension method? 5 answers If I define an extension method such as this: static public String ToTitleCase(this string instance, CultureInfo culture) { if (instance == null) throw new NullReferenceException(); if (culture == null) throw new ArgumentNullException("culture"); return culture.TextInfo.ToTitleCase(instance); } Is it necessary for me to check the string instance for null and throw an null reference exception myself? Or does the CLR treat extension methods like instance methods in this case

Is it possible to create constructor-extension-method ? how?

百般思念 提交于 2019-11-29 10:30:45
问题 Is it possible to add a constructor extension method ? I want to add a List< T > constructor to receive specific amount of bytes out of a given partially filled buffer (without the overhead of copying only the relevant bytes and so on): ... public static List<T>(this List<T> l, T[] a, int n) { for (int i = 0; i < n; i++) l.Add(a[i]); } ... so the usage would be: List<byte> some_list = new List<byte>(my_byte_array,number_of_bytes); I've already added an AddRange extension method: public static

How to call extension method “ElementAt”of List<T> with reflection?

做~自己de王妃 提交于 2019-11-29 10:22:28
I have problem that after creating object "oListType01" of type List < MyClass01 > and after assigning it to the another objet "oObjectType " of type "object" I can not access any more function "ElementAt(1)". I tried by using reflection but I am always getting exception(parameter conflict) in "Invoke" method. Does anyone knows why ? Milan MyClass01 oMy1 = new MyClass01(); oMy1._ID = "1"; MyClass01 oMy2 = new MyClass01(); oMy2._ID = "3"; IList<MyClass01> oListType01 = new List<MyClass01>(); oListType01.Add(oMy1); oListType01.Add(oMy2); object oObjectType = new object(); oObjectType =

Why must C# extension methods be defined in static classes? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-29 09:26:25
问题 This question already has an answer here: Why are extension methods only allowed in non-nested, non-generic static class? 3 answers I understand that C# extension methods must be static. What I don't understand is why these extensions can't be defined in non static classes or generic ones? Update: I am interested in the reason behind this design decision. 回答1: This is more of an observation than an answer, but... When you call an instance method, a reference to the object you are calling is

Override (or shadow) a method with extension method? [duplicate]

送分小仙女□ 提交于 2019-11-29 09:25:52
Possible Duplicate: Is there any way in C# to override a class method with an extension method? Is it possible to override or shadow ( new in C#) instance methods with extension methods? No. From MSDN : You can use extension methods to extend a class or interface, but not to override them. An extension method with the same name and signature as an interface or class method will never be called. At compile time, extension methods always have lower priority than instance methods defined in the type itself. 来源: https://stackoverflow.com/questions/1745166/override-or-shadow-a-method-with-extension

Extension methods on a struct

做~自己de王妃 提交于 2019-11-29 09:03:45
Can you add extension methods to a struct? 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; } } } It is possible to add extension methods to structures, but there is an important caveat. Normal struct methods methods accept this as a ref parameter, but C# will not allow the definition of extension methods which do so. While struct methods

How can I know items is in the enum?

强颜欢笑 提交于 2019-11-29 08:08:58
In this question , I use xor operator between enum with [Flags] attribute as following: [Flags] enum QueryFlag { None = 0x1, ByCustomer = 0x2, ByProduct = 0x4, ByDate = 0x8 } QueryFlag flags = QueryFlag.ByCustomer | QueryFlag.ByProduct; To add an QueryFlag, of course we should use | operator. flags |= QueryFlag.ByDate; To remove one, I have a different way with Dan Tao's answer . I'm using: flags ^= QueryFlag.ByProduct; while he is using: flags &= ~QueryFlag.ByProduct; Obviously his answer is correct and easy to understand. I thought I made a mistake. But after a deep thought I got: a,b a^b a&

Extending Enums, Overkill?

流过昼夜 提交于 2019-11-29 07:49:37
I have an object that needs to be serialized to an EDI format. For this example we'll say it's a car. A car might not be the best example b/c options change over time, but for the real object the Enums will never change. I have many Enums like the following with custom attributes applied. public enum RoofStyle { [DisplayText("Glass Top")] [StringValue("GTR")] Glass, [DisplayText("Convertible Soft Top")] [StringValue("CST")] ConvertibleSoft, [DisplayText("Hard Top")] [StringValue("HT ")] HardTop, [DisplayText("Targa Top")] [StringValue("TT ")] Targa, } The Attributes are accessed via Extension