ViewBag
tipidir dynamic
ancak dahili olarak birSystem.Dynamic.ExpandoObject()
Şöyle ilan edilir:
dynamic ViewBag = new System.Dynamic.ExpandoObject();
bu yüzden yapabilirsiniz:
ViewBag.Foo = "Bar";
Örnek Bir Genişletici Nesne Kodu:
public class ExpanderObject : DynamicObject, IDynamicMetaObjectProvider
{
public Dictionary<string, object> objectDictionary;
public ExpanderObject()
{
objectDictionary = new Dictionary<string, object>();
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
object val;
if (objectDictionary.TryGetValue(binder.Name, out val))
{
result = val;
return true;
}
result = null;
return false;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
try
{
objectDictionary[binder.Name] = value;
return true;
}
catch (Exception ex)
{
return false;
}
}
}