You are currently browsing the category archive for the ‘Dynamic Languages’ category.
I need to pass a model to an ASP.NET MVC view, declaring it as ‘dynamic’. The model is populated (via controller) by another class in another assembly. I use ‘dynamic’ keyword, because it is an anonymous type (in general). Problem: anonymous types are INTERNAL (read it on http://blogs.msdn.com/b/davidebb/archive/2009/12/18/passing-anonymous-objects-to-mvc-views-and-accessing-them-using-dynamic.aspx) and views in ASP.NET MVC are dynamically compiled into another assembly.
So in general when you pass an instance of an anonymous type in an ASP.NET MVC view, you cannot see it and you have an object instead and you don’t access you members.
The solution? Use ExpandoObject. Instead of:
1: dynamic obj = new
2: {
3: ID = item.ID
4: ,
5: Text = text
6: ,
7: Type = item.Type
8: ,
9: Path = pathOf + "/" + text
10: };
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
use
1: dynamic obj = new ExpandoObject();
2: obj.ID = item.ID;
3: obj.Text = text;
4: obj.Type = item.Type;
5: obj.Path = pathOf + "/" + text;
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
ExpandoObject is a public type that comes from DLR (like CallSites that allow us to use dynamic keywork in C#). It has the ability to add members at runtime, that are then accessible via dynamic keywork.
Enjoy!
Un post in italiano, dedicato all’evento UGI ALT.net di oggi, cui ho avuto il piacere di partecipare come relatore parlando di nuovo di Dynamic Languages.
E’ stata la mia prima conferenza milanese, a livello italiano, dopo l’esperienza di xe.net (comunque aperta a tutti) ma più orientata al Triveneto.
Beh, è stata una splendida esperienza, anche perchè è stata un’evoluzione della presentazione precedente e quindi ho potuto mettere in evidenza aspetti che nella prima non ero riuscito a mostrare…se non fosse che il portatile avesse deciso che fosse il momento di fare le bizze. In pratica l’immagine dello schermo del portatile, connesso al proiettore, rimbalzava regolarmente tra il portatile e il proiettore con frequenti “blank”. Non so cosa sia successo. Sono in treno e il portatile non dà alcun segno di problema. Quando, a casa, proverò a collegarlo al monitor, saprò la verità. E’ vero che devo riformattare il PC con Windows 7: spero quanto prima di avere il tempo di farlo! Per adesso chiedo scusa ai numerosi partecipanti che ringrazio di essere stati pazienti e presenti così numerosi.
Approfitto per pubblicare intanto le slides dell’evento (che forza pubblicare un file in Internet a 200Km/h in treno!). Per il codice, mi prometto in un paio di settimane, di pubblicare, pezzo dopo pezzo, i vari esempi, anche perchè ci sono pezzettini cui tengo commentare singolarmente.
Non sono un gran blogger, né lo sarò mai, però spero di scrivere più spesso. Intanto volevo condividere il fatto che oramai sto perseguendo il percorso della programmabilità delle applicazioni, valutando soluzioni tra DLR, Dynamic Languages e DSL. Farò inoltre qualche speech su SQL Server Modeling, qualunque cosa essa sia. Per esempio su xe.net, a maggio, dovrei fare un altro Virtual Meeting online, se vi interessa.
Per intanto grazie a tutti e un saluto a chi è rimasto a Milano per andare a mangiare!
Io ho i bimbi e mia moglie che mi aspettano.
Marco
