extension-methods

C# Logging design: Extension method, alternatives?

…衆ロ難τιáo~ 提交于 2019-12-06 10:23:30
I'd like to write a logger that can be easily appended to any class in my current project. For development, it will be convenient to log messages to the console, whereas in the final release I'd like to log to a file or something. One should be able to change the behavior by editing just a few lines of code, or ideally a settings file. What I have so far is this class structure: public interface ILogger { void LogMessage(String message); // ... other logging functions (exceptions, time etc.) don't matter here }; public interface ILoggable { }; public static class LoggingProvider { private

Extension methods in Mono 2.4 and RhinoMocks 3.5

浪子不回头ぞ 提交于 2019-12-06 10:23:28
I am playing around with MonoDevelop 2.0 and Mono 2.4 in Ubuntu. I have run into problems with extension methods not being available (eg mockView.Stub(...)) in RhinoMocks 3.5 for AAA style tests. I downloaded the RhinoMocks dll from Ayende's site rather than compiled from source. My project in MonoDevelop is setup to target framework 3.5 Using the RhinoMocks c#2.0 syntax with static methods on the RhinoMocksExtensions class works. (e.g RhinoMocksExtensions.Stub(authSvc, delegate(IAuthService svc)) Should AAA syntax (and RhinoMocks in general) work with Mono 2.4 or is it likely I've not setup

Extend Enum with flag methods?

守給你的承諾、 提交于 2019-12-06 09:55:44
I have found good examples on how to create extension methods to read out single values from bitwise enums. But now that C# 4 has added the HasFlag method they are really not needed. What I think would be really helpful though is an extension to SET a single flag! I have many situations where I need to set the flag values individually. I want an extension method with this signature: enumVariable.SetFlag(EnumType.SingleFlag, true); OR possibly: enumVariable.SetFlag<EnumType>(EnumType.SingleFlag, true); Today I found a solution on http://hugoware.net/blog/enums-flags-and-csharp . Thanks Hugo!

Is there a way I can dynamically define a Predicate body from a string containing the code?

爷,独闯天下 提交于 2019-12-06 07:58:34
问题 This is probably a stupid question, but here goes. I would like to be able to dynamically construct a predicate < T > from a string parsed from a database VARCHAR column, or any string, for that matter. For example, say the column in the database contained the following string: return e.SomeStringProperty.Contains("foo"); These code/string values would be stored in the database knowing what the possible properties of the generic "e" is, and knowing that they had to return a boolean. Then, in

RazorEngine extension method fails with RuntimeBinderException after upgrade to Razor 2/ RE 3.2

泄露秘密 提交于 2019-12-06 07:01:44
I have a RazorEngine project that fails following an upgrade to Razor 2.0 and RazorEngine 3.2.0 This worked fine in the previous Razor 1.0 based version of RazorEngine (3.0.8). I have an instance ( myInstance ) of a class ( MyClass ) and and extension method: namespace MyCompany.Extensions { public static class MyClassExtensions { public static string ExtensionMethod(this MyClass thing) { // do stuff } } } I want to call this in a RazorEngine view (simplified example, there are loads of these methods, and all fail the same way): @using MyCompany.Extensions @using MyCompany @{ var myInstance =

Passing interface as a parameter to an extension method

泄露秘密 提交于 2019-12-06 04:31:00
问题 I have used extension methods to extend html helpers to make an RSS repeater: public static string RSSRepeater(this HtmlHelper html, IEnumerable<IRSSable> rss) { string result=""; foreach (IRSSable item in rss) { result += "<item>" + item.GetRSSItem().InnerXml + "</item>"; } return result; } So I make one of my business objects implement IRSSable, and try to pass this to the HTML helper. But I just cannot seem to make it work, I have tried: <%=Html.RSSRepeater(ViewData.Model.GetIssues(null,

Extension methods not showing

∥☆過路亽.° 提交于 2019-12-06 03:54:42
I am creating extension methods for the HtmlHelper class in an MVC web app. Nothing is showing, not even the default InputExtensions. public static class HtmlHelpers { public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script) { if (!RegisteredScriptIncludes.ContainsValue(script)) { RegisteredScriptIncludes.Add(RegisteredScriptIncludes.Count, script); } } public static string RenderScripts(this HtmlHelper htmlhelper) { var scripts = new StringBuilder(); foreach (string script in RegisteredScriptIncludes.Values) { scripts.AppendLine("<script src='" + script + "' type=

What is the correct way to chain methods in .Net

北慕城南 提交于 2019-12-06 03:43:46
In .Net, you can chain methods returning a value or by using a void. Is one of them the "right way"? So you could say 1) Foo myFoo = new Foo(); myfoo.Bars = myBars.DoSomethingCool(x) .DoSomethingElse(y) .AndSomethingElse(z); public static IList<IBar> DoSomethingCool(this IList<IBar> source, object x) { IList<IBar> result = //some fn(source) return result; } In this case, all 3 extension methods need to return IList (the type for myFoo.Bars) or it could also be written as 2) myBars.DoSomethingCool(x) .DoSomethingElse(y) .AndSomethingElse(z); public static void DoSomethingCool(this IList<IBar>

Extending the Enumerable class in c#?

对着背影说爱祢 提交于 2019-12-06 03:29:13
问题 I have situation to extend the Enumerable class in c# to add the new Range method that accepts long parameters. I cannot define the method like this public static IEnumerable<long> Range(this Enumerable source, long start, long length) { for (long i = start; i < length; i++) { yield return i; } } Since extension methods are accesible only through its objects. And it gives me an error 'System.Linq.Enumerable': static types cannot be used as parameters Can someonce clarify me how to do this

Open DTE solution from another program (not add-in)

二次信任 提交于 2019-12-06 02:00:01
Is it possible to modify a solution, and use envdte tools, from a command-line project ? I have an add-in that modifies a solution. But... the changes are required for over a hundred projects... So I'd like to make a c# program that has the same logic, only it iterates through all solution files. The add-in starts with EnvDTE.Solution solution = (EnvDTE.Solution)application.Solution; where DTE2 application is passed from the add-in... How can I get the same solution, which then I query for projects... From a separate program, that will only know the solutionPath ? Is it possible to open the