2

I have a class with custom enum:

public enum Capabilities{
 PowerSave= 1,
 PnP =2,
 Shared=3, }

My class

public class Device
{
       ....
  public Capabilities[] DeviceCapabilities
  {
     get { // logic goes here}
  }

Is there a way using reflection to get the value of this field during runtime? I tried the following but got null reference exception

PropertyInfo[] prs = srcObj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
 foreach (PropertyInfo property in prs)
 {
     if (property.PropertyType.IsArray)
     {
         Array a = (Array)property.GetValue(srcObj, null);
     }    
 }

EDIT: Thanks for your answers, what I really need is a way to get the values dynamically without the need to specify the enum type. Something like:

string enumType = "enumtype"
var property = typeof(Device).GetProperty(enumType);

Is that possible?

3
  • what do you mean by get values of this field? Simply read that array and do what you want with it Commented Jan 9, 2013 at 17:08
  • 1
    Sounds like [Flags] is appropriate here: msdn.microsoft.com/en-us/library/system.flagsattribute.aspx Commented Jan 9, 2013 at 17:09
  • Do you have a stack trace to verify where the NullReferenceException is coming from? It looks like it may be coming from the logic in your DeviceCapabilities property, or from another property in your object. Commented Jan 9, 2013 at 17:15

4 Answers 4

1

The following should do what you desire.

var property = typeof(Device).GetProperty("DeviceCapabilities");

var deviceCapabilities = (Capabilities[])property.GetValue(device);

Note that the method Object PropertyInfo.GetValue(Object) is new in .NET 4.5. In previous versions you have to add an additional argument for the indices.

var deviceCapabilities = (Capabilities[])property.GetValue(device, null);
Sign up to request clarification or add additional context in comments.

Comments

0

This should work:

    var source = new Device();

    var property = source.GetType().GetProperty("DeviceCapabilities");
    var caps = (Array)property.GetValue(source, null);

    foreach (var cap in caps)
        Console.WriteLine(cap);

Comments

0

If you want to enumerate all the possible values of an Enum and return as an array then try this helper function:

public class EnumHelper {
    public static IEnumerable<T> GetValues<T>()
    {
        return Enum.GetValues(typeof(T)).Cast<T>();
    }
}

Then you can simply call:

Capabilities[] array = EnumHelper.GetValues<Capabilities>();

If that isn't what you are after then I'm not sure what you mean.

Comments

0

You can try this

foreach (PropertyInfo property in prs)
{
    string[] enumValues = Enum.GetNames(property.PropertyType);
}

Hope it helps.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.