reflection - What does it mean to "mark a field as literal" in the world of .NET? -


according microsoft's documentation, fieldinfo.getvalue(object) throw notsupportedexception if:

"a field marked literal, field not have 1 of accepted literal types."

i have no idea means

"mark field literal."

i want understand may know how guard against exception.

a literal const field. every const field has value determined @ compile time, initialization literal. @ 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} literal - {1}", f.name, f.isliteral);             try             {                 console.writeline("getvalue = {0}", f.getvalue(null));             }             catch{}         }     } } 

output:

literal_int literal - true getvalue = 5 literal_int_two literal - false literal_string literal - true getvalue = fun literal_random literal - true getvalue =  non_literal literal - false 

however,

but field not have 1 of accepted literal types

is open interpretation , couldn't find example literal doesn't have 'one of accepted literal types' (whatever means).

by briefly looking @ source code, couldn't find relative piece of code stand exception. should safe ignoring clause.


Comments