-3

Is it possible to create property in a class via code at runtime (Like ViewBag of MVC3) in C#?

I want to create property of custom type in a class as we create property in ViewBag of MVC3.

Thanks.

nawfal
  • 70,104
  • 56
  • 326
  • 368

2 Answers2

0

Use a Dictionary - this will allow you to add as many "properties" as you wish.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

The dynamic type in .NET 4 will allow you to create properties at runtime; I think ViewBag is actually an instance of the dynamic type.

By the way, I think what you're looking for is creating properties at runtime - creating properties at compile time would mean adding a getter and a setter to your code as you would normally do!

ekolis
  • 6,270
  • 12
  • 50
  • 101