extension-methods

How to hide extension methods from derived classes?

江枫思渺然 提交于 2020-01-13 11:51:09
问题 I have a base abstract class to implement template method pattern. public abstract class OptionalParameter { //Template Method Pattern public string GenerateQueryString() { return this.GenerateQueryStringWithParameters(); } } And I have an extension method for all OptionalParameter type. public static class OptionalParameterExtensions { public static string GenerateQueryStringWithParameters(this OptionalParameter optionalParameters) { } } I inherited a class from OptionalParameter named

Generic Enum to SelectList extension method

雨燕双飞 提交于 2020-01-13 10:08:41
问题 I need to create a SelectList from any Enum in my project. I have the code below which I create a select list from a specific enum, but I'd like to make an extension method for ANY enum. This example retrieves the value of the DescriptionAttribute on each Enum value var list = new SelectList( Enum.GetValues(typeof(eChargeType)) .Cast<eChargeType>() .Select(n => new { id = (int)n, label = n.ToString() }), "id", "label", charge.type_id); Referencing this post, how do I proceed? public static

What overhead is associated with an extension method at runtime? (.NET) [duplicate]

柔情痞子 提交于 2020-01-13 08:40:54
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Extension Method Performance In a data crunching application that is CPU and/or memory access bound, is the overhead of a one line extension method noticable? Is it any higher than a normal function call, or is it simply a compiler/IDE abstraction? For instance, would the following function be ill advised if it was being called upwards of several thousand times a second: public static void WriteElementString

ASP.NET MVC Html Helper

大城市里の小女人 提交于 2020-01-12 09:58:29
问题 I try to create some Html Helpers which will have an opening tag and closing tag which will include other contents like the Html.BeginForm does. For example in Razor we can use the Html.BeginForm helper which has the following syntax: @using (Html.BeginForm()) { } This code will include the contents of curly brackets within a and . The only way that I solved opening and closing a tag with contents is by using two html helpers. I define two html helpers: public static MvcHtmlString StartForm

“unrecognized selector sent to class” when calling category method from a library

那年仲夏 提交于 2020-01-12 03:32:06
问题 Problem This question may seem a bit long, but I try to give as much information as possible, since I am really staggered by this. I am currently working an a library which should automate XML document parsing. But I am running into a problem now testing the library for the first time. I have a library class called CSXDocumentLayout which represents the layout of a document. This class contains the private method - (NSError *)readLayoutDocument:(NSString *)fpath called from an init method. /*

What fluent interfaces have you made or seen in C# that were very valuable? What was so great about them?

折月煮酒 提交于 2020-01-11 15:29:10
问题 "Fluent interfaces" is a fairly hot topic these days. C# 3.0 has some nice features (particularly extension methods) that help you make them. FYI, a fluent API means that each method call returns something useful, often the same object you called the method on, so you can keep chaining things. Martin Fowler discusses it with a Java example here. The concept kooks something like this: var myListOfPeople = new List<Person>(); var person = new Person(); person.SetFirstName("Douglas").SetLastName

What does “this” mean in a static method declaration?

China☆狼群 提交于 2020-01-11 08:42:08
问题 I've seen some code that uses the keyword this in the function parameter declaration. For example: public static Object SomeMethod( this Object blah, bool blahblah) What does the word this mean in that context? 回答1: It means SomeMethod() is an extension method to the Object class. After defining it you can call this method on any Object instances (despite it being declared static ), like so: object o = new Object(); bool someBool = true; // Some other code... object p = o.SomeMethod(someBool)

Extension methods with generics

北慕城南 提交于 2020-01-11 06:43:19
问题 I was looking at this question and i was curious why this deosn't compile. Given this code, can anyone explain why the call to IBase.Test() doesn't resolve to the correct extension method? public interface IBase { } public interface IChildA : IBase { } public interface IChildB : IBase { } public static class BaseExtensions { public static IBase Test(this IBase self) { return self; } public static T Test<T>(this T self) where T : IChildB { return self; } } public static class TestClass {

Extension methods versus inheritance

杀马特。学长 韩版系。学妹 提交于 2020-01-09 12:31:50
问题 Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times? Thanks! 回答1: Extension methods are useful, but they are harder to discover through the IDE than regular methods, since they are not attached to the original class and there are no clues as to where the code for them might reside. There are some best practice suggestions as to where to put them and how to name them, but these are only guidelines and there is no guarantee that

Extension methods versus inheritance

╄→尐↘猪︶ㄣ 提交于 2020-01-09 12:31:36
问题 Are there rules of thumb that help determine which to use in what case? Should I prefer one over the other most times? Thanks! 回答1: Extension methods are useful, but they are harder to discover through the IDE than regular methods, since they are not attached to the original class and there are no clues as to where the code for them might reside. There are some best practice suggestions as to where to put them and how to name them, but these are only guidelines and there is no guarantee that