cnblogs/dcrenl/C# dynamic类型.html

33 lines
3.7 KiB
HTML
Raw Normal View History

2024-09-24 12:43:01 +08:00
<p>dynamic关键字可充当C#类型系统中的静态类型声明。这样C#就获得了动态功能,同时仍然作为静态类型化语言而存在。</p>
<p><strong>Var与<strong>dynamic</strong>:</strong></p>
<p>var实际上编译器抛给我们的语法糖一旦被编译编译器就会自动匹配var变量的实际类型并用实际类型来替换该变量的声明等同于我们在编码时使用了实际类型声明。而dynamic被编译后是一个Object类型编译器编译时不会对dynamic进行类型检查。</p>
<p>&nbsp;</p>
<p><strong>dynamic与反射</strong><strong>  </strong></p>
<div>
<p class="a">以前我们这样使用反射:&nbsp;</p>
<div class="cnblogs_Highlighter sh-gutter">
<div id="highlighter_342563" class="syntaxhighlighter csharp">
<div class="line number1 index0 alt2"><code class="csharp keyword">public</code>&nbsp;<code class="csharp keyword">class</code>&nbsp;<code class="csharp plain">DynamicSample</code></div>
<div class="line number2 index1 alt1"><code class="csharp plain">{</code></div>
<div class="line number3 index2 alt2"><code class="csharp keyword">public</code>&nbsp;<code class="csharp keyword">string</code>&nbsp;<code class="csharp plain">Name {&nbsp;</code><code class="csharp keyword">get</code><code class="csharp plain">;&nbsp;</code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
<div class="line number4 index3 alt1">&nbsp;</div>
<div class="line number5 index4 alt2"><code class="csharp keyword">public</code>&nbsp;<code class="csharp keyword">int</code>&nbsp;<code class="csharp plain">Add(</code><code class="csharp keyword">int</code>&nbsp;<code class="csharp plain">a,&nbsp;</code><code class="csharp keyword">int</code>&nbsp;<code class="csharp plain">b)</code></div>
<div class="line number6 index5 alt1"><code class="csharp plain">{</code></div>
<div class="line number7 index6 alt2"><code class="csharp keyword">return</code>&nbsp;<code class="csharp plain">a + b;</code></div>
<div class="line number8 index7 alt1"><code class="csharp plain">}</code></div>
<div class="line number9 index8 alt2"><code class="csharp plain">}</code></div>
<div class="line number10 index9 alt1"><code class="csharp plain">DynamicSample dynamicSample =&nbsp;</code><code class="csharp keyword">new</code>&nbsp;<code class="csharp plain">DynamicSample();&nbsp;</code><code class="csharp comments">//create instance为了简化演示我没有使用反射</code></div>
<div class="line number11 index10 alt2"><code class="csharp keyword">var</code>&nbsp;<code class="csharp plain">addMethod =&nbsp;</code><code class="csharp keyword">typeof</code><code class="csharp plain">(DynamicSample).GetMethod(</code><code class="csharp string">"Add"</code><code class="csharp plain">);</code></div>
<div class="line number12 index11 alt1"><code class="csharp keyword">int</code>&nbsp;<code class="csharp plain">re = (</code><code class="csharp keyword">int</code><code class="csharp plain">)addMethod.Invoke(dynamicSample,&nbsp;</code><code class="csharp keyword">new</code>&nbsp;<code class="csharp keyword">object</code><code class="csharp plain">[] { 1, 2 });</code></div>
</div>
</div>
<p class="a">&nbsp;现在,我们有了简化的写法:</p>
<div>
<div class="cnblogs_Highlighter sh-gutter">
<div id="highlighter_595570" class="syntaxhighlighter csharp">
<div class="line number1 index0 alt2"><code class="csharp plain">dynamic dynamicSample2 =&nbsp;</code><code class="csharp keyword">new</code>&nbsp;<code class="csharp plain">DynamicSample();</code></div>
<div class="line number2 index1 alt1"><code class="csharp keyword">int</code>&nbsp;<code class="csharp plain">re2 = dynamicSample2.Add(1, 2);</code></div>
</div>
</div>
</div>
</div>