-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
  • 66,413
  • 54
  • 311
  • 354

2 Answers2

0

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

Oded
  • 477,625
  • 97
  • 867
  • 998
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
  • 5,453
  • 9
  • 42
  • 83