System.Reflection GetProperties method not returning values

前端 未结 4 1155
情书的邮戳
情书的邮戳 2020-12-17 08:19

Can some one explain to me why the GetProperties method would not return public values if the class is setup as follows.

public class DocumentA
         


        
4条回答
  •  臣服心动
    2020-12-17 09:15

    I see this thread is already four years old, but none the less I was unsatisfied with the answers provided. OP should note that OP is referring to Fields not Properties. To dynamically reset all fields (expansion proof) try:

    /**
     * method to iterate through Vehicle class fields (dynamic..)
     * resets each field to null
     **/
    public void reset(){
        try{
            Type myType = this.GetType(); //get the type handle of a specified class
            FieldInfo[] myfield = myType.GetFields(); //get the fields of the specified class
            for (int pointer = 0; pointer < myfield.Length ; pointer++){
                myfield[pointer].SetValue(this, null); //takes field from this instance and fills it with null
            }
        }
        catch(Exception e){
            Debug.Log (e.Message); //prints error message to terminal
        }
    }
    

    Note that GetFields() only has access to public fields for obvious reasons.

提交回复
热议问题