Will the Dynamic Keyword in C#4 Support Extension Methods

The dynamic binding used instead of the extension method for strictly typed value in C#

Now, I think I understand why it works this way. The compiler produces the code that will use the best overload for this function in runtime based on dynamic parameter true type. Since the return type can vary for different overloads it can't be determined at the compile-time, so the following sections (method calls, extension method calls, property access, etc.) can't rely on any strict type and have to assume dynamic.

using System;

namespace ExtensionMethodsAndDynamic
{
class Program
{
static int GetIntFrom(object anything) => 1;
static int GetIntFrom(int anything) => 2;
static int GetIntFrom(string anything) => 3;
static string GetIntFrom(object[] anything) => "4";

static void Main()
{
Console.WriteLine(GetIntFrom((dynamic)(new object()))); // 1
Console.WriteLine(GetIntFrom((dynamic)1)); // 2
Console.WriteLine(GetIntFrom((dynamic)"1")); // 3
Console.WriteLine(GetIntFrom((dynamic)(new object[0]))); // 4
}
}
}

using Linq with dynamic c# objects

Because the Count method is an extension method on IEnumerable<T> (Once you call Where, you don't have a list anymore, but an IEnumerable<T>). Extension methods don't work with dynamic types (at least in C#4.0).

Dynamic lookup will not be able to find extension methods. Whether extension methods apply or not depends on the static context of the call (i.e. which using clauses occur), and this context information is not currently kept as part of the payload.

Will the dynamic keyword in C#4 support extension methods?

dynamic keyword problem

Dynamic keyword was introduced as part of C# 4.0 language - the compiler comes with VS 2010. Its a language feature and does not need runtime support (AFAIK) hence once complied with C# 4.0 compiler, shouldn't have any issue with earlier version of runtime. Changing target framework in VS 2010 does not switch the compiler (which remains at 4.0) - we will receive compiler error only if you use feature that targets new library or runtime. For example, in VS 2008, you could use lambda expressions or var keyword for target runtime 2.0 but extension methods were not available because extension attribute was part of 3.5 assembly.

EDIT: Above is wrong - dynamic keyword requires Framework 4.0. I couldn't even compile in VS2010 when target fx was changed to 3.5. I believe that OP might not have used the dynamic var later in the code so that compiler optimization would have removed it making OP believe that its working.

Creating a dynamic extension method in C#?

No. See https://stackoverflow.com/a/5311527/613130

When you use a dynamic object, you can't call an extension method through the "extension method syntax". To make it clear:

int[] arr = new int[5];
int first1 = arr.First(); // extension method syntax, OK
int first2 = Enumerable.First(arr); // plain syntax, OK

Both of these are ok, but with dynamic

dynamic arr = new int[5];
int first1 = arr.First(); // BOOM!
int first2 = Enumerable.First(arr); // plain syntax, OK

This is logical if you know how dynamic objects work. A dynamic variable/field/... is just an object variable/field/... (plus an attribute) that the C# compiler knows that should be treated as dynamic. And what does "treating as dynamic" means? It means that generated code, instead of using directly the variable, uses reflection to search for required methods/properties/... inside the type of the object (so in this case, inside the int[] type). Clearly reflection can't go around all the loaded assemblies to look for extension methods that could be anywhere.

How to combine extension method's this with params keyword?

Consider to give the class a useful, somewhat specific name to get a nice syntax like this:

KeyPressed.Any(Key.LeftShift, Key.RightShift);

Is implemented by

public static class KeyPressed
{
public static bool Any(params System.Windows.Input.Key[] keys)
{
....
}
}

Well, this doesn't really answer your question, but could still be a useful solution.

How to access count property of a dynamic type in C# 4.0?

The IEnumerable<T> that's returned from that method doesn't have a Count property, so I don't know what you're talking about. Maybe you forgot to write ToList() on the end to reify it into a list, or you meant to call the Count() method on IEnumerable<T>?



Related Topics



Leave a reply



Submit