extension-methods

Swift where condition to check if a property is implemented

ぐ巨炮叔叔 提交于 2020-01-04 04:14:06
问题 I just found another way to make a great use of protocols and protocol extensions in Swift by extending the Optional protocol to add a function so I can provide default values. I wrote a blog post about this here: https://janthielemann.de/random-stuff/providing-default-values-optional-string-empty-optional-string-swift-3-1/ The gist of the post is that I needed a clean and easy way to provide default values for optional String which are nil or empty. To do this, I created a Emptyable protocol

How FirstOrDefault extension method works?

随声附和 提交于 2020-01-04 02:28:05
问题 I was wondering on how FirstOrDefault extension method works? Which one of the following algorithms does it follows? Use: var arr = new[] {1, 2, 3, 4, 5, 6, 7}; return arr.FirstOrDefault(x => x%2 == 0); Algorithm 1: for(int i = 0; i < arr.Length; i++) { if(arr[i] % 2 == 0) return arr[i]; } return 0; Algorithm 2: var list = new List<int>(); for(int i = 0; i < arr.Length; i++) { if(arr[i] % 2 == 0) list.Add(arr[i]); } return list.Count == 0 ? 0 : list[0]; Does the FirstOrDefault algorithm is

Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true

前提是你 提交于 2020-01-04 02:09:25
问题 I've got an extension for all entities: public static class EntityBaseExtensions { public static T Clone<T>(this T item) where T : EntityBase { return item.EntityClone<T>(); } } and public virtual T EntityClone<T>() where T : EntityBase { return this.MemberwiseClone() as T; } but when i call it like: var details = user.Details.Clone(); i get Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true. any ideas? 回答1: the solution is kinda weird:

Please explain extension methods to me

会有一股神秘感。 提交于 2020-01-03 10:56:17
问题 I just looked at this posting: What is the best or most interesting use of Extension Methods you’ve seen? I've never heard of extension methods. Do they apply to every language? What is the point of them? In that particular posting I did not understand the example. 回答1: They are available to C# and VB. They allow you to simulate the addition of methods to classes that are not under your control. You could, for instance, you could add a WordCount method to string. So instead of MyStringUtils

C# Logging design: Extension method, alternatives?

和自甴很熟 提交于 2020-01-02 17:44:13
问题 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.)

Extension methods with .NET Micro Framework

偶尔善良 提交于 2020-01-02 08:19:29
问题 It seems that extensions methods are not supported/working with the .NET Micro Framework. Is there any way to get this usefull language feature working? 回答1: When you add the ExtensionAttribute class to your project you can use extension methods also in the .NET Micro Framework. namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)] public sealed class ExtensionAttribute : Attribute { } } 来源: https:/

Static method and extension method with same name

倾然丶 夕夏残阳落幕 提交于 2020-01-02 03:37:04
问题 I created extension method: public static class XDecimal { public static decimal Floor( this decimal value, int precision) { decimal step = (decimal)Math.Pow(10, precision); return decimal.Floor(step * value) / step; } } Now I try to use it: (10.1234m).Floor(2) But compiler says Member 'decimal.Floor(decimal)' cannot be accessed with an instance reference; qualify it with a type name instead . I understand there is static decimal.Floor(decimal) method. But it has different signature. Why

Extension method and Explicit casting

丶灬走出姿态 提交于 2020-01-02 02:04:58
问题 I'm using class from some assembly(source code is not available), so it is not possible to change their's code I need to add extension method for explicit cast operator, is there any way to achieve that? (I have tried to add as regular extension method, but without success) public static explicit operator MembershipUser(this MembershipUser membership, User user) { return new MembershipUser("SimplyMembershipProvider", user.UserName, user.UserId, user.Email, null, null, user.IsApproved, user

Extension method and Explicit casting

不羁的心 提交于 2020-01-02 02:03:02
问题 I'm using class from some assembly(source code is not available), so it is not possible to change their's code I need to add extension method for explicit cast operator, is there any way to achieve that? (I have tried to add as regular extension method, but without success) public static explicit operator MembershipUser(this MembershipUser membership, User user) { return new MembershipUser("SimplyMembershipProvider", user.UserName, user.UserId, user.Email, null, null, user.IsApproved, user

Create a helper function that returns Razor @ tags?

我怕爱的太早我们不能终老 提交于 2020-01-01 19:00:13
问题 Struggling to find an answer to my question, as I'm not exactly sure what 'type' a razor tag is. Essentially I want to create a helper that does something along these lines: public static xxxxxxx ScriptTag(this HtmlHelper htmlHelper, string url) { return @<script type="text/javascript" src="@Url.Content("~/" + url)" />; } The reason I want this is that I am implementing the extension methods that are outlined in this post. Basically rather than have to do: @Html.Resource(@<script src="@Url