---
title: "XNA学习笔记7-源码分析(续)"
author: "andyjackson"
date: "2010-12-08T20:13:11Z"
tags: []
categories: ["编程语言"]
slug: "xna-7"
draft: false
lastmod: "2010-12-08T20:13:56Z"
id: 836993
url: "https://ajwang.iteye.com/blog/836993"
---

![](http://dl.iteye.com/upload/attachment/354690/c382e20c-478e-37b9-8eed-950467dd1dd4.png)这是第二篇中用到的一张照片，里面描绘了整个过程：importer->processor->serializer->deserializer。上一篇中讨论了deserializer，这一节开始，逐步讨论其他部分。

<span style="color:blue">**先从importer开始**</span>：
```java

namespace Microsoft.Xna.Framework.Content.Pipeline{
  public abstract class ContentImporter<T> : IContentImporter{
    public abstract T Import(string filename, ContentImporterContext context);
    object Import(string filename, ContentImporterContext context){
      return this.Import(filename, context);
    }
  }
}

```
该类是系统中所有导入器的父类，从代码中很容易看出只需要重写其Import方法即可，这里用到了模版设计模式。我们可以看看系统提供的一些导入器：![](http://dl.iteye.com/upload/attachment/362311/840af3f3-ecde-3ad1-82c4-dbb096e698b9.png)这些导入器是在**Microsoft.Xna.Framework.Content.Pipeline.dll**外部，其代码非常复杂，而且设计到调用本地C++的库文件，而在当前dll中定义了两个比较简单的导入器：FontDescriptionImporter和XmlImporter。
```java

namespace Microsoft.Xna.Framework.Content.Pipeline
{ 
  [contentimporter(".spritefont", 
     displayname="Sprite Font Description - XNA Framework", 
     DefaultProcessor="FontDescriptionProcessor")]
  public class FontDescriptionImporter : ContentImporter<FontDescription>
  {
    public override FontDescription Import(string filename, ContentImporterContext context)
    {
      FontDescription description = null;
      using (XmlReader reader = XmlReader.Create(filename))
      {
        description = IntermediateSerializer.Deserialize<FontDescription>(
           reader, filename);
      }
      description.Identity = new ContentIdentity(
          new FileInfo(filename).FullName, "FontDescriptionImporter");
      return description;
    }
  }
}
```

```java

  [localizedcontentimporter("xmlimporterdisplayname", ".xml")]
  public class XmlImporter : ContentImporter<object>
  {
    public override object Import(
        string filename, ContentImporterContext context)
    {
      using (XmlReader reader = XmlReader.Create(filename))
      {
        return IntermediateSerializer.Deserialize<object>(reader, filename);
      }
    }
  }
}

```

      <span style="color:blue">**下面讨论一下processor**</span>。
```java

namespace Microsoft.Xna.Framework.Content.Pipeline{  
  public abstract class ContentProcessor<TInput, TOutput> : IContentProcessor{
    object IContentProcessor.Process(object input, ContentProcessorContext context){
      if (input == null){ throw new ArgumentNullException("input"); }
      if (!(input is TInput)){ throw new }
      TInput local = (TInput) input;
      return this.Process(local, context);
    }   
    public abstract TOutput Process(TInput input, ContentProcessorContext context);
  }
}

```
采用了跟importer和typereader相同的设计思路。自定义的processor只需要重写Process方法即可。serializer也差不多，不想写了~~