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 6971787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:53:04+00:00 2026-05-27T16:53:04+00:00

I need to do this:- MyClass.cs using System; using System.Collections.Generic; using System.Text; namespace set_property__Enum_To_Nullable_Enum

  • 0

I need to do this:-

MyClass.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace set_property__Enum_To_Nullable_Enum
{
    public enum MyEnum : short
    {
        One, 
        Two, 
        Three 
    }

    public class MyClass
    {
        public string MyStringProperty { get; set; }
        public MyEnum? MyEnumProperty { get; set; }

        public void ShowAll(string message)
        {
            Console.WriteLine(message);            
            Console.WriteLine("String   = " + MyStringProperty);
            Console.WriteLine("Enum   = " + MyEnumProperty.Value);
        }
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace set_property__Enum_To_Nullable_Enum
{
    class Program
    {
        static void Main(string[] args)
        {
            //Names
            string myAssemblyName = "MyAssembly";
            string myNamespaceName = "set_property__Enum_To_Nullable_Enum";
            string myClassName = "MyClass";
            string propertyNameOne = "MyStringProperty";
            string propertyNameTwo = "MyEnumProperty";

            //Data            
            string myString = "Hello World!";
            short myInteger = 1;

            //Preprocessing
            Assembly myAssmbly = Assembly.Load(myAssemblyName);
            Type myType = myAssmbly.GetType(myNamespaceName + "." + myClassName);

            //Create class-instance
            object objectInstance = Activator.CreateInstance(myType);

            //Set property-values
            PropertyInfo propertyInfoOne = myType.GetProperty(propertyNameOne);
            propertyInfoOne.SetValue(objectInstance, myString, null);

            PropertyInfo propertyInfoTwo = myType.GetProperty(propertyNameTwo);
            propertyInfoTwo.SetValue(objectInstance, myInteger, null);//<---------------

            //Call method
            myType.InvokeMember("ShowAll",
                                        BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
                                        null,
                                        objectInstance,
                                        new object[] { "My name is Khan" });

            string end = string.Empty;
        }
    }
}

But Int32 value is not automatically converted to MyEnum.

At the specific line, an exception is being generated.

Object of type 'System.Int16' cannot be converted to type 'System.Nullable`1[set_property__Enum_To_Nullable_Enum.MyEnum]'.

How to do it?

Edit

I need further help please!

Enum.ToObject() can not handle null.

  • 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-27T16:53:05+00:00Added an answer on May 27, 2026 at 4:53 pm

    OK, you need to use Enum.ToObject in this case, since you’re using reflection. But you also need to unwrap the nullable to use that, which Nullable.GetUnderlyingType does for you.

    So, you need to get the Type corresponding to MyEnum:

    Type nullableEnumType = propertyInfoTwo.PropertyType;
    Type enumType = Nullable.GetUnderlyingType(nullableEnumType);
    

    then use Enum.ToObject to produce a boxed instance of MyEnum with the value you specify:

    object enumValue = Enum.ToObject(enumType, myInteger);
    

    So, putting it all together:

    object enumValue = Enum.ToObject(Nullable.GetUnderlyingType(propertyInfoTwo.PropertyType), myInteger);
    propertyInfoTwo.SetValue(objectInstance, enumValue, null);
    

    EDIT:

    if myInteger is nullable, itself, you should use:

    object enumValue = 
        myInteger.HasValue
            ? Enum.ToObject(Nullable.GetUnderlyingType(propertyInfoTwo.PropertyType), myInteger.Value);
            : null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I need to serialize a generic dictionary like Dictionary<long, List<MyClass>> . I need
I have a class that creates and uses a System.Threading.Timer, something like this: using
Long story short, I need to sort an array of objects using usort, and
I need this for some animation effects and i remember there is a window
I need this for calling a C function from Java class (JNI) and I
I need this because the constructor in the superclass is calling a method which
I need this roadmap of a Hibernate managed object instance. First, I create an
So from this string: name[id] I need this: id I used str.split ('[]'), but
All I need this for is strcpy(). I want to see whether the first
I don't need this declaration because it only makes my code big and unreadable.

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.