4

According to Microsoft's documentation, FieldInfo.GetValue(object) will throw a NotSupportedException if:

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

I have no idea what it means to

"mark a field as literal."

I want to understand this so that I may know how to guard against this exception.

2
  • 1
    Do you mean FieldInfo.GetValue(object) ?
    – thehennyy
    Commented Sep 25, 2015 at 18:12
  • Yes, thehennyy, I mean FieldInfo.GetValue(object). Thanks for helping me correct my mistake. Commented Sep 25, 2015 at 18:39

3 Answers 3

3

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.

2
  • Thanks, Mark. I've been reading StackOverflow for years, but only today have I now become an active participant. I'm amazed how just how quickly and how thoroughly my question has been answered. As a participant, I have a lot to live up to. A +1 for linking to the .Net source code. Commented Sep 25, 2015 at 22:20
  • @RockAnthonyJohnson Always happy to help :)
    – Mark Segal
    Commented Sep 26, 2015 at 13:57
2

Have a look at the FieldInfo.IsLiteral Property:

Gets a value indicating whether the value is written at compile time and cannot be changed.

The IsLiteral property is set when the FieldAttributes.Literal attribute is set. If this attribute is set, the field cannot be changed and is constant.

2
  • Thanks, thehennyy. I now know what it means to "mark a field as literal." Commented Sep 25, 2015 at 18:52
  • 1
    This doesn't answer OPs question.
    – Mark Segal
    Commented Sep 25, 2015 at 19:06
0

The exception stated by

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

... have to be related to FieldInfo instances created by dynamic or custom code (rather than gathered through Reflection), where often certain validations are delayed or omitted.

A FieldInfo object contains meta-data that represents a field, but its class is inheritable and any derived implementation could potentially allow the bad type condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.