public class ViewBag : DynamicObject {
        private readonly  Dictionary<string,dynamic> dic=new  Dictionary<string,dynamic>(); 
 
        public ViewBag() {
             
        }
        private Dictionary<string,dynamic> ViewData {
            get { 
                return dic;
            }
        } 
        public override IEnumerable<string> GetDynamicMemberNames() { 
            return ViewData.Keys;
        } 
 
        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            result = ViewData[binder.Name]; 
            return true;
        }
 
        public override bool TrySetMember(SetMemberBinder binder, object value) {
            ViewData[binder.Name] = value; 
            return true;
        } 
    }

调用:

 dynamic viewBag = new ViewBag();
viewBag.Title="xxx";
Console.WriteLine(viewBag.Title);

 

相关文章:

  • 2021-11-16
  • 2022-12-23
  • 2022-12-23
  • 2021-08-19
  • 2022-01-24
  • 2021-07-09
  • 2021-09-24
  • 2022-01-13
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2021-11-30
  • 2022-03-03
  • 2021-10-07
相关资源
相似解决方案