Tuesday, 15 January 2013

print all variables in class C# -


To start I will not want to use the reflection to complete it.

I have a class that says

  public class exampleClass {public string var1 = "one"; Public string var2 = "two"; Public int var3 = 3; Public string var4 = "four"; Etc. etc.}   

I want to be able to iterate through the orbit dynamically and print the variable. I thought about the serialization, but it was not certain how to implement it for the matter (just the example that I could get was XML and I did not want this), I really do not want to change the structure of the orbit Either way

This is why I am doing this because I am creating an HTML table and want to:

 for  (int i = 0 ; I & lt; exampleClass calculation; i ++) tbl_row = "& lt; td>; + Example class [i] + "";   

Or something similar. Any suggestions?

The easiest way to use will be reflection that should be enough:

  var example = new example class (); Var allPublicFields = example.GetType () GetFields (BindingFlags.Public | BindingFlags.Instance);   

Use a dictionary instead of the field: dictionary , but it is simple more Engineering Simple one streghtforward solution: Reflection on the clear and maintenance structure of your strong typed class.

No comments:

Post a Comment