Tag Archives: .Net

Using Reflection to serialize DTO’s

Suppose that you have a DTO (data transfer object) that you want to convert into a parameter array to be saved to file or sent over the web. You could serialize it and convert it to XML or JSON. But maybe you want to send it in an HTTP POST or GET and you don’t want to know anything about the class itself. You could use Reflection to iterate through the properties and extract the property names and values and output them to a string.

For example, this code will convert a class into a string suitable for a REST API call:

var properties = prefs.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
 
           properties.ToList().ForEach(property =>
                                           {
                                               var hasDataMemberAttribute = property.GetCustomAttributes(typeof(DataMemberAttribute), false);
                                               if (hasDataMemberAttribute.Length == 1)
                                               {
                                                   string name = property.Name.ToLower();
                                                   string value = String.Empty;
 
                                                   object objValue = property.GetValue(prefs, null);
                                                   if (null != objValue)
                                                       value = objValue.ToString();
 
                                                   // Only serialize properties marked with the [DataMember] attribute:
                                                   var hasBrokerMapAttribute = property.GetCustomAttributes(typeof(DataMemberBrokerMapAttribute),
                                                                                                            false);
                                                   if (hasBrokerMapAttribute.Length == 1)
                                                   {
                                                       name = ((DataMemberBrokerMapAttribute)hasBrokerMapAttribute[0]).Key;
                                                   }
 
                                                   if (value.Length > 0)
                                                   {
                                                       filter.Append(String.Concat("/", name, "=", value));
                                                   }
                                               }
 
                                           });
 
           Debug.WriteLine("Search Filter:" + filter);
           return filter.ToString();

AES interoperability between .Net and iPhone

One of my projects requires encrypting data on the iPhone and decrypting it using .Net. This is easy to do with the Common Crypto library in the iPhone SDK and the AesCryptoServiceProvider class in .Net, but the encryption parameters have to be the same for it to work.

I couldn’t figure it out, but the geniuses at StackOverflow did, so I am posting my results here. The zip file includes a basic iPhone app and a .Net console project with helpful classes to do the encryption/decryption and base64 conversion. I didn’t write most of the code – thanks to Blue Beetle for the .Net code and Greg Haygood for the Objective C.

Download zip.

Customizing Terminal when compiling Mono apps

Pkg-config is a helper tool used when compiling applications and libraries.  If you want to build Mono apps from source using configuration scripts, you will need to put the Mono.pc path in your PKG_CONFIG_PATH environment variable.  If it’s not set, you will get an error like “configure: error: missing the mono.pc file, usually found in the mono-devel package” or “Failed to initialize the ‘Mono 3.5 Profile’ (mono-3.5) target framework”

Here’s how to customize your terminal prompt.   To add the location of mono.pc, edit .profile or .bashrc in the root of your home folder, and add this line

export PKG_CONFIG_PATH=/Library/Frameworks/Mono.framework/Versions/Current/lib/pkgconfig/"

Here is my full .profile file:

export PATH=/Applications/Windows/Darwine/Wine.bundle/Contents/bin:/opt/local/bin:/opt/local/sbin:$PATH
export PKG_CONFIG_PATH=/Library/Frameworks/Mono.framework/Versions/Current/lib/pkgconfig/
export DISPLAY=:0.0

(When using MacPorts, the path is /opt/local/lib/pkgconfig/)