mardi 4 septembre 2018

Loop through all objects in a class at compile time C#

I need to concat all the properties in my class at compile time to build a string. I have seen similar questions, but they were all dealing with runtime scenarios. Basically, I have a thousand properties, and would rather not list them out by hand again. I know about using reflection to use typeof(X).getProperties(...), but I don't wan't any performance hit for doing this at runtime with reflection. Is there a way for me to loop through all the properties in my class to make a string of all my properties?

    Class1 : SomeInterface
    {
            int1;
            int2;
            ...
            int1000;
            string stringOfAllProperties;

            public getAllPropertiesAsString()
            {
               return stringOfAllProperties = int1+int2+int3...;
            }       
     }





Aucun commentaire:

Enregistrer un commentaire