kazinix
kazinix

Reputation: 30103

Create a dynamic property in C#

In razor view engine, you can add property (I'm not sure if they are called properties) to the Page object. How can I create an object that behaves like Page? Here's the sample code in Razor C#:

Page.AProperty = "Hi";
Page.AnotherProperty = 1;

Upvotes: 0

Views: 428

Answers (1)

cuongle
cuongle

Reputation: 75306

You can use ExpandoObject:

dynamic obj = new ExpandoObject();

obj.Pro1 = 1;
obj.Pro2 = "Hi";

Upvotes: 6

Related Questions