extension-methods

Timeout Pattern - How bad is Thread.Abort really?

最后都变了- 提交于 2019-11-26 04:27:38
问题 I\'ve read at various websites that Thread.Abort is not very good to use. In this case, how do you implement a timeout pattern? For instance, I\'ve read that MS uses the pattern below (which I\'ve wrapped in an extension method) throughout the framework. Personally, I think this is a pretty cool extension, but I\'m worried about the Thread.Abort. Does anyone have a better way? public static bool CallandWait(this Action action, int timeout) { Thread subThread = null; Action wrappedAction = ()

No type inference with generic extension method

血红的双手。 提交于 2019-11-26 04:27:10
问题 I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs, bool> breakCond) where TEventInvocatorParameters : EventInvocatorParameters<TEventArgs> where TEventArgs : EventArgs { p.BreakCondition = breakCond; return p; } And this class public class EventInvocatorParameters<T> where T : EventArgs { public Func<T, bool> BreakCondition { get; set; } // Other properties used below omitted for

Why is the &#39;this&#39; keyword required to call an extension method from within the extended class

六眼飞鱼酱① 提交于 2019-11-26 03:41:21
问题 I have created an extension method for an ASP.NET MVC ViewPage, e.g: public static class ViewExtensions { public static string Method<T>(this ViewPage<T> page) where T : class { return \"something\"; } } When calling this method from a View (deriving from ViewPage ), I get the error \" CS0103: The name \'Method\' does not exist in the current context \" unless I use the this keyword to call it: <%: Method() %> <!-- gives error CS0103 --> <%: this.Method() %> <!-- works --> Why is the this

Convert string[] to int[] in one line of code using LINQ

邮差的信 提交于 2019-11-26 03:24:59
问题 I have an array of integers in string form: var arr = new string[] { \"1\", \"2\", \"3\", \"4\" }; I need to an array of \'real\' integers to push it further: void Foo(int[] arr) { .. } I tried to cast int and it of course failed: Foo(arr.Cast<int>.ToArray()); I can do next: var list = new List<int>(arr.Length); arr.ForEach(i => list.Add(Int32.Parse(i))); // maybe Convert.ToInt32() is better? Foo(list.ToArray()); or var list = new List<int>(arr.Length); arr.ForEach(i => { int j; if (Int32

Convert string[] to int[] in one line of code using LINQ

不想你离开。 提交于 2019-11-26 02:50:21
I have an array of integers in string form: var arr = new string[] { "1", "2", "3", "4" }; I need to an array of 'real' integers to push it further: void Foo(int[] arr) { .. } I tried to cast int and it of course failed: Foo(arr.Cast<int>.ToArray()); I can do next: var list = new List<int>(arr.Length); arr.ForEach(i => list.Add(Int32.Parse(i))); // maybe Convert.ToInt32() is better? Foo(list.ToArray()); or var list = new List<int>(arr.Length); arr.ForEach(i => { int j; if (Int32.TryParse(i, out j)) // TryParse is faster, yeah { list.Add(j); } } Foo(list.ToArray()); but both looks ugly. Is

Extension methods must be defined in a non-generic static class

余生颓废 提交于 2019-11-26 02:30:55
问题 I\'m getting the error: Extension methods must be defined in a non-generic static class On the line: public class LinqHelper Here is the helper class, based on Mark Gavells code. I\'m really confused as to what this error means as I am sure it was working fine when I left it on Friday! using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Linq.Expressions; using System.Reflection; /// <summary> /// Helper methods for link /// </summary> public class

Operator Overloading with C# Extension Methods

て烟熏妆下的殇ゞ 提交于 2019-11-26 02:29:59
问题 I\'m attempting to use extension methods to add an operater overload to the C# StringBuilder class. Specifically, given StringBuilder sb , I\'d like sb += \"text\" to become equivalent to sb.Append(\"text\") . Here\'s the syntax for creating an extension method for StringBuilder : public static class sbExtensions { public static StringBuilder blah(this StringBuilder sb) { return sb; } } It successfully adds the blah extension method to the StringBuilder . Unfortunately, operator overloading

Razor HtmlHelper Extensions (or other namespaces for views) Not Found

送分小仙女□ 提交于 2019-11-26 02:18:33
问题 Dunno if this was happening in the PR or Beta, but if I create an extension method on HtmlHelper , it is not recognized in a Razor powered page: namespace SomeNamespace.Extensions { public static class HtmlExtensions { public static string Foo(this HtmlHelper html) { return \"Foo\"; } } } I added it to the <Namespaces> section in Web.config : <pages> <namespaces> <add namespace=\"System.Web.Mvc\" /> <!-- snip --> <add namespace=\"SomeNamespace.Extensions\"/> </namespaces> </pages> But it

How do I use Moq to mock an extension method?

北战南征 提交于 2019-11-26 01:39:51
问题 I am writing a test that depends on the results of an extension method but I don\'t want a future failure of that extension method to ever break this test. Mocking that result seemed the obvious choice but Moq doesn\'t seem to offer a way to override a static method (a requirement for an extension method). There is a similar idea with Moq.Protected and Moq.Stub, but they don\'t seem to offer anything for this scenario. Am I missing something or should I be going about this a different way?

Mocking Extension Methods with Moq

我怕爱的太早我们不能终老 提交于 2019-11-26 00:59:08
问题 I have a preexisting Interface... public interface ISomeInterface { void SomeMethod(); } and I\'ve extended this intreface using a mixin... public static class SomeInterfaceExtensions { public static void AnotherMethod(this ISomeInterface someInterface) { // Implementation here } } I have a class thats calling this which I want to test... public class Caller { private readonly ISomeInterface someInterface; public Caller(ISomeInterface someInterface) { this.someInterface = someInterface; }