lundi 15 octobre 2018

Dynamically reading class

so I have came across making few programs, that build specific XMLs with data from database.

Now i would like to make application, which I could just hang over structured class with values and it would be able to dynamically read it.

My idea of that class is something like this

public class Order
{
    public string PO_NO { get; set; }
    ................
    public List<OrderDetails> OrderDetails = new List<OrderDetails>();
}

and XML would go out as follows

<ORDER>

<PO_NO>18060001</PO_NO>
....
   -<ORDER_DETAILS>

      -<ORDER_DETAIL>
      </ORDER_DETAIL>     
      -<ORDER_DETAIL>
      </ORDER_DETAIL>
    ...
   </ORDER_DETAILS>
-</ORDER>

I hope this covers the structure and the idea.

So far I am struggling on dynamically reading the class.

The last and I quess the closest attempt is this:

foreach (var refer in DatabaseData.getRefs()) //just gets reference numbers for me, for further reading
            {
//THIS one returns the filled class. Eg. the Order class explained higher.
                var MyClass = DatabaseData.read(refer); 
                var fiel = DatabaseData.read(refer).GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                foreach (var field in fiel)
                {
                    var fieldName = field.Name.Replace('<', ' ').Replace('>', ' ').Replace("__", null).Replace("kBackingField", null);

                    if (field.FieldType.ToString().Contains("List"))
                        Debug.WriteLine("its a list");
                    else
                        Debug.WriteLine((string)DatabaseData.read(refer).GetType().GetField(fieldName).GetValue(Activator.CreateInstance(DatabaseData.read(refer).GetType())));
                }
            }

This is the part that troubles me. I have tried many variations of this I found on forums, but I am still getting the. Object reference not set to instance of an object.

(string)DatabaseData.read(refer).GetType().GetField(fieldName).GetValue(Activator.CreateInstance(DatabaseData.read(refer).GetType()))

Any ideas please? Or point me the right way.

Thanks in advance.





Aucun commentaire:

Enregistrer un commentaire