C++的性能C#的产能?! - .Net Native 系列五:.Net Native与反射

   此系列系小九的学堂原创翻译,翻译自微软官方开发向导,一共分为六个主题。本文是第五个主题:.Net Native与反射

  向导文链接:C++的性能C#的产能?! - .Net Native 系列:开发向导

 

  [小九的学堂,致力于以平凡的语言描述不平凡的技术。如要转载,请注明来源:小九的学堂cnblogs.com/xfuture]


   原文:Reflection and .NET Native

 .NET Native与反射

.NET Framework 4.5

    

Note 小贴士

这个主题依赖于预发行的.net native开发者预览版。下载地址: Microsoft Connect website. 友情提示需要注册..

  

  .NET Framework框架可以通过反射机制来进行元编程(在编译时完成部分本应在运行时完成的工作,可提高工作效率)。反射可以动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。支持序列化和反序列化,它支持一个对象的字段先被持久化之后再进行恢复。.NET Framework运行时just-in-time编译器可以基于可用的元数据来生成本地机器码。   

  .NET native运行时并不包含JIT编译器,所以其编译的机器码必须是提前生成的。当然有一系列的方法来测试哪些代码是已经生成的,但是这些规则并不能涵盖所有的元编程场景。所以需要在运行时提供指令来进行搜索元编程。同时.NET Native并不能将.NET Framework下的私有成员编译。

  下面将介绍如何使用基于反射的API和运行时指令xml文件的配置:

  

  基于反射的API


  

  在一些情况下你并不能得知代码中使用了反射而且.NET Native工具并不会保留运行时需要的元数据。本主题所涵盖的API并不能视为反射的API,但他们是基于反射来执行的。如果你使用这些API的话,需要添加一些信息来运行指令.rd.xml文件,这样就能正常调用这些API,而且不会导致MissingMetadataException的异常。

  例如:Type.MakeGenericType method

  通过调用这个方法可以在运行时动态生成泛型AppClass<T>

  

var t = Type.GetType("App1.AppClass`1", true);
Type[] typeArgs = {typeof(int)};
Type t2 = t.MakeGenericType(typeArgs);
Activator.CreateInstance(t2);

  为使该代码能够成功运行,需要一些元数据支持。首先是在.rd.xml中添加对AppClass<T>的泛型支持

  

<Type Name="App1.AppClass`1" Browse="Required PublicAndInternal" />

  添加后在.NET Framework下就可以调用Type.GetType()方法来获得对象类型了。

  但在.NET Native下调用就会抛出MissingMetadataException的异常。

  

This operation cannot be carried out as metadata for the following type was removed for performance reasons:

App1.AppClass`1<System.Int32>.

   你可以在运行时指令文件(.rd.xml)添加下面运行指令来激活对Int32的支持:

  

<TypeInstantiation Name="App1.AppClass" Arguments="System.Int32" 
                   Activate="Required Public" />

    不同的实例需要不同的指令,添加后就能正常运行。

  类似的方法还有:MethodInfo.MakeGenericMethod method, Array.CreateInstance and Type.MakeTypeArray methods

   所以,在依赖反射的方法使用时,需要添加对反射类型的支持的指令才可以使用。

 

   运行时指令文件(.rd.xml)配置 


  

  运行时指令文件(.rd.xml)是一个xml配置的文件,指定了配置的程序元素是否可用于反射。下面是该文件的一个样例。

  

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Application>
  <Namespace Name="Contoso.Cloud.AppServices" Serialize="Required Public" />
  <Namespace Name="ContosoClient.ViewModels" Serialize="Required Public" />
  <Namespace Name="ContosoClient.DataModel" Serialize="Required Public" />
  <Namespace Name="Contoso.Reader.UtilityLib" Serialize="Required Public" />

  <Namespace Name="System.Collections.ObjectModel" >
    <TypeInstantiation Name="ObservableCollection" 
          Arguments="ContosoClient.DataModel.ProductItem" Serialize="Public" />
    <TypeInstantiation Name="ReadOnlyObservableCollection" 
          Arguments="ContosoClient.DataModel.ProductGroup" Serialize="Public" />
  </Namespace>
</Application>
</Directives>

  文件结构:

  该文件在 http://schemas.microsoft.com/netfx/2013/01/metadata 命名空间下。

  根元素Directives是指令元素,可以包含一个或者多个Library元素和零个或者一个Application元素,结构如下

  

Directives [1:1]
          Application [0:1]
                     Assembly [0:M]
                                Namespace [0:M]
                                      . . . 
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M]
                                      . . . 
                     Namespace [0:M]
                                Namespace [0:M]
                                      . . . 
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M] 
                                      . . . 
                     Type [0:M]
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M]
                                      . . . 
                                 GenericParameter [0:M]
                                Method [0:M]
                                       Parameter [0:M] 
¶                                      TypeParameter [0:M]
                                     GenericParameter [0:M]
                                MethodInstantiation (constructed generic method) [0:M] 
                                Property [0:M]
                                Field [0:M]
                                Event [0:M]
                     TypeInstantiation (constructed generic type) [0:M]
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M]
                                      . . . 
                                Method [0:M]
¶                                       Parameter [0:M] 
¶                                      TypeParameter [0:M]
                                     GenericParameter [0:M]
                                MethodInstantiation (constructed generic method) [0:M]
                                Property [0:M]
                                Field [0:M]
                                Event [0:M]
          Library [0:M]
                     Assembly [0:M]
                                Namespace [0:M]
                                      . . . 
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M]
                                      . . . 
                     Namespace [0:M]
                                Namespace [0:M]
                                      . . . 
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M] 
                                      . . . 
                     Type [0:M]
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M]
                                      . . . 
                                GenericParameter [0:M]
                                Method [0:M]
                                MethodInstantiation (constructed generic method) [0:M] 
                                Property [0:M]
                                Field [0:M]
                                Event [0:M]
                     TypeInstantiation (constructed generic type) [0:M]
                                Type [0:M]
                                      . . . 
                                TypeInstantiation (constructed generic type) [0:M]
                                      . . . 
                                Method [0:M]
                                MethodInstantiation (constructed generic method) [0:M]
                                Property [0:M]
                                Field [0:M]
                                Event [0:M]

   

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
     <!-- Child elements go here -->  
  </Application>
  <Library Name="Extensions">
     <!-- Child elements go here -->  
  </Library>
</Directives>

   在节点中可以配置需要反射拿到的属性。具体开发中遇到的反射问题可以参阅之前的一篇文章:C++的性能C#的产能?! - .Net Native 系列《二》:.NET Native开发流程详解

  由于.NET Native是源生的机器码,它的目的就在于高效率高性能实现.NET Framework架构开发的应用程序的运行,反射消耗性能较多,尽量少用或者更改其他方法来实现逻辑。

   

  更多.rd.xml信息可以参阅:http://msdn.microsoft.com/en-us/library/dn600639(v=vs.110).aspx

 

  希望大家喜欢这项技术,也可以写下自己对该技术的展望。

 

posted @ 2014-05-21 16:07  techlead_krischang  阅读(2774)  评论(9编辑  收藏  举报