【发布时间】:2013-02-08 01:40:24
【问题描述】:
我正在尝试移植 Mono.Cecil 以在 Windows Mobile 6 设备上使用 .NET CompactFramework 3.5。首先,我不得不对 Mono.Cecil 的源代码进行一些奇怪的调整(来自its GitHub 页面,提交:ec2a54fb00)。在试图理解为什么需要这些调整时,我有点惊讶。
第一个变化: Mono.Cecil 的源代码有表达式,对 System.Array 类型的对象调用“IsNullOrEmpty()”方法。但是,这种方法在微软实现的.NET框架中根本不存在。因此,代码无法编译。因此,我在 System.Array 类中添加了一个扩展方法:
static class ArrayExtensions
{
public static bool IsNullOrEmpty(this System.Array a)
{
return a.Length == 0;
}
}
第二个变化: Mono.Cecil 的源代码尝试在 System.String 类型的对象上调用“ToLowerInvariant()”方法。但是,CompactFramework 中不存在这样的方法。所以这是第二个调整:
static class StringExtensions
{
#if PocketPC
public static string ToLowerInvariant(this String a)
{
return a.ToLower();
}
#endif
}
这里我只是将对“ToLowerInvariant”方法的调用转发给 String 类的“ToLower”方法。
我在 Visual Studio 2008 中使用上述更改构建了 Mono.Cecil 的源代码,并定义了以下编译符号:
PocketPC
CF
接下来,我需要测试使用上述步骤构建的 Mono.Cecil DLL 文件。我的方法是读取一个程序集并用不同的名称重新创建它。为此,我创建了一个可以在 Windows Mobile 设备上运行的简单应用程序,并将其命名为 SmartDeviceProject1.exe。我阅读了与这个应用程序对应的程序集,并用不同的名称写了出来:
using System;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Forms;
using Mono.Cecil;
namespace SmartDeviceProject3
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
var assemblyDef = AssemblyDefinition.ReadAssembly(@"\Program Files\SmartDeviceProject1\SmartDeviceProject1.exe");
assemblyDef.Write(@"\Program Files\SmartDeviceProject1\SmartDeviceProject1New.exe");
}
}
}
新程序集名为 SmartDeviceProject1New.exe。当我尝试在 Windows Mobile 设备上运行新应用程序 SmartDeviceProject1New.exe 时,它无法运行。错误消息报告该文件不是有效的 PocketPC 应用程序。
我是不是哪里出错了?
P.S:但是,使用上面构建的 Mono.Cecil DLL 文件,我可以浏览 CIL 代码并检查它的不同方面。
【问题讨论】:
标签: visual-studio-2008 windows-mobile compact-framework mono.cecil