extension-methods

Mixing generic methods and extension methods

情到浓时终转凉″ 提交于 2019-12-07 06:08:45
问题 I created the Class1.GetChild<T>() where T : DependencyObject extension method in lib1.dll assembly. After that, all assemblies that depends on lib1.dll failed to compile with error: The type 'System.Windows.DependencyObject' is defined in an assemebly that is not referenced. You must add a reference to assembly 'WindowsBase' etc... Why dependent assemblies requires WindowsBase even if they don't use GetChild ? . To reproduce (vs2010 .net4): lib1.dll (references WindowsBase ) namespace lib1 {

Why can't VB.Net find an extension method on an interface?

梦想与她 提交于 2019-12-07 05:02:18
问题 I have a C# library that has an extension method, something like: public interface ISomething { ... } public class SomethingA : ISomething { ... } public class SomethingB : ISomething { ... } public static class SomethingExtensions { public static int ExtensionMethod(this ISomething input, string extra) { } } The extension works fine if called from C#, but has an issue if called from an external VB.Net application: Dim something = Me.SomethingManager.GetSomething(key) Dim result = something

How to override functions from String class in C#

一世执手 提交于 2019-12-07 04:23:11
问题 For example, I need to see if a string contains a substring, so I just do: String helloworld = "Hello World"; if(helloworld.Contains("ello"){ //do something } but if I have an array of items String helloworld = "Hello World"; String items = { "He", "el", "lo" }; I needed to create a function inside the String class that would return true if either of the items inside the array is contained in the string, for example. I would like to override the function Contains(string) with Contains

ForEach Extension Method for ListItemCollection

*爱你&永不变心* 提交于 2019-12-07 03:35:55
问题 I implemented an ExtensionMethod which basically works as ForEach-Loop, my Implementation looks like this: public static void ForEach(this ListItemCollection collection, Action<ListItem> act ) { foreach (ListItem item in collection) act(item); } However, I'd like the method to stop looping after the first time a specific condition is met. Here's how I currently use it: ddlProcesses.Items.ForEach(item => item.Selected = item.Value == Request["Process"]?true:false); The Problem with this is

Test for UnObserved Exceptions

半腔热情 提交于 2019-12-07 02:28:42
问题 I have a C# Extension method that can be used with tasks to make sure any exceptions thrown are at the very minimum observed, so as to not crash the hosting process. In .NET4.5, the behavior has changed slightly so this won't happen, however the unobserved exception event is still triggered. My challenge here is writing a test to prove the extension method works. I am using NUnit Test Framework and ReSharper is the test runner. I have tried: var wasUnobservedException = false; TaskScheduler

Extending F# List Module

送分小仙女□ 提交于 2019-12-07 01:41:11
问题 I've been adding a few handy methods to some of the F# modules such as List. type Microsoft.FSharp.Collections.FSharpList<'a> with //' static member iterWhile (f:'a -> bool) (ls:'a list) = let rec iterLoop f ls = match ls with | head :: tail -> if f head then iterLoop f tail | _ -> () iterLoop f ls and i'm wondering if it's possible to add mutation? I know List is immutable so how about adding a mutable method to Ref of type List. Something like this. type Ref<'a when 'a :> Microsoft.FSharp

How can additional data be associated with existing objects using extension methods?

瘦欲@ 提交于 2019-12-07 01:23:10
问题 Since .NET Framework 3.5, developers have been able to add extension methods callable from instances of any object type. Extension properties have not been implemented in C#, however. Unlike extension methods, extension properties would involve storing some extra state information for individual objects. However, even for extension methods, it would be highly useful in some programming scenarios to be able to access after-added/extension information for the objects on which those extension

Why can't I implicitly cast a Delegate with Extension methods?

戏子无情 提交于 2019-12-07 01:02:59
问题 I'm trying to figure out a way to automatically cast something to an Action or Func and the best I can come up with is something like this: [TestFixture] public class ExecutionTest { public void BadMethod() { throw new Exception("Something bad happened"); } [Test] public void TestBadMethod() { // Want this, but it won't work!! // BadMethod.Execute().IgnoreExceptions(); // Ick ((Action)BadMethod).Exec().IgnoreExceptions(); // Still ick ((Action)BadMethod).IgnoreExceptions(); // Do not want

Error message “Operator '.' cannot be applied to operand of type 'lambda expression'” when converting a method to Extension Method?

你说的曾经没有我的故事 提交于 2019-12-07 00:38:11
问题 I have a method which i want to convert to Extension Method public static string GetMemberName<T>(Expression<Func<T>> item) { return ((MemberExpression)item.Body).Member.Name; } and calling it like string str = myclass.GetMemberName(() => new Foo().Bar); so it evaluates to str = "Bar"; // It gives the Member name and not its value Now when i try to convert this to extension method by this public static string GetMemberName<T>(this Expression<Func<T>> item) { return ((MemberExpression)item

Command failed due to signal: Segmentation fault: 11 while emitting IR SIL function

假如想象 提交于 2019-12-06 23:55:14
问题 I want to add closure properties in the extension of UITextView so I define a closure using typealias: typealias TextViewHeightDidChangedClosure = (_ currentTextViewHeight:CGFloat)->Void extension UITextView{ func setTextViewHeightDidChanged(textViewHeightDidChanged:TextViewHeightDidChangedBlock){ objc_setAssociatedObject(self, &TextViewHeightDidChangedBlockKey, textViewHeightDidChanged, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC) } func textViewHeightDidChanged()-