本文整理汇总了C#中System.Runtime.CompilerServices.FixedAddressValueTypeAttribute类的典型用法代码示例。如果您正苦于以下问题:C# FixedAddressValueTypeAttribute类的具体用法?C# FixedAddressValueTypeAttribute怎么用?C# FixedAddressValueTypeAttribute使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
FixedAddressValueTypeAttribute类属于System.Runtime.CompilerServices命名空间,在下文中一共展示了FixedAddressValueTypeAttribute类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Runtime.CompilerServices;
public struct Age {
public int years;
public int months;
}
public class FreeClass
{
public static Age FreeAge;
public static unsafe IntPtr AddressOfFreeAge()
{
fixed (Age* pointer = &FreeAge)
{ return (IntPtr) pointer; }
}
}
public class FixedClass
{
[FixedAddressValueType]
public static Age FixedAge;
public static unsafe IntPtr AddressOfFixedAge()
{
fixed (Age* pointer = &FixedAge)
{ return (IntPtr) pointer; }
}
}
public class Example
{
public static void Main()
{
AllocateMemory();
// Get addresses of static Age fields.
IntPtr freePtr1 = FreeClass.AddressOfFreeAge();
AllocateMemory();
IntPtr fixedPtr1 = FixedClass.AddressOfFixedAge();
AllocateMemory();
// Garbage collection.
GC.Collect();
GC.WaitForPendingFinalizers();
// Get addresses of static Age fields after garbage collection.
IntPtr freePtr2 = FreeClass.AddressOfFreeAge();
IntPtr fixedPtr2 = FixedClass.AddressOfFixedAge();
// Display addresses before and after garbage collection
Console.WriteLine("Normal static: {0} -> {1}", freePtr1, freePtr2);
Console.WriteLine("Pinned static: {0} -> {1}", fixedPtr1, fixedPtr2);
}
// Allocate memory for 100,000 objects.
static public void AllocateMemory()
{
for (int ctr = 0; ctr <= 100000; ctr++)
{
object o = new object();
}
}
}
开发者ID:.NET开发者,项目名称:System.Runtime.CompilerServices,代码行数:67,代码来源:FixedAddressValueTypeAttribute 输出:
Normal static: 19932420 -> 19863704
Pinned static: 19985508 -> 19985508
注:本文中的System.Runtime.CompilerServices.FixedAddressValueTypeAttribute类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论