System.Object 是 .NET 中所有类型的根吗?

System.Object 是 .NET 中所有类型的根吗?

 

Written by Allen Lee

 

这不是废话吗?每个人一接触 .NET 就会被提醒任何类型都是直接或者间接继承自 System.Object 的,即使 MSDN 也记录着类似的话。事实果真如此吗?如果你在命令行界面输入 ilasm /?,将会发现这样一个参数:

/NOAUTOINHERIT Disable inheriting from System.Object by default

现在,我尝试利用这个参数做一个没有基类的托管类型。首先,打开记事本,写下如下代码:

//  Adam.cs

public   class  Adam
{
  
public void Hello()
  
{
    System.Console.WriteLine(
"I have no parents!");
  }

}

用 csc /t:library Adam.cs 把 Adam.cs 编译成 Adam.dll。接着,在记事本里写下如下代码:

//  Program.cs

class  Program
{
  
static void Main()
  
{
    Adam adam 
= new Adam();
    adam.Hello();
  }

}

并用 csc /r:Adam.dl Program.cs 把 Program.cs 编译成 Program.exe。运行一下这个程序,得到如下输出:

I have no parents!

当然,此时的 Adam 还不是真正没有 parent 的。现在,用 ILDASM 打开 Adam.dll,依次选择 File -> Dump 菜单,点击 OK,把 IL 代码另存为 Adam.il。接着,用记事本打开 Adam.il,找到 Adam 的定义,把它后面的 extends [mscorlib]System.Object 注释掉,保存、退出。然后,在命令行里输入 ilasm /dll /noautoinherit adam.il,得到新的 Adam.dll。现在,再次运行 Program.exe,虽然输出和之前一样,但当你使用 ILDASM 查看 Adam.dll 里的 Adam 类时,你发现它已经变成“真正的亚当”了!就连 Reflector 也说它没有 Base Types 了,呵呵~~~

然而,我不禁自问,这样一个类究竟有什么用呢?如果没有用处,那么 Microsoft 又为何在 2.0 中引入 /noautoinherit 这样一个参数呢?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In .NET 6 and later versions, the `System.Windows.Forms` namespace is not included in the default installation of WPF applications. However, you can still use the `FolderBrowserDialog` class in your WPF application by adding a reference to the `System.Windows.Forms` NuGet package and using the `System.Windows.Forms.Integration` namespace. Here are the steps to use `FolderBrowserDialog` in .NET 6 WPF: 1. Add a reference to the `System.Windows.Forms` NuGet package in your WPF project. 2. Add the following using statements to the top of your code file: ```csharp using System.Windows.Forms; using System.Windows.Forms.Integration; ``` 3. In your code, create a `FolderBrowserDialog` object and use the `ShowDialog` method to display the dialog box: ```csharp FolderBrowserDialog dialog = new FolderBrowserDialog(); if (dialog.ShowDialog() == DialogResult.OK) { // do something with the selected folder } ``` 4. If you want to use the `FolderBrowserDialog` in a WPF window or page, you can use the `WindowsFormsHost` control to host the dialog box: ```xml <Window xmlns:winforms="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" ...> <Grid> <winforms:WindowsFormsHost> <forms:FolderBrowserDialog x:Name="folderDialog" /> </winforms:WindowsFormsHost> </Grid> </Window> ``` Then, you can use the `ShowDialog` method to display the dialog box: ```csharp if (folderDialog.ShowDialog() == DialogResult.OK) { // do something with the selected folder } ``` Note that you will also need to add a reference to the `WindowsFormsIntegration` assembly in your WPF project.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值