Getting String Value for All Object's Properties

In last post I wen’t through task of restoring properties from their textual representation. But how did we end up with text?

It is as simple as loop through all public properties and then using TypeConverter in order to properly convert a value to it’s string representation. Do notice that ConvertToInvariantString is preferred over ConvertToString in order for code to properly work on non-USA Windows.

public static IDictionary<string, string> GetPairs(object objectInstance) {
    var result = new Dictionary<string, string>();
    foreach (var propertyInfo in objectInstance.GetType().GetProperties()) {  //loops through all public properties
        var propertyConverter = TypeDescriptor.GetConverter(propertyInfo.PropertyType);  //gets converter for property
        var stringValue = propertyConverter.ConvertToInvariantString(propertyInfo.GetValue(objectInstance, null));  //converts value to string
        result.Add(propertyInfo.Name, stringValue);
    }
    return result;
}

P.S. Saving key/value pairs to file is not shown here… guess how it is done… :)

Setting a Value From String

It all started with list of key value pairs from file. Each key was some property on object that needed to be set and value was obviously value for that property. Since there was no type information in file, I had to use reflection in order to set value. And then problem hit me. Reflection would not sort out my problem with converting string value to proper type.

To solve it, I just loop through all key/value pairs and find property with that name in my object’s instance. Once property is found, I get a magic thing called TypeConverter. TypeConverter enables conversion from string to proper type that can be used in standard reflection SetValue call. And thus problem is solved. Code follows:

foreach (var item in pairs) {                                                                         //go through all key/value pairs
    var propertyInfo = instance.GetType().GetProperty(item.Key);                                      //find property with same name
    Trace.Assert(propertyInfo != null);                                                               //we must have this property
    var propertyConverter = TypeDescriptor.GetConverter(propertyInfo.PropertyType);                   //lets find proper converter.
    Trace.Assert(propertyConverter != null);                                                          //we must have converter
    propertyInfo.SetValue(instance, propertyConverter.ConvertFromInvariantString(item.Value), null);  //set value
}

Microsoft’s All In One Code Framework

Browsing through one Belgian guy’s blog I found news about Microsoft’s All In One Code Framework.

In short, it is a site where you should be able to find best practice code samples. You can even make a request for some sample that you need. Of course, not all requests will be granted but interesting and much sought code should appear there eventually.

Of course, anybody can upload their code sample for any request and thus take some load off poor Microsoft guy who got writing samples as a task. :)

It is interesting idea, but let’s wait whether usefulness/noise will be better than rest of Internet.

One Man's Feature, Another Man's Annoyance

Being programmer I need to do send executable files a lot. However my mail client, Google Mail, does not like this. Since it check for executable files is done even in zipped archives, easiest solution for me was to use my trusty WinRAR. Google had no idea that .exe was hidden inside it.

Not any more. Google now supports RAR archives also. While that does help with preview, it also enables Google to check my RAR files for executables. And to annoy me with “Our system detected an illegal attachment on your message.”

Now only way to send executables is to change archive extension to e.g. rarx… Annoyances…

Lenovo App Store

I have quite a few freeware applications out there. Some are for Windows, some are for Windows Mobile and last few were for Android. Occasionally I get some offer related to them. Something like this (direct copy of mail, excessive white-space removed):

I am Jessie, strategic alliance manager of Lenovo App Store (http://www.lenovomm.com/appstore/). I’m writing to seek opportunities for introducing your Mobile/PAD applications to lenovo app store, which is a premium brand and good distribution channel in China market.

If you are interested, the process is simple: 1) Pls sign up here: http://developer.lenovomm.com/developeren/index.jsp. 2) You can simply send us your APKs and authorize us to upload your apps on your behalf. 3) Your apps will be put on shelf after passing test work.

If you are not interested in cooperation now, the reasons are appreciated!

Feel free to contact us if you have any questions.

Best regards, Jessie Strategic Alliance Manager Lenovo Mobile Internet and Digital Home Business Group

This email communication is confidential. Recipient(s) named above is (are) obligated to maintain secrecy.

As I said, this wasn’t that unusual so I went ahead through registration. After e-mail confirmation I got form where I was expected to enter my bank’s name, account number and to give them copy of id card or my passport!

While I have no idea whether this page is scam or not, it does not look good. Why would any normal company ask me for those details? Even if they don’t have any fraud on mind, can I trust them never to be hacked? Can I trust that my data will never be abused?

Frankly, I am scared more if this is valid company. For any company to nonchalantly ask this sort of questions is unsettling…

P.S. Here is form that I was asked to fill:

Illustration

P.P.S. Upon asking, I got instructions to lie to the system and insert fake bank account data. However, I still need to add ID. Yes, I am probably paranoid, but I will not do that…