extension-methods

Method or Extension Method to Handle InvokeRequired

心已入冬 提交于 2019-12-12 10:17:23
问题 I can't quite come up with the solution to creating a generic method to handle the InvokeRequired for void methods (I'll deal with return values later). I was thinking something like: // Probably not the best name, any ideas? :) public static void CheckInvoke(this Control instance, ,Action<object, object> action) { if (instance.InvokeRequired) { instance.Invoke(new MethodInvoker(() => action)); } else { action() } } Then I could write something like: public partial class MyForm : Form {

Object deep clone implementation

邮差的信 提交于 2019-12-12 09:49:57
问题 I have to implement generic extention deepclone method which can be used with any reference type instance to get its deep copy. I implement it as the following static class ClassCopy { static public T DeepClone<T> (this T instance) { if (instance == null) return null; var type = instance.GetType(); T copy; var flags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; var fields = type.GetFields(flags); // If type is serializable - create

Experience with fluent interfaces? I need your opinion!

笑着哭i 提交于 2019-12-12 08:56:03
问题 Sorry for this long question, it is flagged wiki since I'm asking for something that might not have a very concrete answer. If it is closed, so be it. My main question is this: How would you write a fluent interface that isn't fully defined in the base classes, so that programs that uses the fluent interfaces can tack on new words inside the existing structure, and still maintain a guiding interface so that after a dot, the intellisense only lists the keywords that actually apply at this

How to conditionally remove items from a .NET collection

浪尽此生 提交于 2019-12-12 08:19:34
问题 I'm trying to write an extension method in .NET that will operate on a generic collection, and remove all items from the collection that match a given criteria. This was my first attempt: public static void RemoveWhere<T>(this ICollection<T> Coll, Func<T, bool> Criteria){ foreach (T obj in Coll.Where(Criteria)) Coll.Remove(obj); } However this throws an InvalidOperationException, "Collection was modified; enumeration operation may not execute". Which does make sense, so I made a second

Where is the “Fold” LINQ Extension Method?

Deadly 提交于 2019-12-12 08:17:14
问题 I found in MSDN's Linq samples a neat method called Fold() that I want to use. Their example: double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 }; double product = doubles.Fold((runningProduct, nextFactor) => runningProduct * nextFactor); Unfortunately, I can't get this to compile, either in their example or in my own code, and I can't find anywhere else in MSDN (like Enumerable or Array extension methods) that mention this method. The error I get is a plain old "don't know anything about that"

“Program to an interface” using extension methods: When does it go too far?

天大地大妈咪最大 提交于 2019-12-12 07:58:32
问题 Background: In the spirit of "program to an interface, not an implementation" and Haskell type classes, and as a coding experiment, I am thinking about what it would mean to create an API that is principally founded on the combination of interfaces and extension methods. I have two guidelines in mind: Avoid class inheritance whenever possible. Interfaces should be implemented as sealed class es. (This is for two reasons: First, because subclassing raises some nasty questions about how to

C-extension in Python - return Py_BuildValue() memory leak problem

纵饮孤独 提交于 2019-12-12 07:54:55
问题 I have a huge memory leak problem involving a C-extension I'm developing. In C, I have an array of doubles called A and an int variable called AnotherIntVariable that I want to pass to Python. Well, in my C-extension module I do the following: int i; PyObject *lst = PyList_New(len_A); PyObject *num; if(!lst) return NULL; for(i=0;i<len_A;i++){ num=PyFloat_FromDouble(A[i]); if(!num){ Py_DECREF(lst); return NuLL; } PyList_SET_ITEM(lst,i,num); } free(A); return Py_BuildValue("Oi",lst

VB.NET: Extension method for pages that uses GetLocalResourceObject

一个人想着一个人 提交于 2019-12-12 04:23:06
问题 In our .aspx pages, we've got lots of this code: <%= CType(GetLocalResourceObject("key"), String)) %> I'd like to add an extension method that I can use in our .aspx views that allows me to do this: <%= GetLocalResourceString("key") %> The code isn't working, though: Imports System.Runtime.CompilerServices Imports System.Web.UI Module Extensions <Extension()> Public Function GetLocalResourceString(ByVal control as TemplateControl, ByVal resourceKey as String) as String Return CType(control

Confused by the example code which adds new instance methods to types

不羁岁月 提交于 2019-12-12 04:22:26
问题 The following code snippet is copied from the official document where it is used to demonstrate how to use the Extensions to add method to existing types. extension Int { func repetitions(task: () -> Void) { for _ in 0..<self { task()//what's going on here? } } } 3.repetitions { //why I don't have to call as `repetitions()` print("Hello!") } Question: The question is not asking how the extinsion extens. I just feel a bit confused about this code, why it looks like so? why use task() inside

Why C# change my color structure to ValueType?

做~自己de王妃 提交于 2019-12-12 03:58:02
问题 I have defined following code in C++ CLI. However when I want to call SetColor the first parameter which is supposed to appear as System.Drawing.Color; appears as ValueType. This also let me to pass any variable into it. No matter if it's Color or not. [System::Runtime::CompilerServices::ExtensionAttribute] public ref class MyExtensions abstract sealed { public: [System::Runtime::CompilerServices::ExtensionAttribute] static System::String^ SetColor(System::String^ in, System::Drawing::Color^