﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-提琴协奏-随笔分类-C#</title><link>http://www.cppblog.com/flagman/category/15577.html</link><description> 唐亮的个人技术博客
【欢迎转载，但请标明原作者】</description><language>zh-cn</language><lastBuildDate>Sat, 12 Feb 2011 15:41:32 GMT</lastBuildDate><pubDate>Sat, 12 Feb 2011 15:41:32 GMT</pubDate><ttl>60</ttl><item><title>反射的特性是经常会使用到的</title><link>http://www.cppblog.com/flagman/archive/2011/02/12/reflection_feature_used_in_daily_dev.html</link><dc:creator>flagman</dc:creator><author>flagman</author><pubDate>Sat, 12 Feb 2011 09:21:00 GMT</pubDate><guid>http://www.cppblog.com/flagman/archive/2011/02/12/reflection_feature_used_in_daily_dev.html</guid><wfw:comment>http://www.cppblog.com/flagman/comments/139944.html</wfw:comment><comments>http://www.cppblog.com/flagman/archive/2011/02/12/reflection_feature_used_in_daily_dev.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/flagman/comments/commentRss/139944.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flagman/services/trackbacks/139944.html</trackback:ping><description><![CDATA[<p><em>【 某网友讨论道: 】<br>: RT，反射的特性发现很少用啊</em><br></p>
<p><br><br>恰恰相反，有些反射的特性是经常会被使用到的。</p>
<p>反射总体上分成两大特性，一是自省，二是发射；</p>
<p>自省的能力极为重要，而且几乎会天天用到，很少见到过哪个.net应用中不使用attribute的，而attribute特性就是metadata通过在自省能力支撑下实现的；当然自省不单单是attribute特性的运用，只要是在运行时动态检视程序自身的特性都要由反射的自省能力来支持，比如Visual Studio的IDE（这个集成开发环境本身就是.net应用的好案例）对于.net组件的自动探测功能；同时，自省的能力也是基于虚拟机平台的语言，比如c#和java，区别于传统语言比如c和c++的重要特性之一，这提供了程序设计开发更为便利和安全的运行时环境；相对而言，在c++（当然是native而不是managed）的环境下，除了RTTI极为单薄的运行时自省，也就是QT这个库通过meta-object system部分模拟了自省的特性；</p>
<p>反射的另外一个重要特性就是发射，它让&#8220;程序可以写程序&#8221;了，简要的说就是在运行时动态生成MSIL并加载运行以及持久化动态生成的MSIL的能力；由这个特性的支持，让原先一些程序设计和开发领域相对困难和繁琐的工作，比如元编程meta programming，比如动态代理dynamic proxy，比如AOP中的基础设施weaver的实现，变得可能或相对易于实现；反射的特性，也是基于虚拟机平台CLR的支持，以metadata为基础来实现的，所以这也是虚拟机平台语言的特有优势，而在传统语言平台上，这是难以实现的；比如关于meta programming，c++就是通过模板特性实现的编译期meta programming，这与虚拟机平台上实现的运行时meta programming还是有比较大的差距（比如前者如何保证生成的代码的type-safe）；</p>
<p>以上这两个特性，自省和发射，都有个共同点，他们都是围绕着metadata机制，并在虚拟机平台运行时环境CLR支持下实现的，前者是运行时检视相关的metadata，后者是运行时动态生成相关的metadata和MSIL；从这点也就可以看出，要想深入理解这些特性，就需要研究metadata和MSIL的实现，以及虚拟机运行时环境的实现（在java平台上，就是bytecode和JVM）；</p>
<p>所以，反射，可能是虚拟机平台所提供的相对最为强劲，最为复杂，和平台运行时本身关系最密切，也是区别于传统语言和运行时最鲜明的特性。</p>
<img src ="http://www.cppblog.com/flagman/aggbug/139944.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flagman/" target="_blank">flagman</a> 2011-02-12 17:21 <a href="http://www.cppblog.com/flagman/archive/2011/02/12/reflection_feature_used_in_daily_dev.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>CLR系列--探索SSCLI【1】</title><link>http://www.cppblog.com/flagman/archive/2010/12/13/136255.html</link><dc:creator>flagman</dc:creator><author>flagman</author><pubDate>Mon, 13 Dec 2010 01:02:00 GMT</pubDate><guid>http://www.cppblog.com/flagman/archive/2010/12/13/136255.html</guid><wfw:comment>http://www.cppblog.com/flagman/comments/136255.html</wfw:comment><comments>http://www.cppblog.com/flagman/archive/2010/12/13/136255.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flagman/comments/commentRss/136255.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flagman/services/trackbacks/136255.html</trackback:ping><description><![CDATA[<p>Fusion is one of the most importants features among ones in the runtime implementation of CLI.</p>
<p>In the fusion, or any other components or modules, how to retrieve the execution engine instance and how to generate such engine?</p>
<p>UtilExecutionEngine, implemented as COM object, support Queryinterface/AddRef/Release, and exposed via interface IExecutionEngine.</p>
<p>With SELF_NO_HOST defined, <br>BYTE g_ExecutionEngineInstance[sizeof(UtilExecutionEngine)];<br>g_ExecutionEngineInstance would be the singleton instance of current execution engine,</p>
<p>otherwise, without SELF_NO_HOST, the 'sscoree' dll would be loaded and try to get the exported function, which is named 'IEE' from such dll. Here, it is the well-known shim, in .net CLR, such module is named 'mscoree'. Further, if 'IEE' could not be found in such dll, system would try to locate another exported function, named 'LoadLibraryShim', and use such function to load the 'mscorwks' module, and try to locate the 'IEE' exportd functionin it.</p>
<p>It's very obvious that Rotor has implemented its own execution engine, but it also gives or make space for implementation of execution engine from 3rd party. Here, .net CLR is a good candidate definitely, Rotor might load the mscorwks.dll module for its usage.</p>
<p>PAL, PALAPI, for example, HeapAlloc, one famous WIN32 API, has been implemented as one PALAPI (defined in Heap.c), to make it possible that the CLI/Rotor be ported smoothly to other OS, such freebsd/mac os.</p>
<p>CRT routines are also reimplemented, such as memcpy, it has been implemented as GCSafeMemCpy</p>
<p>There're many macros in fuctions, such as SCAN_IGNORE_FAULT/STATIC_CONTRACT_NOTHROW/STATIC_CONTRACT_NOTRIGGER, they are for static analysis tool to scan, analyse and figour out the potential issues in code.</p>
<p>From view point of the execution model by CLI, the act of compiling (including JIT) high-level type descriptions would be separated from the act of turning these type descriptions into processor-specific code and memory structures.</p>
<p>And such executino model, in other word, the well-known 'managed execution', would defer the loading, verification and compilation of components until runtime really needs; At the same time, the type-loading is the key trigger that causes CLI's tool chain to be engaged at runtime. Deferred compilation(lead to JIT)/linking/loading would get better portability to different target platform and be ready for version change; The whole deferred process would driven by well-defined metadata and policy, and it would be very robust for building a virtual execution environment;</p>
<p>At the top of such CLI tool chain, fusion is reponsible for not only finding and binding related assemblies, which are via assembly reference defined in assembly, fusion also takes another important role, loader, and its part of functionality is implemented in PEAssembly, ClassLoader classes. For example, ClassLoader::LoadTypeHandleForTypeKey.</p>
<p>For types in virtual execution environment of CLI, rotor defines four kinds of elements for internal conducting, <br>ELEMENT_TYPE_CLASS for ordinary classes and generic instantiations(including value types);<br>ELEMENT_TYPE_ARRAY AND ELEMENT_TYPE_SZARRAY for array types<br>ELEMENT_TYPE_PRT and ELEMENT_TYPE_BYREF for pointer types<br>ELEMENT_TYPE_FNPTR for function pointer types</p>
<p>every type would be assigned unique ulong-typed token, and such token would be used to look up in m_TypeDefToMethodTableMap (Linear mapping from TypeDef token to MethodTable *)which is maintained by current module; If there it is, the pointer to method table of such type would be retrieved, or it would look up in the loader module, where the method table should exist in while it's JIT loaded, not launched from NGEN image;</p>
<p>And all the unresolved typed would be maintained in a hash table, PendingTypeLoadTable; Types and only those types that are needed, such as dependencies, including parent types, are loaded in runtime, such type is fully loaded and ready for further execution, and other unresolved types would be kept in the previous hash table.</p>
<img src ="http://www.cppblog.com/flagman/aggbug/136255.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flagman/" target="_blank">flagman</a> 2010-12-13 09:02 <a href="http://www.cppblog.com/flagman/archive/2010/12/13/136255.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>