Posts

Showing posts from December, 2012

Using Reflection to Get the Value of a Nested Property

I recently ran into a situation wherein I needed to use reflection to get the value of a nested property. We were returning a custom type that inherits from JsonResponse, but this custom type is private to the controller class that uses it (I'm not sure why -- since we're returning it from some action methods I'd have thought it could be public. But I digress). When writing some unit tests for these methods, I wanted to check the value of the success property which just so happens to be nested within the Data property. So I wrote a little method to assist with this, as shown below (the method and class names have been changed to protect the innocent). The steps to follow are: Get the type of the object. Using the type, get the parent property of the nested property you want via Type.GetProperty(), specifying the name of the parent property as the parameter. Using the property, get its value using PropertyInfo.GetValue(), specifying the parent object and null as pa

A Simple Method to Get an Object's Properties and Values at Runtime

I recently wrote some code to integrate with a WCF service authored by a different team and encountered an error, happening internal to the service, during my initial integration testing of one of the methods. I wanted to provide the developer on the other team with the parameters I passed into the method when the exception occurred, and one of those parameters was an object with more than a few properties. To that end, I wrote a quick method to provide the object's properties and their values so I could include it in the exception handling on my end and provide this info to the other developer to aid in debugging. This method requires a using statements for System.Reflection and System.Text. It could also be expanded a bit (for example, in the way it represents the value for property values that are objects), but as a quickly-written method it gets the job done. protected   string  GetObjectPropertiesAndValues( object  target) {      StringBuilder  output =  new   StringBuilde

Setting a log4net AdoNetAppender Connection String at Runtime

I recently implemented log4net in a project to log to a database using the log4net AdoNetAppender class. But I had a need to be able to specify the connection string at runtime. Here's how I was able to do it. Hopefully this will help out someone who has the same need. :) protected   void  SetLog4NetAppenderConnectionString( string  connectionString) {     log4net.Repository.Hierarchy. Hierarchy  hierarchy =         log4net. LogManager .GetLoggerRepository()               as  log4net.Repository.Hierarchy. Hierarchy ;      if  (hierarchy !=  null )     {         log4net.Appender. AdoNetAppender  appender             = (log4net.Appender. AdoNetAppender )hierarchy.GetAppenders()                 .Where(x => x.GetType() ==                       typeof (log4net.Appender. AdoNetAppender ))                 .FirstOrDefault();          if  (appender !=  null )         {             appender.ConnectionString = connectionString;             appender.ActivateOptions();         }     }

The Cause and Solution for the "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[YourTypeHere]' cannot be serialized." Exception

Image
The other day I worked on some new code that ran fine locally, but when deployed to one of our internal web servers threw the following exception: "System.Runtime.Serialization.InvalidDataContractException: Type 'System.Threading.Tasks.Task`1[NameOfTheOffendingType]' cannot be serialized." This had me scratching my head -- after all, to use that old developer's saying, "it worked fine on my machine". Not only that, but the exception was occurring when I was calling a method that didn't use the type mentioned in the exception (according to the stack trace it looks like the exception was happening during one of the many calls made under the hood during the use of a proxy class method). But once I found the cause of the problem, it made total sense. In this case, I had added a new WCF service reference to the project, and the proxy class that was generated for it was including some new task-based asynchronous operations that are supported in .NET 4.