How to invoke static method in C#4.0 with dynamic type?
Posted
by Morgan Cheng
on Stack Overflow
See other posts from Stack Overflow
or by Morgan Cheng
Published on 2010-05-13T08:33:26Z
Indexed on
2010/05/13
8:44 UTC
Read the original article
Hit count: 209
In C#4.0, we have dynamic type, but how to invoke static method of dynamic type object?
Below code will generate exception at run time.
class Foo
{
public static int Sum(int x, int y)
{
return x + y;
}
}
class Program
{
static void Main(string[] args)
{
dynamic d = new Foo();
Console.WriteLine(d.Sum(1, 3));
}
}
IMHO, dynamic is invented to bridge C# and other programming language. There is some other language (e.g. Java) allows to invoke static method through object instead of type.
BTW, The introduction of C#4.0 is not so impressive compared to C#3.0.
© Stack Overflow or respective owner