有没有一种方法来测试

有没有一种方法来测试

本文介绍了有没有一种方法来测试,如果一个变量是动态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面这段代码会始终返回true,除非变量 v

The following piece of code will always return true unless the variable v is null:

v is dynamic

与下面的测试将不能编译(typeof运算符不能在动态类型中使用):

and the following test will not compile ("The typeof operator cannot be used on the dynamic type"):

v.GetType() == typeof(dynamic)

那么,有没有办法测试如果变量动态

So is there a way to test if the variable is dynamic?

推荐答案

首先,你需要给变量和对象分开。变量是动态如果它被定义为动态 。就这些。没有什么更多。 A 字段的或的属性的将与 [动态] 属性进行批注,即

Firstly, you need to separate the variable and the object. A variable is dynamic if it is defined as dynamic. That is all. There is nothing more. A field or property would be annotated with the [Dynamic] attribute, i.e.

public dynamic Foo {get;set;}

居然是:

[Dynamic]
public object Foo {get;set;}

这基本上是作为一个提示编译器通过动态 API,而不是通过面向对象的API。

This basically acts as a prompt for the compiler to access the object via the dynamic API rather than via the OOP API.

这是对象的支持全动态功能是否实现 IDynamicMetaObjectProvider - 然而,这样一个对象可以通过两种动态 API,并通过访问常规的面向对象的API(它可以兼得)。通过动态 IDynamicMetaObjectProvider 可通过两种API访问(但C $ C>)。

An object supports full dynamic capabilities if it implements IDynamicMetaObjectProvider - however, such an object can be accessed via both the dynamic API and via the regular OOP API (it can have both). Equally, an object that doesn't implement IDynamicMetaObjectProvider can be accessed via either API (but: only the public members will be available via dynamic).

这篇关于有没有一种方法来测试,如果一个变量是动态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:46