extension-methods

Extension methods not allowed in nested static classes?

别说谁变了你拦得住时间么 提交于 2019-12-10 02:48:40
问题 Why is this? I would find it really nice to be able to have some extension methods locked down to be used only within one of my classes. Don't really want to have certain extension methods available everywhere... and they look so much nicer than regular static methods :P For clarification: The reason I want these extension methods is because I am extending a Form, which has a DataGridView on it. And I am getting very tired of lines like these: foreach(var row in grid.Rows.OfType

How to use reflection to get extension method on generic type

旧城冷巷雨未停 提交于 2019-12-10 02:06:18
问题 From various sources on teh interwebs I've gleaned this following function: public static Nullable<T> TryParseNullable<T>(this Nullable<T> t, string input) where T : struct { if (string.IsNullOrEmpty(input)) return default(T); Nullable<T> result = new Nullable<T>(); try { IConvertible convertibleString = (IConvertible)input; result = new Nullable<T>((T)convertibleString.ToType(typeof(T), CultureInfo.CurrentCulture)); } catch (InvalidCastException) { } catch (FormatException) { } return result

Extension methods for specific generic types

二次信任 提交于 2019-12-10 01:59:00
问题 I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me: What I want to do is something like the following: type IEnumerable<int> with member this.foo = this.ToString() Yet it gives me the compiler error (underlining the int keyword): Unexpected identifier in type name. Expected infix operator, quote symbol or other token. The following does work, though it does not specifically bind

Extension Method to Get the Values of Any Enum

馋奶兔 提交于 2019-12-10 01:56:47
问题 I've been trying to create an extension method, that would work on any enum, to return its values. Instead of doing this: Enum.GetValues(typeof(BiasCode)).Cast<BiasCode>() It would be nice to do this: new BiasCode().Values() It would even be better without new , but that's another issue. I have a .NET fiddle that has a solution that's close (code shown below). The problem with this code is that the extension method is returning List<int> . I would like to have it return a list of the enum

Why don't my HtmlHelper extensions work?

£可爱£侵袭症+ 提交于 2019-12-10 01:43:15
问题 I'm building an ASP.Net MVC website. Rather than have everything in one project, I've decided to separate the Web, Model and Controller out into different projects in the same solution, that reference each-other. The referencing goes like this: Web ---[references]---> Controller ---[references]---> Model Now I wanted to add 2 custom methods to the HtmlHelper class - they're called "IncludeScript" and "IncludeStyle". They each take a single string parameter, and generate a script or link tag

Null safe way to get values from an IDataReader

旧街凉风 提交于 2019-12-10 01:25:31
问题 (LocalVariable)ABC.string(Name)= (Idatareader)datareader.GetString(0); this name value is coming from database.. what happening here is if this name value is null while reading it's throwing an exception? I am manually doing some if condition here. I don't want to write a manual condition to check all my variables.. I am doing something like this now.. string abc = (Idatareader)datareader.GetValue(0); if(abc = null) //assiging null else assiging abc value is there something like can we write

Parse any Object Model into Anything [closed]

梦想的初衷 提交于 2019-12-10 00:45:40
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 months ago . In my .NET project, I want an extension method or library to parse any object model into anything. This is a very important issue for me. 回答1: this Is Extention Method In My Framework Library. It's a Perfect Method. Code public static class GenericExtensions { #region ParseTo public static TOut ParseTo<TInput,

How to check name of element with WriteEndElement

与世无争的帅哥 提交于 2019-12-09 17:17:59
问题 I'm writing xml with XmlWriter. My code has lots of sections like this: xml.WriteStartElement("payload"); ThirdPartyLibrary.Serialise(results, xml); xml.WriteEndElement(); // </payload> The problem is that the ThirdPartyLibrary.Serialise method is unreliable. It can happen (depending on the variable results ) that it doesn't close all the tags it opens. As a consequence, my WriteEndElement line is perverted, consumed closing the library's hanging tags, rather than writing </payload> . Thus I

An analog of String.Join(string, string[]) for IEnumerable<T>

本秂侑毒 提交于 2019-12-09 16:51:48
问题 class String contains very useful method - String.Join(string, string[]) . It creates a string from an array, separating each element of array with a symbol given. But general - it doesn't add a separator after the last element! I uses it for ASP.NET coding for separating with " <br /> " or Environment.NewLine . So I want to add an empty row after each row in asp:Table . What method of IEnumerable<TableRow> can I use for the same functionality? 回答1: I wrote an extension method: public static

How to make dictionary extension-methods?

筅森魡賤 提交于 2019-12-09 14:45:46
问题 I'm trying to write a Dictionary extension that works independently of the data types of Key/Value. I tried pass it by using the object data type, assuming that it will works with any type. My code: public static class DictionaryExtensionsClass { public static void AddFormat(this Dictionary< ?? unknow type/*object*/, ??unknow type/*object*/> Dic, ??unknow type/*object*/ Key, string str, params object[] arglist) { Dic.Add(Key, String.Format(str, arglist)); } } 回答1: You just make the method