extension-methods

How do I add multiple attributes to an Enum?

蹲街弑〆低调 提交于 2019-12-20 10:35:12
问题 I have a SQL lookup-table called ClientCreditResolutionPlanActionType that I want to convert to an enum in c#. Very basic request, right? Right. My table, now enum, however, has several columns, or now, description properties that need to go with it: StatusIcon StatusText TypeText So I figured I could do ... namespace System.ComponentModel { class StatusIconAttribute : Attribute { public string StatusIcon; public StatusIconAttribute(string statusIcon) { StatusIcon = statusIcon; } } class

Extension methods overridden by class gives no warning

我们两清 提交于 2019-12-20 09:54:50
问题 I had a discussion in another thread, and found out that class methods takes precedence over extension methods with the same name and parameters. This is good as extension methods won't hijack methods, but assume you have added some extension methods to a third party library: public class ThirdParty { } public static class ThirdPartyExtensions { public static void MyMethod(this ThirdParty test) { Console.WriteLine("My extension method"); } } Works as expected: ThirdParty.MyMethod -> "My

Partial Class vs Extension Method

强颜欢笑 提交于 2019-12-20 09:33:08
问题 I dont have much experience of using these 2 ways to extend a class or create extension methods against a class. By looking others work, I have a question here. I saw people using a parital class to extend an entity class in a project. Meanwhile, in the same project, there is another folder containing a lot extension methods to the entity class. Is it right to do so? I mean these 2 ways both work well. Could you give me some real practice how to pick one from them when I want extend a class?

Define an Extension Method for IEnumerable<T> which returns IEnumerable<T>?

喜你入骨 提交于 2019-12-20 08:49:48
问题 How do I define an Extension Method for IEnumerable<T> which returns IEnumerable<T> ? The goal is to make the Extension Method available for all IEnumerable and IEnumerable<T> where T can be an anonymous type. 回答1: The easiest way to write any iterator is with an iterator block, for example: static IEnumerable<T> Where<T>(this IEnumerable<T> data, Func<T, bool> predicate) { foreach(T value in data) { if(predicate(value)) yield return value; } } The key here is the " yield return ", which

Does _ArrayType or _ArrayProtocol not available in Swift 3.1?

六月ゝ 毕业季﹏ 提交于 2019-12-20 04:23:32
问题 I was using _ArrayType in my project when I was running on swift 2.1. I upgraded to swift 3.0.2 (Xcode 8.2.1) last week and I found here that _ArrayType is changed to _ArrayProtocol and it was working well. Today I upgraded my Xcode to 8.3.1, and it gives me error: Use of undeclared type '_ArrayProtocol' . Here is my code: extension _ArrayProtocol where Iterator.Element == UInt8 { static func stringValue(_ array: [UInt8]) -> String { return String(cString: array) } } What's wrong now? Why

How can I prevent a public class that provides extension methods from appearing in Intellisense?

血红的双手。 提交于 2019-12-20 02:36:18
问题 Is there any way to 'hide' the name of a class, whose sole purpose is to provide extension methods, from Intellisense? I would like to remove the class name from the Intellisense list but need the extension methods of the class to be available to external assemblies via Intellisense in the usual way. 回答1: Ok, I have the answer to this. Hallgrim's suggestion of marking the class with.. [EditorBrowsable(EditorBrowsableState.Never)] ..does actually work but only where the assembly is being

Lambda and Expression.Call for an extension method

感情迁移 提交于 2019-12-19 17:38:51
问题 I need to implement an expression for a method like here: var prop = Expression.Property(someItem, "Name"); var value = Expression.Constant(someConstant); var contains = typeof(string).GetMethod("Contains", new[] {typeof(string)}); var expression = Expression.Call(prop, contains, value); But for my extension method: public static class StringEx { public static bool Like(this string a, string b) { return a.ToLower().Contains(b.ToLower()); } } Unfortunately, next code throws an

Extension Method and Razor Page

喜你入骨 提交于 2019-12-19 09:00:10
问题 I have defined an extension method in app_code like below. public static class Extensions { public static string Hi(this object obj) { return "hi"; } } In the razor page, anything can say Hi :) @Html.Hi(); @Request.Hi(); @this.Hi(); But @Hi() doesn't work. Is there a way to make @Hi() work? 回答1: C# only allows you to call extension methods qualified by an object instance. If you have an extension method that extends your type, you can't call it "directly"; you need to write this

Error message in XSLT with C# extension function

混江龙づ霸主 提交于 2019-12-19 08:17:12
问题 I am received the following error while trying to implement a C# extension function in XSLT. Extension function parameters or return values which have CLR type 'Char[]' are not supported.** code: <xsl:variable name="stringList"> <xsl:value-of select="extension:GetList('AAA BBB CCC', ' ')"/> </xsl:variable> <msxsl:script language="C#" implements-prefix="extension"> <![CDATA[ public string[] GetList(string str, char[] delimiter) { ... ... return str.Split(delimiter, StringSplitOptions.None); }

Why generic extension method with constraint is not recognized as extension method? [duplicate]

≡放荡痞女 提交于 2019-12-19 08:03:18
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: No type inference with generic extension method Consider two methods: public static IEnumerable<V> Merge<V> (this IEnumerable<IEnumerable<V>> coll) public static IEnumerable<V> Merge<T, V> (this IEnumerable<T> coll) where T : IEnumerable<V> Both compile just fine, in both cases the type of generic types will be known at compile time of caller, and thus the exact type of extended type. You can call both fine, but