static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
static void Main( string [] args) { dynamic dict = new DynamicDictionary(); dict.Foo = "Some Value" ; // Compare to dict["Foo"] = "Some Value"; dict.Bar = 123; // Compare to dict["Bar"] = 123; Console .WriteLine( "Foo: {0}, Bar: {1}" , dict.Foo, dict.Bar); Console .ReadLine(); } * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- public class DynamicDictionary: DynamicObject
- {
- Dictionary < string , object >
- _dictionary = new Dictionary < string , object > ();
- public override bool TrySetMember (SetMemberBinder binder, object value )
- {
- _dictionary [binder.Name] = value ;
- return true ;
- }
- public override bool TryGetMember (GetMemberBinder binder,
- out object result)
- {
- return _dictionary.TryGetValue (binder.Name, out result);
- }
- }
* This source code was highlighted with Source Code Highlighter .
- // store in ViewData
- ViewData [ "Message" ] = "Hello World" ;
- // pull out of view data
- <% = Html.Encode (ViewData [ "Message" ])%>
* This source code was highlighted with Source Code Highlighter .
- public ActionResult Index () {
- Data.Message = "<cool> Welcome to ASP.NET MVC! </ Cool> (encoded)" ;
- Data.Body = "<strong> This is not encoded </ strong>." ;
- return View ();
- }
* This source code was highlighted with Source Code Highlighter .
- < asp: Content ContentPlaceHolderID = "MainContent" runat = "server" >
- < h2 > <% = Data.Message %> </ h2 >
- < p >
- <% = Data._Body %>
- </ p >
- </ asp: Content >
* This source code was highlighted with Source Code Highlighter .
- public class DynamicViewData: DynamicObject {
- public DynamicViewData (ViewDataDictionary viewData) {
- _viewData = viewData;
- }
- private ViewDataDictionary _viewData;
- public override bool TrySetMember (SetMemberBinder binder, object value ) {
- _viewData [binder.Name] = value ;
- return true ;
- }
- public override bool TryGetMember (GetMemberBinder binder,
- out object result) {
- string key = binder.Name;
- bool encoded = true ;
- if (key.StartsWith ( "_" )) {
- key = key.Substring (1);
- encoded = false ;
- }
- result = _viewData.Eval (key);
- if (encoded) {
- result = System.Web.HttpUtility.HtmlEncode (result.ToString ());
- }
- return true ;
- }
- }
* This source code was highlighted with Source Code Highlighter .
- public class DynamicController: Controller {
- public dynamic Data {
- get {
- _viewData = _viewData ?? new DynamicViewData (ViewData);
- return _viewData;
- }
- }
- dynamic _viewData = null ;
- }
* This source code was highlighted with Source Code Highlighter .
- public class DynamicViewPage: ViewPage {
- public dynamic Data {
- get {
- _viewData = _viewData ?? new DynamicViewData (ViewData);
- return _viewData;
- }
- }
- dynamic _viewData = null ;
- }
Source: https://habr.com/ru/post/68152/