System.GC.GetGeneration 方法 (WeakReference)

方法描述

返回指定弱引用的目标的当前代数。

语法定义(C# System.GC.GetGeneration 方法 (WeakReference) 的用法)

public static int GetGeneration(
	WeakReference wo
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
wo System-WeakReference 引用要确定其代数的目标对象的 WeakReference。
返回值 System.Int32 wo 的目标的当前代数。

提示和注释

System.GC.GetGeneration 方法 (WeakReference)例子

下面的示例演示如何使用 GetGeneration 方法来确定弱引用对象的存现时期。

using System;

namespace GCGetGenerationWeakExample
{
    public class MyGCCollectClass
    {
        private const long maxGarbage = 1000;

        static void Main()
        {
            // Create a strong reference to an object.
            MyGCCollectClass myGCCol = new MyGCCollectClass();

            // Put some objects in memory.
            myGCCol.MakeSomeGarbage();

            // Get the generation of managed memory where myGCCol is stored.
            Console.WriteLine("The object is in generation: {0}", GC.GetGeneration(myGCCol));
						
            // Perform a full garbage collection.
            // Because there is a strong reference to myGCCol, it will
            // not be garbage collected.
            GC.Collect();
			
            // Get the generation of managed memory where myGCCol is stored.
            Console.WriteLine("The object is in generation: {0}", GC.GetGeneration(myGCCol));
			
            // Create a WeakReference to myGCCol.
            WeakReference wkref = new WeakReference(myGCCol);
            // Remove the strong reference to myGCCol.
            myGCCol = null;

            // Get the generation of managed memory where wkref is stored.
            Console.WriteLine("The WeakReference to the object is in generation: {0}", GC.GetGeneration(wkref));
			
            // Perform another full garbage collection.
            // A WeakReference will not survive a garbage collection.
            GC.Collect();
		
            // Try to get the generation of managed memory where wkref is stored.
            // Because it has been collected, an exception will be thrown.
            try
            {
                Console.WriteLine("The WeakReference to the object is in generation: {0}", GC.GetGeneration(wkref));
                Console.Read();
            }
            catch(Exception e)
            {
                Console.WriteLine("The WeakReference to the object has been garbage collected: '{0}'", e);
                Console.Read();
            }
        }

        void MakeSomeGarbage()
        {
            Version vt;

            for(int i = 0; i < maxGarbage; i++)
            {
                // Create objects and release them to fill up memory
                // with unused objects.
                vt = new Version();
            }
        }
    }
}

异常

异常 异常描述
ArgumentException 已经对 wo 执行了垃圾回收。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。