extension-methods

How to get MethodInfo for generic extension method?

心已入冬 提交于 2019-12-01 00:05:07
问题 I have an IEnumerable<T> , and I want to call the Enumerable.Contains method by reflection. I'm just struggling to get the syntax right. Here's what I currently have: var containsMethod = typeof(Enumerable).GetMethod("Contains", new[] { typeof(IEnumerable<T>), typeof(T) }); This just comes back with a null. What is the correct way to get the MethodInfo ? 回答1: What is the correct way to get the MethodInfo? You have to find the generic method - which is unfortunately a bit of a pain - and then

Using Extensions: Weighing The Pros vs Cons

拟墨画扇 提交于 2019-12-01 00:04:30
Recently I asked a question about how to clean up what I considered ugly code. One recommendation was to create an Extension Method that would perform the desired function and return back what I wanted. My first thought was 'Great! How cool are Extensions...' but after a little more thinking I am starting to have second thoughts about using Extensions... My main concern is that it seems like Extensions are a custom 'shortcut' that can make it hard for other developers to follow. I understand using an Extension can help make the code syntax easier to read, but what about following the voice

Why not allow Extension method definition in nested class? [closed]

大城市里の小女人 提交于 2019-11-30 23:43:08
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 11 months ago . I would find it convenient/logical to write my exensions for a class in a nested class. The main reason is I could simply name that class Extensions and let it's outer naming scope give it a unique type name for the compiler. What is the technical reason to disallow

C# Extension method precedence

╄→гoц情女王★ 提交于 2019-11-30 23:40:31
问题 I'm a bit confused about how extension methods work. If I'm reading this correctly http://msdn.microsoft.com/en-us/library/bb383977.aspx and this If an extension method has the same signature as a method in the sealed class, what is the call precedence?. Then the following should write out "Instance", but instead it writes "Extension method". interface IFoo { } class Foo : IFoo { public void Say() { Console.WriteLine("Instance"); } } static class FooExts { public static void Say(this IFoo foo

C#: implicit operator and extension methods

谁说我不能喝 提交于 2019-11-30 22:50:55
问题 I am trying to create a PredicateBuilder<T> class which wraps an Expression<Func<T, bool>> and provides some methods to easily build up an expression with various And and Or methods. I thought it would be cool if I could use this PredicateBuilder<T> as an Expression<Func<T, bool>> directly, and thought this could be done by having an implicit operator method thing. Stripped down version of the class looks like this: class PredicateBuilder<T> { public Expression<Func<T, bool>> Predicate { get;

Does Groovy have extension methods like in C#?

谁说我不能喝 提交于 2019-11-30 22:49:33
问题 Can I add to a method to a final class somehow like in C#? So I could do something like: "Some text".myOwnFunction(); Instead of: MyStaticClass.myOwnFunction("Some text"); 回答1: You can either add it to all future string instances via the metaClass String.metaClass.myOwnFunction = {-> delegate.length() } assert "Tim".myOwnFunction() == 3 Or you can add it to a single instance String a = "Tim" a.metaClass.myOwnFunction = {-> delegate.length() } assert a.myOwnFunction() == 3 Or you can add these

Can you add extension methods that you call like static methods?

泄露秘密 提交于 2019-11-30 22:21:00
According to Microsoft, "Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type". Is there a way to add an extension method that it called as if it was a static method? Or to do something else that has the same effect? Edit: By which I mean "called as if it was a static method on the extended class". Sorry for the ambiguity. According to Microsoft, "Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type". Yes, extension methods are static methods.

NVelocity extension method ASP.NET webform

故事扮演 提交于 2019-11-30 21:13:46
问题 I was wondering if it's possible to use an extension method with asp.net webforms and nvelocity. I would like to set some defaults if the string value is null or empty. Example of .vm file: Example of my email body... Billable Status: $billableStatus.Evaluate() rest of my email body... Attempted extension method: public static class Helper { public static string Evaluate(this string value) { if (String.IsNullOrEmpty(value)) return "Not Provided"; else return value; } } Or is there an

C#: Best practice for validating “this” argument in extension methods

江枫思渺然 提交于 2019-11-30 20:48:32
Let's say I have an extension method public static T TakeRandom<T>(this IEnumerable<T> e) { ... To validate the argument e, should I: A) if (e == null) throw new NullReferenceException() B) if (e == null) throw new ArgumentNullException("e") C) Not check e What's the consensus? My first thought is to always validate arguments, so thrown an ArgumentNullException. Then again, since TakeRandom() becomes a method of e, perhaps it should be a NullReferenceException. But if it's a NullReferenceException, if I try to use a member of e inside TakeRandom(), NullReferenceException will be thrown anyway.

Generate Extension Methods using System.CodeDom

回眸只為那壹抹淺笑 提交于 2019-11-30 20:45:20
Has anyone ever tried to generate extension methods using System.CodeDom under .NET 4.0? There doesn't seem to be any way to specify a CodeMemberMethod or CodeParameterDeclarationExpression as being an extension method/parameter. If this isn't possible, are there any good workarounds? Thanks Apparently CodeDom isn't able to generate the correct code for the first parameter of an extension method, but you can cheat it like this: var param = new CodeParameterDeclarationExpression("this string", "s"); It will blissfully ignore the fact that "this string" is not a valid type... See Extension