If you write a program in MVC, you should know that ViewBag is a data transfer tool for the front and back, so are you wondering about the usage of viewbag?
viewbag.mode1l=Newobject(); Viewbag.model2=newobject(); Viewbag.model3=newobject();
We know that when using object properties it is necessary to declare (that is, this attribute is already defined in the class of the object) to use (at this point the auto hint of VS will pop up all the properties of this class), but you will find Viewbab when using ViewBag . No properties will pop up after that (unless you've already added a property), and we can write a property on our own, but it works correctly. This attribute is attributed to the dynamic.
1. Before you talk about dynamic, go back to the var category.
Starting with Visual C # 3.0, variables declared in the method scope can have implicitly-typed var. An implicitly typed local variable is a strongly typed variable (as if you have declared the type), but the type is determined by the compiler. The following two I declarations are functionally equivalent:
var Ten // implicitly typed (implicit declaration)int//explicitly typed (show statement)
The type of Var is determined by the compiler (that is, at compile time, the compiler determines the type of the variable based on the value of the variable or the type of object referenced), but note that once the type of the variable is determined, it cannot be changed.
var 1; // i is an int, which is equivalent to int i = 1; 1.0; // Error, 1.0 for double type
Note that var variables must be initialized at the time of Declaration, as follows:
var 1; // correct var 1; // Error
2.var Usage Examples:
//allow, but do not need to use Var, because the type of query results can be declared as ienumerable<string>string[] Words = {"Apple","Strawberry","Grape","Peach","Banana" };varWordquery = fromWordinchwordswhereword[0] =='g' SelectWord;//var is not required because the element type is a string type and is not an anonymous typeforeach(stringSinchwordquery) {Console.WriteLine (s);}//var must be used because the result was a collection of anonymous types, and the name of that type was not accessible ex Cept to the compiler itself. " var must be used in an > expression because the result is a collection of anonymous types, and the name of the type is only accessible to the compiler itselfvarCustquery = fromCustinchCustomerswhereCust. City = ="Phoenix" Select New{Cust. Name, Cust. Phone};//foreach iteration variable item must also be implicitly typed. " The >foreach iteration variable item must also be converted to an implicit type because Custquery is an anonymous collectionforeach(varIteminchcustquery) {Console.WriteLine ("name={0}, Phone={1}", item. Name, item. Phone);}
3.dynamic
In operations that are implemented by the dynamic type, the role of this type is to bypass compile-time type checking and resolve these operations at run time instead. The dynamic type simplifies access to COM APIs such as the Office Automation API, dynamic APIs (such as the IronPython Library), and the HTML Document Object Model (DOM).
In most cases,Dynamic type andThe behavior of type object is the same.However, you will not use the compiler to includedynamic the operation of the type expression to parse or type-check. dynamic is compiled into variables of type dynamic is compiled into a variable of type object. dynamic exists only at compile time, not at run time. " As a result, the type dynamic exists only at compile time and does not exist at run time.
classprogram{Static voidMain (string[] args) { DynamicDyn =1; Objectobj =1; Dyn= dyn +3;//compilation can pass, and dynamic can bypass the compilerobj = obj +3;//error, compilation cannot be passedSystem.Console.WriteLine (dyn. GetType ()); System.Console.WriteLine (obj. GetType ()); }}//Output ResultsSystem.Int32System.Int32
Some common uses of 4.dynamic
(1) in a declaration, a type that is a property, field, indexer, parameter, return value, or type constraint.
classexampleclass{//Field Static Dynamicfield; //Properties DynamicProp {Get;Set; } //return values and Parameters Public DynamicExamplemethod (Dynamicd) {DynamicLocal ="Local Variable"; intboth =2; if(d is int) { returnLocal; } Else { returnboth ; } }}
(2) in an explicit type conversion, as the target type of the transformation.
Static voidconverttodynamic () {DynamicD; inti = -; D= (Dynamic) I; Console.WriteLine (d); strings ="Example string."; D= (Dynamic) s; Console.WriteLine (d); DateTime DT=Datetime.today; D= (Dynamic) DT; Console.WriteLine (d);}//Results:// -//Example string.//2/17/2009 9:12:00 AM
(3) In any context where the type acts as a value (such as the is operator or as operator to the right) or as a parameter of typeof as part of a constructed type.
int 8 ; Dynamic D; if is Dynamic asdynamic; Console.WriteLine (typeof(list<dynamic>)); // Console.WriteLine (typeof (Dynamic)); // compilation Error
5.dynamic and Viewbab
Viewbab is a dynamic type property, so the compiler does not check it, so we can customize the properties. We can also implement our own viewbab through the ExpandoObject () class, as shown in the following example:
Public classprogram{ Public Static voidMain (string[] args) { DynamicModel =NewExpandoObject ();//at run time, model is converted to an instance of ExpandoObject ()Model. Index =0;//ExpandoObject () has an event propertychanged that dynamically triggers this event at runtime,Model. Number =0;//then add the two attributes of index and number to the classConsole.WriteLine (model. Index); Console.WriteLine (model. number); Console.readkey (); }}
You may feel that the dynamic doesn't use much, so you don't have to understand it, but when you actually meet it, you're going to have a big loss. As stated above, it plays an important role in the operation of the COM API, the dynamic API, the HTML object model, which simplifies the operation, but at the same time it can be confusing (what type of object it points to), because simplification usually implies hiding, It encapsulates the complex background implementation and opens up a simple interface for us to use, which simplifies our operation, but we are more likely to confuse it, and we don't know why it does this, only knowing that we can do it, so that we can't dig deep into many issues. In web development, in-depth understanding of dynamic, conducive to our deep into the framework to learn from us. NET architecture is extremely helpful.
Original link: https://www.cnblogs.com/SilentCode/p/4920598.html
The Var and dynamic of MVC