jeudi 13 août 2020

Get Value from Override using Reflection

I want to get the value from a virtual property from a inherited class see my code below:

Base class:

public class TestBaseClass
{
   public virtual int Index { get; }
}

Inherited class:

public class TestInheritedClass : TestBaseClass
{
   public override int Index => 100; // I want to get this value using reflection
}

My Code:

static void Main(string[] args) 
{
   var assemblies = Assembly.LoadFile(args[0]);
   foreach(var type in assembly.ExportedTypes)
   {
      if(type.IsSubclassOf(typeof(TestBaseClass))
      {
         foreach(var prop in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
         {
            var value = prop.GetValue(typeof(int), null); // I expect to return 100 from override Index value
         }
      }
   }
}

Did I missed something or did I made this all wrong? I'm trying to get the value 100 from the virtual property. Is there a way to get the value?





Aucun commentaire:

Enregistrer un commentaire