Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6853611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:31:58+00:00 2026-05-27T01:31:58+00:00

In an application I have some code that has a FieldInfo for a Nullable<int>

  • 0

In an application I have some code that has a FieldInfo for a Nullable<int> and I need to retrieve the nullable value (not the underlying value) like in the sample bellow:

class Test
{
    public int? value;
}

public class Program
{
    static void Main(string[] args)
    {
        var obj = new Test { value = 10 };
        var fld = typeof (Test).GetField("value");
        var v = fld.GetValue(obj);

        System.Diagnostics.Debug.WriteLine(v.GetType().FullName);
        System.Diagnostics.Debug.WriteLine(fld.FieldType.FullName);
     }
 }

My problem is that v is always assigned the underlying value (in this sample an int) instead the nullable (in this sample a Nullable<int>).

PS: The real application don’t has the type of the nullable at compile time, so a cast is not possible.

Thanks in advance for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:31:59+00:00Added an answer on May 27, 2026 at 1:31 am

    In this case v is of type object. If value is null then v will be null; if value is some integer then v will be that integer. If you want v to actually have the type Nullable<int> then you have to declare it as such: var v = (int?) fld.GetValue(obj);.

    If you need to be able to reference v.Value and get the boxed value back, you’ll probably have to record the fact that fld was nullable (Nullable.GetUnderlyingType(fld.FieldType) != null). Note that generics will not help you here because you do not know the T at compile time.

    Here’s a helper you can use:

    struct NullableObject
    {
        public object Value { get; private set; }
    
        public static object GetField(object Target, FieldInfo Field)
        {
            object value = Field.GetValue(Target);
            if (Nullable.GetUnderlyingType(Field.FieldType) != null)
                return new NullableObject { Value = value };
            return value;
        }
    }
    
    public static class NullableHelper
    {
        public static object GetNullableValue(this FieldInfo field, object target)
        {
            return NullableObject.GetField(target, field);
        }
    }
    

    Then instead of calling var v = fld.GetValue(obj);, say var v = fld.GetNullableValue(obj);. If fld represents a Nullable type, you’ll get an object with a Value property; if not, you’ll just get the value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a console application that require to use some code that need administrator
I have some code in my application that looks something like this: char *hash
I have some code that attempts to test whether my application is running with
I currently have some code that will produce a crash dump when my application
I have some VBA code that needs to talk to a running c# application.
I have some old code (an old but still maintained VB6 application) that from
I have to modify some code in a application I am working on that
I have an application written in C# that invokes some C code as well.
I have some code that locates all the contact folders that a user has
I have some legacy code that needs to be used in a new application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.