Monday, March 18, 2013

Convert an Anonymous type object to a dictionary

I wanted to write a method that would take an anonymous object and have it spit out key,value pairs from the properties defined on that anonymous object.

For example:

new {j="j1",k="k1"}

Should create a dictionary with 2 keys (j,k) which have corresponding values of (j1,k1).

A while back I had written about an helper method that would convert all the properties on an object into a dictionary: http://blog.aggregatedintelligence.com/2013/01/convert-objects-property-values-to.html

It turns out that the same method can be used even for anonymous types!

Monday, March 11, 2013

Did you know–XOR swap algorithm?

Did you know that you can use the XOR operator to swap the values in 2 variables without the use of a 3rd variable?

Check it out:

int x = 111;
int y = 3333;

x ^= y;
y ^= x;
x ^= y;

x.Dump();
y.Dump();

Outputs:

3333
111

Sunday, March 10, 2013

MemoryStream exception–Memory stream is not expandable

Just got hit by this exception.

I was creating a memory stream from bytes that I had read from a file:

var fileBytes = File.ReadAllBytes(filePath);
MemoryStream ms = new MemoryStream(fileBytes, true);

The memory stream was then being used in a bunch of operations that could add data and when it did, it caused the “Memory stream is not expandable” exception to be thrown.

Found out that its because of the constructor that I was using. Because I was providing memory-stream the actual bytes, it was creating the memory stream as a non-expandable one. Instead, if you construct it and then write to it, the memory stream has no such restriction and it can expand.

MemoryStream ms = new MemoryStream();
ms.Write(fileBytes, 0, fileBytes.Length);

Monday, March 04, 2013

Dilbert–Gratuitous displays of mental superiority!

but I try not to frighten ordinary people with any gratuitous displays of mental superiority

UnityBootstrapper–Order in which methods are called

The MSDN documentation page “Initializing Prism Applications” shows the basic stages of the bootstrapping process, but it leaves out when some of the methods that can be overriden get called.

Process as defined in MSDN:

Gg430868.544A7BF3FF93BE95A370A3D97774244A(en-us,PandP.40).png

 

Here is the order in which all methods that can be overridden are called:

  1. Run (called typically when app is starting up)
  2. CreateLogger
  3. CreateModuleCatalog
  4. ConfigureModuleCatalog
  5. CreateContainer
  6. ConfigureContainer
  7. ConfigureServiceLocator
  8. ConfigureRegionAdapterMappings
  9. ConfigureDefaultRegionBehaviors
  10. RegisterFrameworkExceptionTypes
  11. Create Shell
  12. InitializeShell
  13. InitializeModules

And here is logging output of the Prism bootstrapper process (where UnityLoggerFacade is a logging class that I created):

UnityLoggerFacade              - Logger was created successfully.
UnityLoggerFacade              - Creating module catalog.
UnityLoggerFacade              - Configuring module catalog.
UnityLoggerFacade              - Creating Unity container.
UnityLoggerFacade              - Configuring the Unity container.
UnityLoggerFacade              - Adding UnityBootstrapperExtension to container.
UnityLoggerFacade              - Configuring ServiceLocator singleton.
UnityLoggerFacade              - Configuring region adapters.
UnityLoggerFacade              - Configuring default region behaviors.
UnityLoggerFacade              - Registering Framework Exception Types.
UnityLoggerFacade              - Creating the shell.
UnityLoggerFacade              - Setting the RegionManager.
UnityLoggerFacade              - Updating Regions.
UnityLoggerFacade              - Initializing the shell.
UnityLoggerFacade              - Initializing modules.
UnityLoggerFacade              - Bootstrapper sequence completed.