在Framework 4.0中:找出新增的方法与新增的类(一)

 更新时间:2020年6月25日 11:41  点击:2373
程序思路:动态加载V4和V2的mscorlib.dll程序集,通过反射进行比较。
之所以加载mscorlib.dll 是因为framework中的大部分类都在这里,而发生变更的也就是这里最多。

第一步:新建控制台程序:

加载程序集:

image

加载程序集完成后,自然要获取程序集中的所有Type,这里直接使用默认的GetTypes方法。

image

获取了v4Types v2Types之后,就要对v2Types里面的所有Typev4Types里面的所有Type进行比较,

而比较的内容就是GetMembers返回的所有MemberInfo.

完整代码如下:

复制代码 代码如下:

static void Main(string[] args)
{
    string v4AssemblyPath = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll";
    string v2AssemblyPath = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll";
    //Assembly v4Assembly = typeof(object).Assembly;
    Assembly v4Assembly = Assembly.LoadFile(v4AssemblyPath);
    Assembly v2Assembly = Assembly.LoadFile(v2AssemblyPath);
    Type[] v4Types = v4Assembly.GetTypes();
    Type[] v2Types = v2Assembly.GetTypes();
    foreach (Type v2Type in v2Types)
    {
        Type v4Type = v4Types.First(t => t.FullName == v2Type.FullName);
        MemberInfo[] v2Mis = v2Type.GetMethods();
        MemberInfo[] v4Mis = v4Type.GetMethods();
        if (v2Mis.Length != v4Mis.Length)
        {
            foreach (MemberInfo v2Mi in v2Mis)
            {
                bool isExist = false;
                foreach (MemberInfo v4Mi in v4Mis)
                {
                    if (v2Mi.Name == v4Mi.Name)
                    {
                        isExist = true;
                        break;
                    }
                }
                if (!isExist)
                {
                    Console.WriteLine("{0}:{1}", v2Type.FullName, v2Mi.Name);
                }
            }
        }
    }
    Console.WriteLine("程序执行完毕!");
    Console.ReadLine();
}


程序运行结果如下:

image

[!--infotagslink--]

相关文章