检测缺失方法

时间:2016-02-20 01:34:47

标签: c# .net

假设我有2个不同的队伍维护2个dll:

Team1.dll(v1.0)

public class Foo
{
    int GetValue() { return 3; }
}

Team2.dll(v1.0)

public class Bar
{
    public int IncFooValue(Foo foo) { return foo.GetValue() + 1; }
}

执行Team1.dll(v1.0)和Team2.dll(v1.0)时,一切都很好。但是假设Team1.dll被更改了&删除了方法Foo.GetValue()(v1.1)并将其放在Team2.dll旁边(所有这些都没有重建Team2.dll)。如果执行,那么你会得到一个MissingMethodException。

问题: 如何在不执行Team1.dll的情况下检测Team1.dll是否与Team2.dll不兼容?

例如:

Foreach Class in Team2.dll
    Foreach Method in Class
        Foreach Instruction in Method
            If Instruction not exists in Team1.dll
                Throw "Does not exist"

1 个答案:

答案 0 :(得分:0)

检查类型是否包含方法的定义。

      if (foo.GetType().GetMethod("GetValue") != null)
      {
        return foo.GetValue() + 1;
      }

正如其他人所建议的那样,你可能最好不要追求另一种策略(更好的版本控制/向后兼容性或在主叫端处理异常。