为什么反射搜索突然找不到任何东西

为什么反射搜索突然找不到任何东西

本文介绍了为什么反射搜索突然找不到任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据此问题和答案并且运行良好:

I had the following code as per this question&answer How do I get the MIME type of a file being requested in ASP.NET C#?and it was working perfectly fine:

    static MimeMappingWrapper()
    {
        // dirty trick - Assembly.LoadWIthPartialName has been deprecated
        //Assembly ass = Assembly.LoadWithPartialName("System.Web");
        Assembly ass = Assembly.GetAssembly(typeof (HttpApplication));
        Type mimeMappingType = ass.GetType("System.Web.MimeMapping");

        GetMimeMappingMethod = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
    }

现在mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic)突然返回null.

可能是什么原因?在应用程序中没有什么特别的更改,即使更改了,它也将如何影响包装类的此构造函数?

What could be the reason? Nothing special was changed in the application and even if it was, how could it influence this constructor for the wrapper class?

推荐答案

我的猜测是服务器上安装的.NET Framework已升级,并且新版本下不再存在私有构造函数.

My guess would be that the .NET Framework installed on the server got upgraded, and the private constructor is no longer present under the new version.

程序集开发人员(在这种情况下为Microsoft)可以随意更改任何私有(或内部)类型或成员,而不必认为这是一个重大更改.

Assembly developers (in this case, Microsoft) are allowed to change any private (or internal) types or members at their whim, without it being considered to have been a breaking change.

编辑:我在.NET 4.0下的PC上进行了检查,但该方法仍然存在.这是它的签名:

Edit: I checked on my PC under .NET 4.0, and the method is still present. This is its signature:

// System.Web.MimeMapping
internal static string GetMimeMapping(string FileName)

这时,我的两个建议是在运行时检查您的实际.NET版本.

At this point, my two suggestions would be to check your actual .NET version at runtime…

var version = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion();

...并枚举MimeMapping类的方法以检查它们是否符合您的期望:

…and to enumerate the methods of the MimeMapping class to check whether they correspond to what you expect:

var methods = mimeMappingType.GetMethods(BindingFlags.Static | BindingFlags.NonPublic);

更新:好消息是 MimeMapping 类及其 GetMimeMapping 方法似乎在.NET 4.5中将其公开.

Update: The good news is that the MimeMapping class and its GetMimeMapping method seem like they might be made public in .NET 4.5.

但是,这意味着您的代码肯定会中断,因为您只在搜索NonPublic方法.尝试改为执行全包搜索,然后查看是否显示GetMimeMapping:

However, this means that your code would definitely break, since you’re only searching for NonPublic methods. Try performing an all-inclusive search instead and see whether GetMimeMapping shows up:

var methods = mimeMappingType.GetMethods(
    BindingFlags.Instance |
    BindingFlags.Static |
    BindingFlags.Public |
    BindingFlags.NonPublic |
    BindingFlags.FlattenHierarchy);

这篇关于为什么反射搜索突然找不到任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:57