错误85参数1:无法从'System.Reflection.ConstructorInfo'转换为'Mono.Cecil.TypeReference'

时间:2012-07-27 17:21:51

标签: static analysis gendarme

目前我正在尝试使用Visual Studio 2010构建Gendarme 2.10。以下是我收到的一些错误:

Error   85  Argument 1: cannot convert from 'System.Reflection.ConstructorInfo' to 'Mono.Cecil.TypeReference'   C:\Tools\mono-tools\external\cecil\Test\Mono.Cecil.Tests\CustomAttributesTests.cs   359 45  Mono.Cecil.Tests
Error   12  Argument 1: cannot convert from 'System.Reflection.FieldInfo' to 'Mono.Cecil.TypeReference' C:\Tools\mono-tools\external\cecil\Test\Mono.Cecil.Tests\ImportReflectionTests.cs   103 45  Mono.Cecil.Tests
Error   24  Argument 1: cannot convert from 'System.Reflection.FieldInfo' to 'Mono.Cecil.TypeReference' C:\Tools\mono-tools\external\cecil\Test\Mono.Cecil.Tests\ImportReflectionTests.cs   149 44  Mono.Cecil.Tests
Error   46  Argument 1: cannot convert from 'System.Reflection.FieldInfo' to 'Mono.Cecil.TypeReference' C:\Tools\mono-tools\external\cecil\Test\Mono.Cecil.Tests\ImportReflectionTests.cs   198 44  Mono.Cecil.Tests
Error   60  Argument 1: cannot convert from 'System.Reflection.FieldInfo' to 'Mono.Cecil.TypeReference' C:\Tools\mono-tools\external\cecil\Test\Mono.Cecil.Tests\ImportReflectionTests.cs   276 39      Mono.Cecil.Tests
Error   14  Argument 1: cannot convert from 'System.Reflection.MethodInfo' to 'Mono.Cecil.TypeReference'    C:\Tools\mono-tools\external\cecil\Test\Mono.Cecil.Tests\ImportReflectionTests.cs   117 43  Mono.Cecil.Tests

删除所有测试项目后,以下是我遇到的错误:

Error   4   Argument 1: cannot convert from 'System.Type' to 'Mono.Cecil.TypeReference' C:\Tools\mono-tools\gendarme\framework\Gendarme.Framework.Helpers\PrimitiveReferences.cs    53  25  Gendarme.Framework
Error   3   The best overloaded method match for 'Mono.Cecil.ModuleDefinition.Import(Mono.Cecil.TypeReference)' has some invalid arguments  C:\Tools\mono-tools\gendarme\framework\Gendarme.Framework.Helpers\PrimitiveReferences.cs    53  10  Gendarme.Framework

以下是上述两个错误所引用的代码:

static TypeReference GetReference (Type type, IMetadataTokenProvider metadata)
{
    ModuleDefinition module = metadata.GetAssembly ().MainModule;
    TypeReference tr;
    if (!module.TryGetTypeReference (type.FullName, out tr))
        tr = module.Import (type);
    return tr;
}

有没有人有任何建议? TIA。

罗杰

1 个答案:

答案 0 :(得分:0)

这是我放在一起的临时解决方案。请随时评论:

    static TypeReference GetReference (Type type, IMetadataTokenProvider metadata)
    {
        ModuleDefinition module = metadata.GetAssembly ().MainModule;
        ModuleKind kind = ModuleKind.Windows;
        ModuleDefinition definition = ModuleDefinition.CreateModule(module.Name, kind);
        Version version = new Version(1, 0);
        AssemblyNameDefinition nameDefinition = new AssemblyNameDefinition(module.Name, version);
        AssemblyDefinition assemblyDefinition = AssemblyDefinition.CreateAssembly(nameDefinition, definition.Name, kind);
        assemblyDefinition = AssemblyDefinition.ReadAssembly(module.Name);
        definition.Assembly = assemblyDefinition;
        IMetadataScope scope = new ModuleReference(module.Name);
        scope.MetadataToken = assemblyDefinition.MetadataToken;

        TypeReference tr = new TypeReference(type.Namespace, type.Name, definition, scope);
        //if (!module.TryGetTypeReference(type.FullName, out tr))
        //    tr = module.Import(type);
        return tr;
    }