mercredi 16 décembre 2015

Can I call extension method if I have a PropertyInfo and object with variable for this extension?

Can I call extension method, if I have a propertyInfo and object with variable for this extension?

I have an extension:

public static string GetTitle(this MyEnum myEnum)
{
    switch (myEnum)
    {
        case MyEnum.One:
            return "one";
        case MyEnum.Two:
            return "two";
        default:
            return "zero";
    }
}

and enum:

public enum MyEnum
{
  Zero, One, Two
}

and the class

public class MyClass
{
   public string A {get;set;}
   public MyEnum B {get;set;}
}

When I got PropertyInfo of this class, I need to call an extension. I try to do this

// .....
foreach(var prop in properties){
 var value = prop.GetType().IsEnum ? prop.GetTitle() : prop.GetValue(myObj, null).ToString()
 }
// .....

But it doesn't work.

I have a several different enums and several different extensions. And I try to get values regardless of the type.





Aucun commentaire:

Enregistrer un commentaire