extension-methods

Preserving state in an extension method

送分小仙女□ 提交于 2019-12-01 16:44:06
The C# team has previously considered adding extension properties, events, etc. to C#. Per Eric Lippert: http://blogs.msdn.com/b/ericlippert/archive/2009/10/05/why-no-extension-properties.aspx For these features to be useful however, they would have to be able to store some new kind of state with an object. It seems like the only way to do this would be to use a dictionary and associate each instance of an object with whatever the additional state is. It would be useful if it were possible to copy this functionality "manually" by creating my own dictionary (and perhaps get/set extension

Specifying constructor constraint for Generic Parameter [duplicate]

戏子无情 提交于 2019-12-01 16:43:47
This question already has an answer here: Is there a generic constructor with parameter constraint in C#? 7 answers I have a collection of objects which I pass as parameter to create objects of another type (one for one). I am doing this in many places (basically converting from data objects to business objects). I want to write a generic extension method to accomplish this. But I am stuck because I don't know how I can specify constraint that business object has a constructor taking data object as parameter. Following is code of my function: public static IList<T> ConvertTo<A,T>(this

Implementing multiparameter C++ template like behaviour on C# using Policy Pattern

无人久伴 提交于 2019-12-01 16:22:11
I'm trying to implement a c++ like template with C# generics and policy pattern based on this answer This is a sample of the pattern: interface ISomePolicy<T,U> { void _doSomething(U u); } class MyClass<T,U>: ISomePolicy<int, double>, ISomePolicy<int, int> { internal T myElement {get;set;} public MyClass(T Element) { myElement = Element; } void ISomePolicy<int, double>._doSomething(double u) { Console.WriteLine("this is int, double"); } void ISomePolicy<int, int>._doSomething(int u) { Console.WriteLine("this is int, int"); } } static class MyClassExtension { //What I want to do public static

Operators as method parameters in C#

蓝咒 提交于 2019-12-01 16:05:18
I don't think it's possible to use operators as a parameters to methods in C# 3.0 but is there a way to emulate that or some syntactic sugar that makes it seem like that's what's going on? I ask because I recently implemented the thrush combinator in C# but while translating Raganwald's Ruby example (1..100).select(&:odd?).inject(&:+).into { |x| x * x } Which reads "Take the numbers from 1 to 100, keep the odd ones, take the sum of those, and then answer the square of that number." I fell short on the Symbol#to_proc stuff. That's the &: in the select(&:odd?) and the inject(&:+) above. Well, in

Type extension errors for Dictionary<'K, 'V>

吃可爱长大的小学妹 提交于 2019-12-01 16:03:39
问题 The following type extension module Dict = open System.Collections.Generic type Dictionary<'K, 'V> with member this.Difference(that:Dictionary<'K, 'T>) = let dict = Dictionary() for KeyValue(k, v) in this do if not (that.ContainsKey(k)) then dict.Add(k, v) dict gives the error: The signature and implementation are not compatible because the declaration of the type parameter 'TKey' requires a constraint of the form 'TKey : equality But when I add the constraint it gives the error: The declared

How to deal with a flaw in System.Data.DataTableExtensions.CopyToDataTable()

感情迁移 提交于 2019-12-01 15:57:51
问题 Hey guys, so I've come across something which is perhaps a flaw in the Extension method .CopyToDataTable. This method is used by Importing (in VB.NET) System.Data.DataTableExtensions and then calling the method against an IEnumerable. You would do this if you want to filter a Datatable using LINQ, and then restore the DataTable at the end. i.e: Imports System.Data.DataRowExtensions Imports System.Data.DataTableExtensions Public Class SomeClass Private Shared Function GetData() As DataTable

Can you make an Extension Method Static/Shared?

跟風遠走 提交于 2019-12-01 15:57:40
问题 OK, I've probably misunderstood something here but, as far as I can see ... An extension method has to be contained in a module, not a class You can't make methods in modules Static/Shared Therefore you can't use an extension method on a class without instantiating it. In other words you can't make an extension method on String called "MyExtensionMethod" and use: String.MyExtensionMethod("String") But instead .. Dim test As String test.MyExtensionMethod("string") Is this correct? Or is there

How can I set properties on all items from a linq query with values from another object that is also pulled from a query?

断了今生、忘了曾经 提交于 2019-12-01 15:56:44
I have a query pulling from a database: List<myClass> items = new List<myClass>(from i in context select new myClass { A = i.A, B = "", // i doesn't know this, this comes from elsewhere C = i.C } I also have another query doing a similar thing: List<myClass2> otherItems = new List<myClass2>(from j in context select new myClass2 { A = j.A, // A is the intersection, there will only be 1 A here but many A's in items B = j.B } In reality these classes are much larger and query data that is separated not only by database but by server as well. Is it possible to use a LINQ query to populate the

Implementing multiparameter C++ template like behaviour on C# using Policy Pattern

陌路散爱 提交于 2019-12-01 15:18:07
问题 I'm trying to implement a c++ like template with C# generics and policy pattern based on this answer This is a sample of the pattern: interface ISomePolicy<T,U> { void _doSomething(U u); } class MyClass<T,U>: ISomePolicy<int, double>, ISomePolicy<int, int> { internal T myElement {get;set;} public MyClass(T Element) { myElement = Element; } void ISomePolicy<int, double>._doSomething(double u) { Console.WriteLine("this is int, double"); } void ISomePolicy<int, int>._doSomething(int u) { Console

Specifying constructor constraint for Generic Parameter [duplicate]

放肆的年华 提交于 2019-12-01 14:32:15
问题 This question already has answers here : Is there a generic constructor with parameter constraint in C#? (7 answers) Closed 6 years ago . I have a collection of objects which I pass as parameter to create objects of another type (one for one). I am doing this in many places (basically converting from data objects to business objects). I want to write a generic extension method to accomplish this. But I am stuck because I don't know how I can specify constraint that business object has a