如何模拟在.NET 4的损坏状态的异常? [英] How to simulate a corrupt state exception in .NET 4?

查看:147
本文介绍了如何模拟在.NET 4的损坏状态的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,在.NET 4微软增加了HandleProcessCorruptedStateExceptions属性:

Well, in .NET 4 Microsoft added the HandleProcessCorruptedStateExceptions attribute:

<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.exceptionservices.handleprocesscorruptedstateexceptionsattribute.aspx">HandleProcessCorruptedStateExceptionsAttribute类

我想测试此功能。我怎样才能把我的应用程序,以一个腐败国家?

I want to test this feature. How can I bring my application to a "corrupt state"?

推荐答案

搞砸了垃圾回收堆始终是一个很好的方式:

Screwing up the garbage collected heap is always a good way:

using System;
using System.Runtime.InteropServices;


class Program {
  unsafe static void Main(string[] args) {
    var obj = new byte[1];
    var pin = GCHandle.Alloc(obj, GCHandleType.Pinned);
    byte* p = (byte*)pin.AddrOfPinnedObject();
    for (int ix = 0; ix < 256; ++ix) *p-- = 0;
    GC.Collect();   // kaboom
  }
}

这篇关于如何模拟在.NET 4的损坏状态的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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