Information Technology Reference
In-Depth Information
into the rest of your program, or into all the code developed by develop-
ers who use your objects.
One scenario where you will use dynamic typing is to interact with objects
created in dynamic environments, such as IronPython. When your design
makes use of dynamic objects created using dynamic languages, you
should wrap them in C# objects that enable the rest of the C# world to
blissfully ignore the fact that dynamic typing is even happening.
Yo u m a y w a n t t o p i c k a d i f f e r e n t s o l u t i o n f o r t h o s e s i t u a t i o n s w h e r e y o u
use dynamic to produce duck typing. Look at the usages of the duck typ-
ing sample from Item 38. In every case, the result of the calculation was
dynamic. That might not look too bad. But, the compiler is doing quite a
bit of work to make this work. These two lines of code (see Item 38):
dynamic answer = Add( 5 , 5 );
Console .WriteLine(answer);
turn into this to handle dynamic objects:
// Compiler generated, not legal user C# code
object answer = Add(5, 5);
if (<Main>o__SiteContainer0.<>p__Site1 == null)
{
<Main>o__SiteContainer0.<>p__Site1 =
CallSite<Action<CallSite, Type, object>>.Create(
new CSharpInvokeMemberBinder(
CSharpCallFlags.None, "WriteLine",
typeof(Program), null, new CSharpArgumentInfo[]
{
new CSharpArgumentInfo(
CSharpArgumentInfoFlags.IsStaticType |
CSharpArgumentInfoFlags.UseCompileTimeType,
null),
new CSharpArgumentInfo(
CSharpArgumentInfoFlags.None,
null)
}));
}
<Main>o__SiteContainer0.<>p__Site1.Target.Invoke(
<Main>o__SiteContainer0.<>p__Site1,
typeof(Console), answer);
 
Search WWH ::




Custom Search