Skip navigation

Category Archives: C# Generic Methods and Classes

This section contains fully developed custom methods or classes that are generic enough to be able to be used on several different projects.

I am working on a project currently where I wished to share non secure data across domains using Javascript and JSON objects.

After several hours of tinkering and researching I finally was able to figure out a solution which works in the normal .NET framework.

Read More »

I’m working on a project right now where I have an XML data source that is provided by a third party and I need to import the data into a usable object model which I plan to store in MongoDB.

I didn’t want to have to write a ton of redundant code for each and every field on the object in a factory pattern. There is literally about 40 to 50 fields on this object.

What I discovered is this cool thing called reflection.

Here is the code snippet:

var name = "PropertyName";
var newValue = "This is my value I want to set";

PropertyInfo prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
if (null != prop && prop.CanWrite)
{
      prop.SetValue(obj, Convert.ChangeType(newValue, prop.PropertyType), null);
}

Essentially the first two lines here are ensuring that firstly the property exists on the object and secondly that the property is settable.

Since all the properties on my object match up to fields on my datasource, at least the fields I want to translate over do, this works great.

Here is the method I wrote that allows me to pass an XElement with a Generic and iteratively populate all the fields of the object from values nested within the XElement.

private static T BuildByReflection<T>(XElement element) where T : new()
{
   var obj = new T();
   foreach (var subElem in element.Elements())
   {
      var name = subElem.Name.LocalName;
      PropertyInfo prop = obj.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
      if (null != prop && prop.CanWrite)
      {
          prop.SetValue(obj, Convert.ChangeType(subElem.Value, prop.PropertyType), null);
      }
   }

   return obj;
}

I mentioned this cool little method in my previous post and felt it was worth an explanation.

Up until recently when I wanted to sort a generic list of objects I used the methods I outlined back in June of 2011.

Recently using LINQ I’ve discovered a much more compact code method:

.OrderBy();

Let’s assume we are using our old CoffeeMaker class…

class CoffeeMaker
{

[...]

public int ID;
public string Name;
public decimal Price;

}

And we have a list of CoffeeMakers that we want to sort on the Name.

We simply do the following:

List cMakers = GetCoffeeMakers();
cMakers.OrderBy(cm => cm.Name);

Much shorter for scenarios where you wont be reusing the sort routine frequently and the use doesn’t dictate the neccessity of implementing the full IComparable interface.

At work we constantly struggle with timeouts on our data sources no matter if they are sqldatasource or tableadapter.

As I understand it from my research that is because .Net applications utilize their own timeout settings and while you may have timeouts set on your SQL Database the .Net timeouts are relatively short duration, most defaulting to 30 seconds. With the sheer volume and complexity of some of the queries we have to run, this simply is too short. We sometimes need another 15 to 30 seconds to prevent timeouts.

Read More »

I found myself recently with the need to be able to create System.Drawing.Color objects that were shades of a specific color in terms of “lighter” or “darker”. For those familiar with color theory you may be aware that the best color space to easily manipulate that information falls within HSL where the values of L are the H and Value in relationship to Black and White. Read More »

The following code snipet is a custom method that I use alot to copy the selected items within one ListBox into another list box.

Read More »

Follow

Get every new post delivered to your Inbox.