ViewBag
adalah tipe dynamic
tetapi, secara internal sebuahSystem.Dynamic.ExpandoObject()
Ini dinyatakan seperti ini:
dynamic ViewBag = new System.Dynamic.ExpandoObject();
itulah mengapa Anda bisa melakukan:
ViewBag.Foo = "Bar";
Contoh Kode Objek Expander:
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;
}
}
}