【问题标题】:what is "fieldof()" method in c#什么是 C# 中的“fieldof()”方法
【发布时间】:2026-02-17 20:00:01
【问题描述】:

我在dnspy中反编译了一些unity dll文件 并得到了这条线

RuntimeHelpers.InitializeArray(array, fieldof(<PrivateImplementationDetails>.51A7A390CD6DE245186881400B18C9D822EFE240).FieldHandle);

我需要知道我以前没见过的那一行中的 fieldof() 函数(因为我是初学者)

以及为什么它在该行显示错误的两个原因

【问题讨论】:

  • C# 中没有这样的语法。它是由您的反编译器生成的。大概意思是“获取该字段的FieldInfo”。
  • @Sweeper 那么你能告诉我我上传的那行代码没有错误的c#代码是什么
  • 这有帮助吗? Elegant infoof operators in C#
  • @Abra 是的。非常感谢!如果你不想,你可以把它作为这个问题的答案发布

标签: c# field


【解决方案1】:

在 MSIL(C# 代码(和一堆其他语言)编译成的中间语言)中,有一个方便的 fieldof 运算符,它可以获取字段的 FieldInfo。但是,fieldof 在 C# 中不存在。

在 C# 中,您需要执行以下操作:

var type = typeof(EnclosingClass); // "EnclosingClass" is the class this code is in
// assuming PrivateImplementationDetails is private
var fieldInfo = type.GetField("PrivateImplementationDetails", BindingFlags.NonPublic);
RuntimeHelpers.InitializeArray(array, fieldInfo.FieldHandle);

【讨论】:

  • 感谢您的回答,需要一点帮助
  • 私有静态 RenderTextureFormat GetPreferredRenderTextureFormat() { RenderTextureFormat[] array = new RenderTextureFormat[3]; var type = typeof(RenderTextureFormat); var fieldInfo = type.GetField("PrivateImplementationDetail", BindingFlags.NonPublic); RuntimeHelpers.InitializeArray(array, fieldInfo.FieldHandle); foreach (RenderTextureFormat renderTextureFormat in array) {` if (SystemInfo.SupportsRenderTextureFormat(renderTextureFormat)) return renderTextureFormat; } 返回 RenderTextureFormat.Default; }
  • 在这段代码中,我是否正确地将对象赋给了 typeof 函数?
  • 不,进一步查找文件。找到GetPreferredRenderTextureFormat 所在的RenderTextureFormat 是方法返回类型(它可能同时是封闭类,但我无法仅凭此代码判断)。
  • 哦,好的,再次感谢答案中的代码,它对我有很大帮助
【解决方案2】:

我建议从 C# 8.0 开始阅读以下内容,了解每个 C# 版本的新功能。

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8

在早期,我们习惯于实现如下属性:

private string userName;
public string UserName
{
    get { return userName; }
    set { userName = value; }
}

因此,当您反编译具有该属性的类的程序集时,您会看到与此完全一样的反编译器输出。

从 C# 2.0 开始,并且一直在改进,现在有多种方法可以在 C# 中实现属性。

public string StringProperty1 => "String Property Value";

public string StringProperty2 { get; private set; }

public ICollection<double> Grades { get; } = new List<double>();

这里有什么共同点?

它们没有可读取或写入的字段。此类声明的字段由编译器创建并存储在名为PrivateImplementationDetails 的结构中。这不必是一个单独的字段。这只是运行时访问属性的自动生成的私有支持字段的方式。

例如,对于名为 AProperty 的 int[] 属性,将生成以下 IL:

.field private int32[] '<AProperty>k__BackingField'
.custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = ( 01 00 00 00 ) 

当您查看设置此属性的此类的构造函数 IL 时,您会看到它正在访问(支持)字段,如下所示:

  IL_0009:  ldc.i4.3
  IL_000a:  newarr     [mscorlib]System.Int32
  IL_000f:  dup
  IL_0010:  ldtoken    field valuetype '<PrivateImplementationDetails>{3CA49917-EFBC-4E01-A884-1CFF6283A97C}'/'__StaticArrayInitTypeSize=12' '<PrivateImplementationDetails>{3CA49917-EFBC-4E01-A884-1CFF6283A97C}'::'$$method0x6000029-1'
  IL_0015:  call       void [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(class [mscorlib]System.Array,

最后,您在反编译器输出中看到的意思是,它正在设置私有自动生成的字段。

【讨论】: