c#

C# Updating an object with a dictionary<string, string> value

This is more of an exploratory entry than a solution to a problem. Today I wrote this code to handle updating a C# object from a Dictionary. If the object had a key, then it should update its value. I ran into trouble because the object had nullable values, and this tripped the Converter object. I tried a few times, but the clock was ticking away, so I had to find another solution.

At his point I remembered about UpdateModel(). If you are using .NET MVC, you don't need to use this. There is already the handy UpdateModel() function that will do this work for you. So if you have this problem in MVC, use UpdateModel().

At the end of the day, I came back to this problem because I knew that there was a solution, since UpdateModel() does the same function, and I wanted to know how it was done. So I attempted a number of solutions to how to convert a string to a nullable value. At some point I found the solution below in Stack Overflow:

     using System.ComponentModel;
 
    public partial class Memo
    {
        public void Update(Dictionary<string,string> collection)
        {
            var properties = TypeDescriptor.GetProperties(this);
 
                foreach (PropertyDescriptor property in properties)
                {
                    if (collection.Contains(property.Name))
                    {
                        var value = collection[property.Name];
                        property.SetValue(this, property.Converter.ConvertFromInvariantString(value));
                    }
                }
            }
 
        }
    }

The solution was the PropertyDescriptor and its converter. This worked. I should learn more about this object in the future.

Source:

On using PropertyDescriptorCollection
http://stackoverflow.com/questions/446522/how-to-set-nullable-type-via-r...

To regenerate an aspx.designer.cs file

Okay, here is one that gets me all the time, and which I have to look for the solution all the time.

To regenerate the designer page
1. Delete your bogus deisgner.cs page
2. Right click on the aspx file, and select Convert to Web Application

The solution is found in the link below, by John Q. Smith

http://social.msdn.microsoft.com/forums/en-US/csharpide/thread/128202d7-...

Syndicate content