c# - 在 C# 中查看类型 T 的内部实现的可能性

标签 c# .net generics .net-4.0

当@No One 回答我的问题时here他在下面提到了类似的东西,

 using(SomeClass c = new SomeClass())
 { }

**

will translate to

**

 try
 {
     SomeClass c = new SomeClass();
 }
 finally
 {
   c.Dispose();
 }

好吧,你们是怎么说的?

此外,你们中的一些人提到“内部将以某种方式实现”,您如何看待某些东西的内部实现?

例如,当我浏览 MSDN 中的“泛型概念”时,他们说 List 是泛型的,并展示了如何在内部实现列表的示例实现 here .我们如何才能真正为其他人做到这一点?

我在 VS 2010 中尝试过。但是对象浏览器仅显示特定类型公开的方法或字段。

(我只是很好奇,因为 list 的内部实现的例子太棒了。非常有趣的东西!)

最佳答案

C# specification在第 8.13 章对此进行了解释:

A using statement of the form

using (ResourceType resource = expression) statement

corresponds to one of three possible expansions. When ResourceType is a non-nullable value type, the expansion is

{
  ResourceType resource = expression;
  try {
      statement;
  }
  finally {
      ((IDisposable)resource).Dispose();
  }
}

Otherwise, when ResourceType is a nullable value type or a reference type other than dynamic, the expansion is

{
  ResourceType resource = expression;
  try {
      statement;
  }
  finally {
      if (resource != null) ((IDisposable)resource).Dispose();
  }
}

Otherwise, when ResourceType is dynamic, the expansion is

{
  ResourceType resource = expression;
  IDisposable d = (IDisposable)resource;
  try {
      statement;
  }
  finally {
      if (d != null) d.Dispose();
  }
}

关于您的第二个问题:.NET 源代码可用于浏览器/下载。检查这个问题:Downloadable/browsable version of the .NET Framework source code?

关于c# - 在 C# 中查看类型 T 的内部实现的可能性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18629056/

相关文章:

c# - 更改asp.net中gridview内文本框的背景颜色?

c# - 确保服务连通性

html - 无法使用 Orchard CMS 从 CSS 加载图像

c# - 在 .NET : good, 中隐藏继承的通用接口(interface)成员是坏的还是丑的?

java - 两个通用接口(interface)相互需要,如何正确实现它们?

扩展类的 Java 泛型未检查警告

c# - 从服务器资源管理器拖放到数据集和 DBML 设计器不工作

c# - 保留同一个类的列表

java - 采取什么方法使我的代码更通用 Java/Spring?

c# - 在 C# 中使用外部文件和 MVVM 模式在运行时更改语言