extension-methods

Can't await async extension method

时光总嘲笑我的痴心妄想 提交于 2019-11-28 00:19:29
Situation is pretty simple - I wrote an extension method and made it async with return type Task<T> . But when I try to call it using await, compiler throws an error which suggests that the extension method wasn't recognized as async at all. Here's my code: public static async Task<NodeReference<T>> CreateWithLabel<T>(this GraphClient client, T source, String label) where T: class { var node = client.Create(source); var url = string.Format(ConfigurationManager.AppSettings[configKey] + "/node/{0}/labels", node.Id); var serializedLabel = string.Empty; using (var tempStream = new MemoryStream())

Generic extension method resolution fails

独自空忆成欢 提交于 2019-11-28 00:19:18
The following program does not compile, because in the line with the error, the compiler chooses the method with a single T parameter as the resolution, which fails because the List<T> does not fit the generic constraints of a single T . The compiler does not recognize that there is another method that could be used. If I remove the single- T method, the compiler will correctly find the method for many objects. I've read two blog posts about generic method resolution, one from JonSkeet here and another from Eric Lippert here , but I could not find an explanation or a way to solve my problem.

Using ms: xpath functions inside XPathExpression

你说的曾经没有我的故事 提交于 2019-11-28 00:16:45
问题 I am trying to use Microsoft XPath Extension Functions (such as ms:string-compare http://msdn.microsoft.com/en-us/library/ms256114.aspx) inside an XPathExpression object. These functions are extensions inside the MSXML library, and if I use them in an XslCompiledTransform (simply adding the "ms" namespace) they work like a charm: var xsl = @" <?xml version=""1.0"" encoding=""UTF-8""?> <xsl:stylesheet version=""2.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" xmlns:xs=""http://www.w3

Checking for nil Value in Swift Dictionary Extension

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:13:45
I have the following code in a playground file: extension Dictionary { func test() { for key in self.keys { self[key] } } } var dict: [String: AnyObject?] = [ "test": nil ] dict.test() I will henceforth refer to the line within the for-each loop as the output since it is what's relevant. In this particular instance the output is nil . When I change the for-each loop to look like this: for key in self.keys { print(self[key]) } The output is "Optional(nil)\n" . What I really want to do is check the value for nil, but the code: for key in self.keys { self[key] == nil } outputs false . One other

Can I add an implicit conversion for two classes which I don't directly control?

人走茶凉 提交于 2019-11-27 23:20:38
问题 I'd like to be able to implicitly convert between two classes which are otherwise incompatible. One of the classes is Microsoft.Xna.Framework.Vector3 , and the other is just a Vector class used in an F# project. I'm writing a 3d game in C# with XNA, and -- although it's drawn in 3D, the gameplay takes place in only two dimensions (it's a birds-eye-view). The F# class takes care of the physics, using a 2D vector: type Vector<'t when 't :> SuperUnit<'t>> = | Cartesian of 't * 't | Polar of 't *

Do Extension Methods Hide Dependencies?

删除回忆录丶 提交于 2019-11-27 23:19:18
问题 All, Wanted to get a few thoughts on this. Lately I am becoming more and more of a subscriber of "purist" DI/IOC principles when designing/developing. Part of this (a big part) involves making sure there is little coupling between my classes, and that their dependencies are resolved via the constructor (there are certainly other ways of managing this, but you get the idea). My basic premise is that extension methods violate the principles of DI/IOC. I created the following extension method

Is extending String class with IsNullOrEmpty confusing?

亡梦爱人 提交于 2019-11-27 22:19:48
Everyone knows and love String.IsNullOrEmpty(yourString) method. I was wondering if it's going to confuse developers or make code better if we extend String class to have method like this: yourString.IsNullOrEmpty(); Pro: More readable. Less typing. Cons: Can be confusing because yourString variable can be null and it looks like you're executing method on a null variable. What do you think? The same question we can ask about myObject.IsNull() method. That how I would write it: public static class StringExt { public static bool IsNullOrEmpty(this string text) { return string.IsNullOrEmpty(text)

LINQ .Cast() extension method fails but (type)object works

走远了吗. 提交于 2019-11-27 20:55:56
问题 To convert between some LINQ to SQL objects and DTOs we have created explicit cast operators on the DTOs. That way we can do the following: DTOType MyDTO = (LinqToSQLType)MyLinq2SQLObj; This works well. However when you try to cast using the LINQ .Cast() extension method it trows an invalid cast exception saying cannot cast type Linq2SQLType to type DTOType. i.e. the below does not work List<DTO.Name> Names = dbContact.tNames.Cast<DTO.Name>() .ToList(); But the below works fine: DAL.tName

Is there a workaround for generic type constraint of “special class” Enum in C# 3.0? [duplicate]

霸气de小男生 提交于 2019-11-27 20:34:57
问题 This question already has answers here : Anyone know a good workaround for the lack of an enum generic constraint? (12 answers) Closed 6 years ago . Update: See the bottom of this question for a C# workaround. Hi there, Consider the following extension method: public static bool HasFlags<T>(this T value, T flags) where T : System.Enum { // ... } This will, as you may know, throw an error at compile-time, since a class is not normally allowed to inherit from System.Enum . The problem is that

Why does this extension method throw a NullReferenceException in VB.NET?

允我心安 提交于 2019-11-27 20:30:56
From previous experience I had been under the impression that it's perfectly legal (though perhaps not advisable) to call extension methods on a null instance. So in C#, this code compiles and runs: // code in static class static bool IsNull(this object obj) { return obj == null; } // code elsewhere object x = null; bool exists = !x.IsNull(); However, I was just putting together a little suite of example code for the other members of my development team (we just upgraded to .NET 3.5 and I've been assigned the task of getting the team up to speed on some of the new features available to us),