.NET 8 - Set enum value with reflection

M74

Member
Joined
Jan 8, 2025
Messages
7
Programming Experience
10+
Hello,

I don't understand how to set enum values with reflections.
Questions:
1) How do I set the enum and then call the constructor with reflections?

Attachement:
    Attachement attachement = new Attachement("Test.hex", AttachementContentType.Unspecified, new byte[] { 97, 98, 99, 100 });

2) Is there any "Reflection parser tool", that generates reflections calls based on a existing dll? (I hope you understand what I mean)

Thank you for your help.
BR
 
Last edited:
Solution
Thank you. With this it's working
C#:
using ReflectionEnumTest;

var tc1refl = typeof(TestclassWithEnum);
var te1refl = typeof(TestEnum);
var tc2inst = Activator.CreateInstance(tc1refl, new object[] { "p12", te1refl.GetField("two").GetValue(null), "p32" });

Console.ReadLine();
Reflection pretty much revolves around the Type class. You get a Type object that represents your data type in whatever way is most appropriate under the circumstances. That may be calling GetType on an object reference, using the typeof operator or something else. Once you have that Type object, it can give you all sorts of information about that data type, e.g. call GetConstructor or GetConstructors to get ConstructorInfo objects that you can use to construct instances of that type. There are similar FieldInfo, PropertyInfo and MethodInfo types to access other members. I suggest that you do some reading on the Type class, starting with the link I provided to the documentation, and get a bit familiar with it that way. If you still can't figure out the best way to accomplish your aim, please provide more specific details about your scenario and why exactly you're using Reflection in the first place.

As for the enum, that's not something I've ever done but my first guess would be that you would create a Type object for your enum type and then call its GetField method and then GetValue on the resulting FieldInfo. If you try that and it doesn't work, post back and I can investigate further or someone else may already know what to do.
 
Thank you for the answer. The basics of Reflection I know, but I have questions with enums.

Here I prepared an easy example.

Class "TestclassWithEnum.cs"
C#:
namespace ReflectionEnumTest
{
  public enum TestEnum
  {
    one,
    two,
    three
  }


  public class TestclassWithEnum
  {
    public string P1 { get; set; }
    public TestEnum P2 { get; set; }
    public string P3 { get; set; }


    public TestclassWithEnum()
    { }


    public TestclassWithEnum(string p1, TestEnum p2, string p3) : this()
    {
      this.P1 = p1;
      this.P2 = p2;
      this.P3 = p3;
    }
  }
}

Console application "program.cs":
C#:
using ReflectionEnumTest;

// Conventional instances
TestclassWithEnum tc1 = new TestclassWithEnum("p11", TestEnum.two, "p31");
TestEnum te1 = TestEnum.one;

// Reflection types
var tc1refl = tc1.GetType();
var te1refl = te1.GetType();


// Instance of the enum WITH REFLECTION
var te2inst = Activator.CreateInstance(te1refl);
var te2fieldTwo = te1refl.GetField("two");
// TODO set the enum value WITH REFLECTION

// Instance of TestClass WITH REFLECTION
var tc2inst = Activator.CreateInstance(tc1refl, new object[] { "p12", te2inst, "p32"});

Console.ReadLine();

The question is how to set the enum value by reflection (See "TODO")
Thank you for your help.
BR
 
You should not be creating an instance of your enum. You seem to have simply ignored what I posted previously. Firstly, using GetType to get a Type for TestEnum is silly. You should be using typeof. Once you have a Type, calling GetField gives you a FieldInfo. As I've already stated, you then need to call GetValue on that FieldInfo to get a value that you can use. When calling GetField, you may have to stipulate that it's a static field you want. If I remember correctly, when calling GetValue to get the value for a static field, you specify null for the instance to get the value from.
 
Thank you. With this it's working
C#:
using ReflectionEnumTest;

var tc1refl = typeof(TestclassWithEnum);
var te1refl = typeof(TestEnum);
var tc2inst = Activator.CreateInstance(tc1refl, new object[] { "p12", te1refl.GetField("two").GetValue(null), "p32" });

Console.ReadLine();
 
Solution
Back
Top Bottom