jeudi 17 novembre 2016

Access a certain unknown field of a known class using reflection

Say I have the following class:

class Person
{
     public enum firstField { // values }
     public enum secondField { // values }
     // more enums
} 

I want to build a function that accepts a Person class as its first parameter, and the type of one of the first parameter's fields as its second parameter - With the assumption that Person's fields are of distinct types (e.g., it is a class of enums fields).

My aim is to be able to use reflection on the second parameter, and then use some equivalent of a dot notation to access the passed class.

public void SetPersonField(Person person, <should it be Type?> personFieldType)
{
   // accessing person's correct field type
} 

How can it be done?





Aucun commentaire:

Enregistrer un commentaire