动态 - 是引用类型还是值类型?

时间:2022-03-02 16:13:11

I assume it is of Reference Type as ... you can instantiate stuff like this:

我假设它是引用类型为...你可以实例化这样的东西:

dynamic d1 = new SomeClass(); // SomeClass extends from System.Dynamic.DynamicObject
dynamic d2 = new ExpandoObject() ;
d2.x = 100;
d2.y = "120";

2 个解决方案

#1


4  

dynamic seems to be a reference type, as detailed in MSDN here:

动态似乎是一个引用类型,详见MSDN:

http://msdn.microsoft.com/en-us/library/490f96s2.aspx

http://msdn.microsoft.com/en-us/library/490f96s2.aspx

#2


0  

dynamic is Reference Type

this is what i get From C# in Depth

这就是我从深度中获得的C#

Let’s suppose our products aren’t stored in a database, or in XML, or in memory. They’re accessible via a web service of sorts, but you only have Python code to access it—and that code uses the dynamic nature of Python to build results without declaring a type with all the properties you need to access. Instead, it’ll let you ask for any property, and try to work out what you mean at execution time.

假设我们的产品不存储在数据库,XML或内存中。它们可以通过各种Web服务访问,但您只能使用Python代码来访问它 - 并且该代码使用Python的动态特性来构建结果,而无需声明具有您需要访问的所有属性的类型。相反,它会让你要求任何财产,并尝试找出你在执行时的意思。

The answer comes in the form of dynamic—a new type,4 which the C# compiler allows you to use dynamically. If an expression is of type dynamic, you can call methods on it, access properties, pass it around as a method argument, and so on—and most of the normal binding process happens at execution time instead of compile time. You can implicitly convert a value from dynamic to any other type.

答案来自动态的形式 - 一种新的类型,4 C#编译器允许您动态使用它。如果表达式是动态类型,则可以在其上调用方法,访问属性,将其作为方法参数传递,等等 - 并且大多数正常绑定过程在执行时而不是编译时发生。您可以隐式地将值从动态转换为任何其他类型。

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.ExecuteFile("FindProducts.py");
dynamic products = scope.GetVariable("products");
foreach (dynamic product in products)
{
Console.WriteLine("{0}: {1}", product.ProductName, product.Price);
}

Both products and product are declared to be dynamic, so the compiler is happy to let us iterate over the list of products and print out the properties, even though it doesn’t know whether it’ll work. If we’d made a typo, using product.Name instead of product.ProductName, for example, that would only show up at execution time. This is completely contrary to the rest of C#, which is statically typed. But dynamic typing only comes into play when expressions with a type of dynamic are involved: most C# code is likely to remain statically typed throughout.

产品和产品都被声明为动态的,因此编译器很乐意让我们遍历产品列表并打印出属性,即使它不知道它是否有效。如果我们输入了拼写错误,例如使用product.Name而不是product.ProductName,它只会在执行时显示。这与静态输入的C#的其余部分完全相反。但是,当涉及具有动态类型的表达式时,动态类型才会发挥作用:大多数C#代码可能始终保持静态类型。

#1


4  

dynamic seems to be a reference type, as detailed in MSDN here:

动态似乎是一个引用类型,详见MSDN:

http://msdn.microsoft.com/en-us/library/490f96s2.aspx

http://msdn.microsoft.com/en-us/library/490f96s2.aspx

#2


0  

dynamic is Reference Type

this is what i get From C# in Depth

这就是我从深度中获得的C#

Let’s suppose our products aren’t stored in a database, or in XML, or in memory. They’re accessible via a web service of sorts, but you only have Python code to access it—and that code uses the dynamic nature of Python to build results without declaring a type with all the properties you need to access. Instead, it’ll let you ask for any property, and try to work out what you mean at execution time.

假设我们的产品不存储在数据库,XML或内存中。它们可以通过各种Web服务访问,但您只能使用Python代码来访问它 - 并且该代码使用Python的动态特性来构建结果,而无需声明具有您需要访问的所有属性的类型。相反,它会让你要求任何财产,并尝试找出你在执行时的意思。

The answer comes in the form of dynamic—a new type,4 which the C# compiler allows you to use dynamically. If an expression is of type dynamic, you can call methods on it, access properties, pass it around as a method argument, and so on—and most of the normal binding process happens at execution time instead of compile time. You can implicitly convert a value from dynamic to any other type.

答案来自动态的形式 - 一种新的类型,4 C#编译器允许您动态使用它。如果表达式是动态类型,则可以在其上调用方法,访问属性,将其作为方法参数传递,等等 - 并且大多数正常绑定过程在执行时而不是编译时发生。您可以隐式地将值从动态转换为任何其他类型。

ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.ExecuteFile("FindProducts.py");
dynamic products = scope.GetVariable("products");
foreach (dynamic product in products)
{
Console.WriteLine("{0}: {1}", product.ProductName, product.Price);
}

Both products and product are declared to be dynamic, so the compiler is happy to let us iterate over the list of products and print out the properties, even though it doesn’t know whether it’ll work. If we’d made a typo, using product.Name instead of product.ProductName, for example, that would only show up at execution time. This is completely contrary to the rest of C#, which is statically typed. But dynamic typing only comes into play when expressions with a type of dynamic are involved: most C# code is likely to remain statically typed throughout.

产品和产品都被声明为动态的,因此编译器很乐意让我们遍历产品列表并打印出属性,即使它不知道它是否有效。如果我们输入了拼写错误,例如使用product.Name而不是product.ProductName,它只会在执行时显示。这与静态输入的C#的其余部分完全相反。但是,当涉及具有动态类型的表达式时,动态类型才会发挥作用:大多数C#代码可能始终保持静态类型。