extension-methods

Change value using Extension Method in C# (as allowed in VB.NET)

安稳与你 提交于 2019-12-25 08:46:12
问题 I am trying to make something like string s = "Hello"; s.Clear(); And I created the extension method like this: public static void Clear(this string s) { s = string.Empty; } But, when I see the value, it does not change: I can't use ref or out because these can't be used with the keyword this . Any ideas? 回答1: This is not possible. From the documentation: Strings are immutable--the contents of a string object cannot be changed after the object is created, although the syntax makes it appear

how can I expand the hit area of a specific UIButton in Swift?

跟風遠走 提交于 2019-12-25 08:44:22
问题 In my application I have a UIButton that is quite small, so I thought about increasing the hit area of it. I found an extension for that: fileprivate let minimumHitArea = CGSize(width: 100, height: 100) extension UIButton { open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { // if the button is hidden/disabled/transparent it can't be hit if self.isHidden || !self.isUserInteractionEnabled || self.alpha < 0.01 { return nil } // increase the hit frame to be at least

F# Extension Methods on Lists, IEnumerable, etc

雨燕双飞 提交于 2019-12-25 04:45:32
问题 In C#, if I had a widget definition, say: class widget { public string PrettyName() { ... do stuff here } } and I wanted to allow for easy printing of a list of Widgets, I might do this: namespace ExtensionMethods { public static PrintAll( this IEnumerable<Widget> widgets, TextWriter writer ) { foreach(var w in widgets) { writer.WriteLine( w.PrettyName() ) } } } How would I accomplish something similar with a record type and a collection (List or Seq preferrably in F#). I'd love to have a

Swift Dictionary access value using key within extension

十年热恋 提交于 2019-12-25 04:26:54
问题 It turns out that within a Dictionary extension, the subscript is quite useless since it says Ambiguous reference to member 'subscript' . It seems I'll either have to do what Swift does in its subscript(Key) or call a function. Any ideas? For example, public extension Dictionary { public func bool(_ key: String) -> Bool? { return self[key] as? Bool } } won't work, since the subscript is said to be ambiguous. ADDED My misunderstanding came from the fact that I assumed that Key is an

Generics & static classes. Implement query helper method

折月煮酒 提交于 2019-12-25 02:59:35
问题 Currently we implement a mapping service like this (the service uses automapper, and we make use of the projection feature on it for this part) // Injected // IGenericRepository<Entity> entityRepo var query = this.entityRepo .FindAll(a => a.Id == someId) .Take(1); var result = this.mappingService .Map<Entity, EntityDto>(query) .FirstOrDefault(); I'd like to create an extension that would allow me to do the following var result = this.entityRepo .FindAll(a => a.Id == someId) .Take(1).Map

Reflection instantiation

别来无恙 提交于 2019-12-25 02:47:39
问题 I am currently trying to create a generic instance factory for which takes an interface as the generic parameter (enforced in the constructor) and then lets you get instantiated objects which implement that interface from all types in all loaded assemblies. The current implementation is as follows: public class InstantiationFactory<T> { protected Type Type { get; set; } public InstantiationFactory() { this.Type = typeof(T); if (!this.Type.IsInterface) { // is there a more descriptive

Adding Functionality to C# Structures

 ̄綄美尐妖づ 提交于 2019-12-25 00:17:36
问题 I'm trying to find how to add functionality to C# structures, and in a more elegant way than just wrapping them in entirely new classes. An extension method just isn't as powerful as an override, and most definitely cannot give a struct event firing/handling capabilities either. The specific problem I'm currently facing however is detecting and reacting to changes in a Vector2 structure's members. This is only an internal requirement though, so users of my library should be able to use

I can edit the content before printing?

青春壹個敷衍的年華 提交于 2019-12-24 19:12:24
问题 The first chrome plugin to work. I do not have detailed information. Before I print the contents of This link I want to get the specific part. <table class="receiptSectionTable2Col" > just how can I print the contents of this table. Is this possible? 回答1: I think this answer from this question will help you: HTMLElement.prototype.printMe = printMe; function printMe(query){ var myframe = document.createElement('IFRAME'); myframe.domain = document.domain; myframe.style.position = "absolute";

Simulate Inheritance by Extension

社会主义新天地 提交于 2019-12-24 16:25:02
问题 I have a library of classes that describe different pieces of connecting hardware such as nails, screws and bolts that we will call the ConnectorLibrary. I am attempting to build a library on top of that one that will handle analyzing the grip capacity of each class in that library that we will call ConnectorGripAnalysisLibrary. For this question we will work with the classes: Screw , Bolt , and Connector . Both Screw and Bolt inherit from Connector (which is an abstract class) and they are

IEnumerable reports compiler error when used with the namespace System.Collections

倾然丶 夕夏残阳落幕 提交于 2019-12-24 16:04:58
问题 I was looking at this Squares extension method which was already there in Internet. I could not get this compiling. The compiler reports something like, "The non-generic type `System.Collections.IEnumerable' cannot be used with the type arguments" . any ideas what is wrong with this code below ? any help is much appreciated. using System.IO; using System; using System.Collections; static class Program { static IEnumerable<int> Squares (this int from, int to) { for (int i=from;i<=to;i++) {