How to use anonymous object to extends an object property for the instance of 1 method call.<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to use anonymous object to extends an object property for the instance of 1 method call.

Posted on: March 9, 2012

I had to use a method that with reflection would take the properties name and value to create a SQL query. The problem is that the library was limited to methods directly inside the object passed and not value of inner object. For example, if the class A contain an object B, we were not able to access B properties.

We could have done something pretty much nicer like looping through all objects to get desired sub-properties but to make it short and because we wanted just to have the unique identifier of those object that we decided to overload the method of the library to extend the properties for the time of this method call.

1MyMethod(myObject, new {
2 MyNewPersonalMethodName1 = myObject.SubObject.Id1,
3 MyNewPersonalMethodName2 = myObject.SubObject.Id2
4 });
5 //or MyMethod(myObject, myOtherObjectToUseProperties);

This is a representation of how it was done. The MyMethod was overloaded to accept a second parameter. This one will be used to receive an anonymous object. This way, it's easy to have a name-value relationship. It gives us the possibility to pass a complete real object if desired or simply pass an anonymous object as parameter.

1public void MyMethod(object myObject, object extendedProperties) {
2 //You can do something with all properties of myObject and its value here
3 //Here you can do something with the extended properties
4 if (extendedProperties != null) {
5 var typeInfo = extendedProperties.GetType();
6 var propertyInfo = typeInfo.GetProperties();
7 foreach (PropertyInfo p in propertyInfo) {
8 var nameToUpdate = p.Name;
9 var valueToUpdate = (object)p.GetValue(extendedProperties, null);
10 //Do something with nameToUpdate and valueToUpdate
11 }
12 }

The next step is to use reflection to be able to get all properties' names and values. It's after that easy to be able to get the name to build a SQL query or a JSON object from object.