Antonio Esposito's


Prodotti tipici .NETtiani

dynamic keyword & late-binding

Ciao a tutti

in c# 4 è stata aggiunta la keyword dynamic per poter finalmente utilizzare il late-binding tanto utile con i tipi anonimi spesso usati con LINQ.
es:

static void Main(string[] args)
{
  test(new {Value="pippo"}); //tipo anonimo
}

static void test(dynamic p)
{
 Console.WriteLine(p.Value); //questo è il late-binding
}

La keyword dynamic in pratica ci permette di accedere a dei metodi di classe a runtime.
In VB questa keyword non esiste perchè è sempre possibile utilizzare il late binding sui tipi Object
es:

shared sub Main(args as string())
  test(new with { .Value="pippo"})
end sub

shared sub test(object o)
  Console.WriteLine(o.Value); //late-binding
end sub

Categoria: Tips
sabato, 13 feb 2010 Ore. 15.05
Statistiche
  • Views Home Page: 10.693
  • Views Posts: 58.915
  • Views Gallerie: 0
  • n° Posts: 44
  • n° Commenti: 16
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003