A literal
is a const
field. Every const field has its value determined at compile time, by initialization from a literal. Look at this code
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{}
}
}
}
Output:
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
However,
but the field does not have one of the accepted literal types
is open to interpretation and I couldn't find an example for a literal that doesn't have 'one of the accepted literal types' (whatever that means).
By briefly looking at the source code, I couldn't find a relative piece of code to stand for this exception. You should be safe ignoring this clause.