We are a Web application development programmer. I think you have a lot of access to JavaScript. In JavaScript, we often use a function that allows you to create an object anytime, anywhere, then you can customize the attributes you need for it, as shown in the following simple example:
Code
<Script type = "text/javascript">
Function getObject (name ){
Var obj = new Object ();
// Customize two attributes
Obj. Name = name;
Obj. Sex = "male ";
Return obj;
}
Function onbtnClick (){
Var retobj = getObject (" Dragon"); // call Custom Attributes
Alert ("Name:" + retobj. Name + "Gender:" + retobj. Sex );
}
</Script>
Use the following code to call
<Input type = "button" id = "btnTest" value = "custom attribute" onclick = "onbtnClick ();"/>
After you click the custom attribute button, the corresponding custom attribute value is obtained as expected.
Once, I was crazy and excited about the convenient usage above. These days I saw that C #4.0 also provided this kind of usage, and I was very excited, I think you have been familiar with it for a long time. Please forgive me for being slow and slow. Today, I will share with you a brief introduction to the keywords dynamic and object ExpendoObject in C #4.0.
My type, I am the master, and the compiler bypasses ···
The biggest feature of dynamic is that its type is determined at runtime, which is also the biggest difference between it and the keyword of the static type. If you use the dynamic keyword to define a variable in your code operations, the compiler will not check the type of the variable during compilation, allow it to be explained during running. In most cases, the dynamic type and object type have the same behavior. However, the compiler will not use the parsing or type check for operations that contain dynamic expression, but will compile the variable to the variable of the type object and package the operation information about it, explain the operation at runtime. Like the following code:
Dynamic objDynamic = objDynamic + 1; // compiled through
Object objObject = objObject + 1; // compilation fails
The reason is that the compiler does not check the dynamic type, which is the biggest difference between them.
Dynamic keywords can be used in many situations, such as declarations as attributes, fields, indexers, parameters, return values, or type constraints. Their usage is similar to other basic types of keywords, for more details, see Dynamic (C # reference)
To implement a function in JavaScript like above, an ExpandoObject object is added in C #4.0, which is located in the Assembly:System. Core (in System. Core. dll) namespace:In System. Dynamic, his usage is similar to that of JavaScript I mentioned above. I am lazy here. I will use an example of msdn: