-6

How do I Make an object in c# that I can loop through its fields?(Or if someone have an idea how to sync an object with a database table I will love to hear it)

O.Deitch
  • 3
  • 2
  • 6
    See http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class – WraithNath Apr 04 '16 at 10:29
  • 1
    Look up "reflection", though you don't actually need that to sync an object with a database if you're using something like Linq. – ChrisF Apr 04 '16 at 10:30
  • 5
    What you seem to need is an ORM (Object Relationnal Mapper), there are plenty of them : Entity Framework, Dapper, NHibernate... – Sam Bauwens Apr 04 '16 at 10:30

1 Answers1

0

To sync your model (obect) with a database you can use an ORM suck as - EntityFramework Code First approach, its really easy and get you going fast with zero configuration, you have other optons like NHibernate and Link2Sql but none of them is as good and easy as EntityFramework. you can read more about it here - https://msdn.microsoft.com/en-us/data/jj590134

to iterate through object propeties you can use this code -

public void IterateProperties(object foo)
{
    Type type = foo.GetType();

    // public properties
    foreach (PropertyInfo propertyInfo in type.GetProperties())
    {
        if (propertyInfo.CanRead)
        {
            object fooPropertyValue = propertyInfo.GetValue(foo, null);
        }
    }
}

hope this helps.

Ran Sasportas
  • 2,256
  • 1
  • 14
  • 21