extension-methods

ASP.NET MVC 4 property-renaming for posting

限于喜欢 提交于 2019-12-12 03:34:15
问题 Follwing convention(s) are given. Each Action has a single parameter of type BaseRequest with data depending on the Action . The ViewModel is always of type BaseResponse . What I'm trying to do is, that if a View contains a form, the POST -Action requires some sort of BaseRequest . How can I achieve correct-model binding as of the ViewModel is BaseResponse ? I already tried to add a property of XYZRequest in XYZResponse so I could bind like @Html.ChecBoxFor(m => m.RequestObject

Generic Extension with less arguments than parameters

点点圈 提交于 2019-12-12 02:54:18
问题 I stumbled with this issue, if I define a class class MyClass<T,U> { internal T myEement {get;set;} public MyClass(T Element) { myEement = Element; } } And declare extensions. static class MyClassExtension{ //What I want to do public static void nonWorkingExtension<P,T,U>(this P p, U u) where P:MyClass<T,U> { T Element = p.myEement; //Do something with u and p.Element } //What I have to do public static void workingExtension<P, T, U>(this P p, T dummy, U u) where P : MyClass<T, U> { T Element

C# Calling a dictionary within an extension class?

為{幸葍}努か 提交于 2019-12-12 02:32:46
问题 Re-written everything exactly as my program has it: class DictionaryInitializer { public class DictionarySetup { public string theDescription { get; set; } public string theClass { get; set; } } public class DictionaryInit { //IS_Revenues data public Dictionary<int, DictionarySetup> accountRevenue = new Dictionary<int, DictionarySetup>() { { 400000, new DictionarySetup {theDescription="Call", theClass="Revenues"}}, { 400001, new DictionarySetup {theDescription="Bill", theClass="Revenues"}}, {

How to write extension method which will make invocation list members in multicast C# delegate run sequentially?

好久不见. 提交于 2019-12-12 02:18:51
问题 I have an async delegate which I await in the async method: async Task M1() { Debug.WriteLine("M1.A"); await Task.Delay(10); Debug.WriteLine("M1.B"); } async Task M2() { Debug.WriteLine("M2.A"); await Task.Delay(1); Debug.WriteLine("M2.B"); } delegate Task MyDel(); async void button_Click(object sender, RoutedEventArgs e) { MyDel del = null; del += M1; del += M2; await del(); } The output is: M1.A M2.A M2.B M1.B That is, both invocation members go off simultaneously, not awaiting each other.

How can I make Linq extension method available for the entities?

守給你的承諾、 提交于 2019-12-12 01:56:21
问题 I wrote a method that extends System.Linq class. My ContainsAnyWord method allows me to search all words in a string against a string instead of comparing one string to another. Here is the method that I wrote to extend Linq public static class LinqExtension { public static bool ContainsAnyWord(this string value, string searchFor) { if (!string.IsNullOrWhiteSpace(value) && !string.IsNullOrWhiteSpace(searchFor)) { value = value.ToLower(); IEnumerable<string> tags = StopwordTool.GetPossibleTags

how to use a function as parameter in extension method?

柔情痞子 提交于 2019-12-11 23:59:01
问题 I have read that is recomended to use functions instead of predicates in the extension methods, so I am trying to do it. public static void Insert<T>(this ObservableCollection<T> paramOC, T NewElement, Func<T, T, bool> condition) { //code } I am trying to use in this way: myOC.Insert(myNewElement, e=>e.Name.CompareTo(myNewElement.Name) > 0)); But I get an error that says that the delete System.Func does not take 1 argument. However, if I use a predicate intead of the function, it works. What

What is the correct way to call an extension method (TreeNodeCollection Add method)?

醉酒当歌 提交于 2019-12-11 16:04:17
问题 The pertinent parts of my code are below. In the MyTreeView class (last block of code below), the line of code TncExtensions.TncNodeAdd(this, myTreeViewNode); generates the error CS7036 There is no argument given that corresponds to the required formal parameter 'myTreeViewNode' of 'TncExtensions.TncNodeAdd(TreeNodeCollection, MyTreeView_Abstract, MyTreeViewNode_Abstract)' Why can't the compiler figure out what the 2nd formal parameter is for my TreeNodeCollection extension method? public

Get Instance of ViewController from within extension

余生颓废 提交于 2019-12-11 14:12:29
问题 I'd like to get access to a ViewController from an extension and then override a function from a library which should change variables or invoke methods from a specific ViewController (in this case "ViewController"). How can I do that? Or is there a more recommended option? (Hint: I don't want to instantiate a new VC) import PopupDialog class ViewController: UIViewController { //e.g. variable and method to access from extension var score: Int = 0; func startGame(){ ... } } extension

How do I use the extension method Sum on a .NET list in F#?

安稳与你 提交于 2019-12-11 14:03:46
问题 Suppose I have the following record type: open System.Collections.Generic type Store = { Products: List<Product>; } member this.TotalNumberOfItems = this.Products.Sum(fun p -> p.NumberInInventory) I want to sum a count of total items in the store as in the method above, however the System.Collections.Generic.List extension methods don't seem to be available. Intellisense does not show them. How can I call Sum from F#? 回答1: You just need to open the System.Linq namespace. For example: open

Extension to constrained Dictionary Conforming to Protocol

拈花ヽ惹草 提交于 2019-12-11 10:09:42
问题 I am trying to create a protocol that allows any object to be instantiated using JSON NSData. I am trying to create an extension to [String: String] dictionary that conforms to this protocol. Unfortunately for some reason the following code doesn't work: public protocol InitializableWithData { init(data: NSData?) throws } extension Dictionary: InitializableWithData where Key: String, Value: String { public init(data: NSData?) { self.init() // Parse NSData into a [String: String] } } I get the