为什么C#编译器创建此代码PrivateImplementationDetails? [英] Why does the c# compiler create a PrivateImplementationDetails from this code?

查看:2534
本文介绍了为什么C#编译器创建此代码PrivateImplementationDetails?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现下面的代码:

public static class MimeHelper
    {
    	public static string GetMimeType(string strFileName)
    	{
    		string retval;
    		switch (System.IO.Path.GetExtension(strFileName).ToLower())
    		{
    			case ".3dm": retval = "x-world/x-3dmf"; break;
    			case ".3dmf": retval = "x-world/x-3dmf"; break;
    			case ".a": retval = "application/octet-stream"; break;
    			// etc...
    			default: retval = "application/octet-stream"; break;
    		}
    		return retval;
    	} 
    }



导致编译器来创建此namespaceless,内部类(从反射复制):

causes the compiler to create this namespaceless, internal class (copied from Reflector):

<PrivateImplementationDetails>{621DEE27-4B15-4773-9203-D6658527CF2B}
    - $$method0x60000b0-1 : Dictionary<String, Int32>
    - Used By: MimeHelper.GetMimeType(String) : String



这是为什么?
我怎么会改变上面的代码,因此不会发生(只是出于兴趣)

Why is that? How would I change the above code so it doesn't happen (just out of interest)

感谢

安德鲁

推荐答案

它创建字典来处理各种情况的查询在switch语句,而不是使数IFS分支出来的设置返回值。相信我 - 你不想改变它是如何做的 - 除非你想在地图明确

It's creating the dictionary to handle the lookups of the various cases in the switch statement instead of making several branching ifs out of it to set the return value. Trust me -- you don't want to change how it's doing it -- unless you want to make the map explicit.

ASIDE :我原本认为字典存储从每个案件的一个索引地图到另一个地图的返回值。根据@Scott(见注释),它实际存储的索引对应于这种情况下,要执行的代码的一个标签。这使得当你考虑到这将每种情况下执行的代码可能会有所不同,可能是比在给定的例子长得多绝对意义上的。

ASIDE: I had originally assumed that the dictionary stored a map from each case to the an index into another map for the return values. According to @Scott (see comments), it actually stores an index to a label for the code that should be executed for that case. This makes absolute sense when you consider that the code that would be executed for each case may differ and may be much longer than in the given example.

修改:基于您的评论,我想我可能会忍不住映射存储在外部配置文件,阅读他们在启动时,构建实际的地图 - 无论是从关键单级地图值或从关键指数和指数值类似的多级地图。我认为它会更容易在一个配置文件,以保持这些映射,而不是每次你需要添加或删除特定情况下的时间更新代码。

EDIT: Based on your comment, I think I might be tempted to store the mappings in an external configuration file, read them in during start up, and construct the actual map -- either a single level map from key to value or a similar multilevel map from key to index and index to value. I think it would be easier to maintain these mappings in a configuration file than to update the code every time you needed to add or remove a particular case.

这篇关于为什么C#编译器创建此代码PrivateImplementationDetails?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆