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

4

44

I'm listening to a talk about C#4's dynamic keyword and I'm wondering... Will this feature be orthogonal to other .NET features, for example will it support extension methods?

public static class StrExtension {
    public static string twice(this string str) { return str + str; }
}
...
dynamic x = "Yo";
x.twice(); // will this work?

Note: This question was asked before C#4 was shipped which is why it's phrased in the future tense.

Thousandth answered 3/11, 2008 at 15:28 Comment(3)
Awesome question. My Guess is "No" since they aren't really part of the class, and aren't available via reflection.Derringdo
I'd also guess "no". But I think this would work: var x = "Y0"; x.twice();Neckwear
Joel, this is allready working. Changing it would be a breaking changeUnreflective
E
48

From the "New Features in C# 4" word doc:

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.

Elamitic answered 3/11, 2008 at 15:33 Comment(3)
Well, that was weird... This suddenly went from three answers to one. I guess as I deleted my wrong answer, the other guy deleted his "not quite as correct answer"....Amoreta
jon , in a short sentence ( just for understanding ) what is the exact reason where dynamic doesnt supports extension method ?Corrugation
@RoyiNamir The magic of extension methods appears to be a compile time construct. At runtime C# isn't carrying around the state for every extension method it can find in the namespace this is a performance concern.Novosibirsk
S
4

This works which I find interesting at least...

public static class StrExtension
{
   public static string twice(this string str) { return str + str; }
}

...
dynamic x = "Yo";
StrExtension.twice(x);

Still, if the compiler can find the correct extension method at compile time then I don't see why it can't package up a set of extension methods to be looked up at runtime? It would be like a v-table for non-member methods.

EDIT:

This is cool... http://www2.research.att.com/~bs/multimethods.pdf

Surface answered 30/11, 2009 at 22:39 Comment(2)
You are calling a static class and giving it a string... That should always work :)Palindrome
This actually doesn't work when the string you pass is a dynamic property on a POCO objectAmygdalin
U
2

It can't work, Extension methods work depending on having the namespace included in the file and, as far as I know, MSIL has no idea about files and including namespaces.

Unreflective answered 20/11, 2008 at 17:53 Comment(0)
V
1

You can create an extension method for object and assign it to a dynamic:

public static void MyExt(this object o) {
    dynamic d = o;
    d.myProp = "foo";
}

and call it like this:

ClassWithMyProp x;
x.MyExt();
Volute answered 1/2, 2013 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.