extension-methods

Mathematical Equality for Func<int, int>

旧时模样 提交于 2019-12-14 02:20:04
问题 I have a Sequence type that implements ISequence . ISequence implements IEnumerable<Element> where Element is another custom type. Currently, my Sequence type stores the instructions for generating the N-th term of a sequence as a Func<int, int> . This is convenient because it allows me to call NTerm(int n) with a simple call to the Func and to create Sequence s with a lambda. I would like to avoid changing that approach. I would like to check equality of two Sequence objects based on

Cast object without any more info than its System.Type

亡梦爱人 提交于 2019-12-14 01:28:34
问题 For a SO question i quite recently wrote a generic extension method that should load an object from another, i.e. assign all the properties of the source to the target and do so recursively if the property is a reference-type. I got quite far using reflection but i hit a problem when it came to the types of properties that are reference-types, here is my first approach: First approach: public static void Load<T>(this T target, T source, bool deep) { foreach (PropertyInfo property in typeof(T)

Extending Selenium: How to call commands?

泄露秘密 提交于 2019-12-14 00:56:05
问题 I read about user extensions and extending selenium but am wondering how to call a command from within a custom command I'm creating. I added a file similar to the following to Selenium core extensions (user-extensions.js) in Selenium IDE Options. // selenium-action-example.js Selenium.prototype.doExample = function() { this.doOpen("/"); // doesn't waitForPageToLoad like the command does // These two commands are equivalent to the clickAndWait command. NOT! // For proof, see the

C# Extension Method for Null or value

断了今生、忘了曾经 提交于 2019-12-13 22:28:55
问题 How to write an extension method that should check value of the object,if object is null then it should return null otherwise value{without doing casting at receiving end}. something like... public static object GetDefault(this object obj) { if (obj == null) return null; else return obj; } I mean without casting can i check for null? int? a=a.GetDefault(); ContactType type=type.GetDefault(); [For EnumType] string s=a.GetDefault() 回答1: This should work: public static class ExtensionMethods {

Setting a WebControls TabIndex based on the ControlId of another control

℡╲_俬逩灬. 提交于 2019-12-13 19:22:12
问题 I have an ASP.NET Webforms site that is regularly having features added. The majority of time a new WebControl is added to the page and I need to increment the TabIndex to all subsequent controls on the page. I'd prefer a more robust solution than choosing an arbitrary gap between the initial assigned tab indexes. Setting the tab indexes using the designer tab order functionality is one option but I'd prefer to stay in the source view. Ideally, if I had, for example, three check boxes I'd

Should I keep extensions in their own “Extensions” file?

匆匆过客 提交于 2019-12-13 15:13:35
问题 I have a few extensions that I'd like to use throughout my project but I'm unsure if I should keep them in their own file, or if I should just make specific extensions in each viewController file. Which would be more efficient or better? 回答1: I think this is largely a matter of style rather than efficiency. That said, yes you should put them in their own file. That way if you want to reuse them between projects, you can simply drag and drop them into a new project. In general, it is best to

Convert generic free function into Array extension

放肆的年华 提交于 2019-12-13 09:54:01
问题 I wrote some code to perform run length encoding and decoding. I have my encoding function as a method in an extension to Array, but I can't make the decoding in a similar fashion. Is this possible? I can't find any ways of introducing new generic types into extensions. func runLengthDecode<T: Equatable>(_ runLengthEncoding: [(element: T, count: Int)]) -> [T] { return runLengthEncoding.flatMap{ repeatElement($0.element, count: $0.count)} } I wish this function were a method on Array, as well.

Extension method Gets “No overload for method” Error

柔情痞子 提交于 2019-12-13 08:26:31
问题 I just recently upgraded this project from ASP.Net 3.5 to 4.0 so that I could use the concurrentDictionary instead of Dictionary because of the thread safe feature. To use it I created an extension using code found in help forums. It is all very close to working and I don't know how I can modify the extension for it to work properly. Here is the code: var catalogs = (from _catalog in entities.catalogs from rolePermission in entities.c_roleperm from _group in entities.c_group from _user in

Attach event handler to be called only once

这一生的挚爱 提交于 2019-12-13 03:40:06
问题 I am currently trying to write an extension function to be able to easily attach an action that is only used once when the event is fired, then unsubscribed. I am trying something like this: public static void AttachOnce<TEventArgs>([NotNull] this EventHandler<TEventArgs> me, [NotNull] Action<object, TEventArgs> action) where TEventArgs : System.EventArgs { var handler = me; EventHandler<TEventArgs> wrappedAction = null; wrappedAction = (sender, args) => { action(sender, args); handler -=

Split string extension with generic type?

巧了我就是萌 提交于 2019-12-13 01:34:02
问题 I would like to create a Split extension that would allow me to split any string to a strongly-typed list. I have a head start, but since I was going to reuse this in many projects, I would like to get input from the community (and so you can add it to your own toolbox ;) Any ideas from here? public static class Converters { public static IEnumerable<T> Split<T>(this string source, char delimiter) { var type = typeof(T); //SPLIT TO INTEGER LIST if (type == typeof(int)) { return source.Split