extension-methods

why allow extension methods on null objects?

…衆ロ難τιáo~ 提交于 2019-12-04 23:01:58
what is the point of allowing invocation of extension methods on null objects? this is making me unnecessarily check for a null object in the extension method. AFAIK,i can't understand this? Please explain. Extension methods are syntactic sugar of the C# language, they get compiled to normal static method calls in ILCode. A static method doesn't know anything about the parameters at compile time. Simply put, why not? You can sometimes skip the test if the first method you call within the extension would also throw the correct error. You're essentially asking for the code to be different so

Lambdas within Extension methods: Possible memory leak?

我是研究僧i 提交于 2019-12-04 22:51:10
问题 I just gave an answer to a quite simple question by using an extension method. But after writing it down i remembered that you can't unsubscribe a lambda from an event handler. So far no big problem. But how does all this behave within an extension method?? Below is my code snipped again. So can anyone enlighten me, if this will lead to myriads of timers hanging around in memory if you call this extension method multiple times? I would say no, cause the scope of the timer is limited within

Convert to IEnumerable<dynamic>?

Deadly 提交于 2019-12-04 18:36:00
问题 I wrote this extension method : public static class A { public static IEnumerable<dynamic> AsDynamic<T>(this IEnumerable<T> f) { foreach (var element in f) { yield return (dynamic) element; } } } And tested it : List<int> l = new List<int>(){1,2,3}; Console.WriteLine ( l.AsDynamic().GetType()); However the output is : typeof (IEnumerable<Object>) Why it is not typeof (IEnumerable<dynamic>) ? How can I make it to be like it ? 回答1: I think that you have a misunderstanding of what dynamic means.

How to call extension methods using Eval in a databound control

*爱你&永不变心* 提交于 2019-12-04 18:29:46
问题 I have a simple extension method on the int type so I can do the following: string timeLength = 61.ToTime() // timeLength will be "1:01" This works great in code, but I want to use this extension method in a Repeater Template. When databinding I want to do the following: <%# Eval("LengthInSeconds").ToTime() %> That didn't work so I tried: <%# ((int) Eval("LengthInSeconds")).ToTime() %> and it still didn't work. The JIT compiler is not seeing my extension method and I do have the proper import

How can I call C# extension methods in VB code

独自空忆成欢 提交于 2019-12-04 18:23:56
问题 I have a class library with some extension methods written in C# and an old website written in VB. I want to call my extension methods from the VB code but they don't appear in intelisense and I get compile errors when I visit the site. I have got all the required Import s because other classes contained in the same namespaces are appearing fine in Intelisense. Any suggestions EDIT: More info to help with some comments. my implementation looks like this //C# code compiled as DLL namespace x.y

Get attribute values from matching XML nodes using XPath query

最后都变了- 提交于 2019-12-04 18:04:07
问题 This doesn't seem like it should be difficult, but I'm stuck currently. I'm trying to get the attribute values for a particular attribute from nodes that match a given XPath query string. Here's what I have so far: public static IEnumerable<string> GetAttributes(this XmlDocument xml, string xpathQuery, string attributeName) { var doc = new XPathDocument(new XmlNodeReader(xml)); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr = nav.Compile(xpathQuery); XPathNodeIterator

C# generic method resolution fails with an ambiguous call error

送分小仙女□ 提交于 2019-12-04 18:03:46
问题 Suppose I have defined two unrelated types and two extension methods with the same signature but different type filters: public class Foo {} public class Bar {} public static class FooExtensions { public static TFoo Frob<TFoo>(this TFoo foo) where TFoo : Foo { } public static TFoo Brob<TFoo>(this TFoo foo) where TFoo : Foo { } } public static class BarExtensions { public static TBar Frob<TBar>(this TBar bar) where TBar : Bar { } } Then when I write new Foo().Frob(); I get an error error

Extending typed Arrays (of primitive types like Bool) in Swift 3?

天大地大妈咪最大 提交于 2019-12-04 17:37:46
问题 Previously in Swift 2.2 I'm able to do: extension _ArrayType where Generator.Element == Bool{ var allTrue : Bool{ return !self.contains(false) } } which extends [Bool] with .allTrue . E.g. [true, true, false].allTrue == false But in Swift 3.0 I'm getting this error: undeclared type _ArrayType So I tried switching it to Array and using the new keyword Iterator extension Array where Iterator.Element == Bool var allTrue : Bool{ return !self.contains(false) } } But I got a different error

How do extension methods work under-the-hood?

假装没事ソ 提交于 2019-12-04 16:58:26
问题 A contractor where I work is using extension methods to implement CRUD on well-known internal classes that we own . I say it is better to use normal inheritance over extension methods for the following reasons. Using extension methods obfuscates, hides & confuses the source of the CRUD methods. I assume extension methods make heavy use of reflection (which is slower). His logic is, "It's compiled, so it's fast." Maybe I'm wrong...but just because it is compiled doesn't mean it doesn't use

Translate a List<TypeA> to List<TypeB>

人盡茶涼 提交于 2019-12-04 16:54:32
Understood the concept of translate . Used it in converting a DataModel Type to DTO type for presentation layer like this and worked fine. objTypeB = objTypeA.TranslateTo<clsTypeB>(); Discrepancy between TypeA and TypeB was just the datatype of few properties and I converted them in the Property Set method. But in the above implementation if the source is List<TypeA> , I have loop through each to translate to TypeB and add it another List<TypeB> instance. Is it possible to do something like this instead: Assume resultListA is a List<clsTypeA> var resultListB = resultListA.TranslateTo<List