gpt4 book ai didi

reflection - .NET 世界中的 "mark a field as literal"是什么意思?

转载 作者:行者123 更新时间:2023-12-04 18:53:08 24 4
gpt4 key购买 nike

根据微软的文档,FieldInfo.GetValue(object)会抛出 NotSupportedException如果:

"A field is marked literal, but the field does not have one of the accepted literal types."



我不知道这意味着什么

"mark a field as literal."



我想了解这一点,以便我知道如何防范这种异常。

最佳答案

一个 literalconst field 。每个 const 字段的值都在编译时通过从文字初始化来确定。看看这个代码

using System;
using System.Reflection;


public class Program
{
const int literal_int = 5;
readonly int literal_int_two = 5;
const string literal_string = "Fun";
const Random literal_random = null;
int non_literal;

public static void Main()
{
foreach (FieldInfo f in typeof(Program).GetFields(BindingFlags.Instance
| BindingFlags.NonPublic
| BindingFlags.Static
| BindingFlags.FlattenHierarchy))
{
Console.WriteLine("{0} is literal - {1}", f.Name, f.IsLiteral);
try
{
Console.WriteLine("GetValue = {0}", f.GetValue(null));
}
catch{}
}
}
}

输出:
literal_int is literal - True
GetValue = 5
literal_int_two is literal - False
literal_string is literal - True
GetValue = Fun
literal_random is literal - True
GetValue =
non_literal is literal - False

然而,

but the field does not have one of the accepted literal types



可以接受解释,我找不到一个没有“一种可接受的文字类型”的文字示例(无论这意味着什么)。

通过简要查看 source code ,我找不到一个相关的代码来代表这个异常。您应该安全地忽略此条款。

关于reflection - .NET 世界中的 "mark a field as literal"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32788026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com