33 lines
3.7 KiB
HTML
33 lines
3.7 KiB
HTML
<p>dynamic关键字可充当C#类型系统中的静态类型声明。这样,C#就获得了动态功能,同时仍然作为静态类型化语言而存在。</p>
|
||
<p><strong>Var与<strong>dynamic</strong>:</strong></p>
|
||
<p>var实际上编译器抛给我们的语法糖,一旦被编译,编译器就会自动匹配var变量的实际类型,并用实际类型来替换该变量的声明,等同于我们在编码时使用了实际类型声明。而dynamic被编译后是一个Object类型,编译器编译时不会对dynamic进行类型检查。</p>
|
||
<p> </p>
|
||
<p><strong>dynamic与反射</strong><strong> </strong></p>
|
||
<div>
|
||
<p class="a">以前我们这样使用反射: </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> <code class="csharp keyword">class</code> <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> <code class="csharp keyword">string</code> <code class="csharp plain">Name { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div>
|
||
<div class="line number4 index3 alt1"> </div>
|
||
<div class="line number5 index4 alt2"><code class="csharp keyword">public</code> <code class="csharp keyword">int</code> <code class="csharp plain">Add(</code><code class="csharp keyword">int</code> <code class="csharp plain">a, </code><code class="csharp keyword">int</code> <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> <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 = </code><code class="csharp keyword">new</code> <code class="csharp plain">DynamicSample(); </code><code class="csharp comments">//create instance为了简化演示,我没有使用反射</code></div>
|
||
<div class="line number11 index10 alt2"><code class="csharp keyword">var</code> <code class="csharp plain">addMethod = </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> <code class="csharp plain">re = (</code><code class="csharp keyword">int</code><code class="csharp plain">)addMethod.Invoke(dynamicSample, </code><code class="csharp keyword">new</code> <code class="csharp keyword">object</code><code class="csharp plain">[] { 1, 2 });</code></div>
|
||
</div>
|
||
</div>
|
||
<p class="a"> 现在,我们有了简化的写法:</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 = </code><code class="csharp keyword">new</code> <code class="csharp plain">DynamicSample();</code></div>
|
||
<div class="line number2 index1 alt1"><code class="csharp keyword">int</code> <code class="csharp plain">re2 = dynamicSample2.Add(1, 2);</code></div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div> |