title: C# - how to loop through each property & value of an object
body: <p>I have below <code>DTO</code> class  in <code>C#</code></p>
  
  <pre><code>public class Customer
  {
     public int Id { get; set;}
  
     public string Name { get;set;}
  
     // &amp; so on (say 25 properties)
  }
  </code></pre>
  
  <p>I am using <code>DynmoDb</code> as Db(NoSql), to add the above item to DynamoDb table. This is how my method looks like</p>
  
  <pre><code> public async Task&lt;bool&gt; Add(Customer customer)
      {
          try
          {
              var res = await _dynamoClient.PutItemAsync(
              tableName: _tableName, item: SetObject(customer)).ConfigureAwait(false);
              return res.HttpStatusCode == System.Net.HttpStatusCode.OK
          }
          catch (Exception ex)
          {
              Logger.Log(ex, eventObj);
              return false;
          }
      }
  </code></pre>
  
  <p>Private SetObject() method:-</p>
  
  <pre><code>private Dictionary&lt;string, AttributeValue&gt; SetObject(Customer obj)
      {
          //DynamoDb - Using Low Level API
          var attributes = new Dictionary&lt;string,
          AttributeValue&gt; {
              //Id
              {
                  nameof(obj.Id),
                  new AttributeValue {
                      S = obj.Id
                  }
              },
              //Name
              {
                  nameof(obj.Name),
                  new AttributeValue {
                      S = obj.Name.Trim()
                  }
              },
          };
          return attributes;
      }
  </code></pre>
  
  <p>This private method looks cumbersome &amp; error prone to me. I don't want to look up through each property one by one in this way.</p>
  
  <p>I am thinking if there is some way to loop through all the properties &amp; assign <code>name</code> &amp; <code>value</code> to <code>Dictionary</code> collection</p>
  
  <pre><code> var propsList = typeof(EventTO).GetProperties().Select(x =&gt; x.Name).ToList&lt;string&gt;();
  </code></pre>
  
  <p>This gives me all the property names but <strong>how do I fetch the value &amp; key from the passed object of type customer.</strong></p>
  
  <p>Thanks!</p>