Looking for Efficent Type Testing and Type Conversion

The Question

I’m wondering if there is a more efficient way to check the type of IGH_GOO in order to cast it to alternative types.

Use Case

Given a list of GH_Goo items of multiple non-geometry types [ GH_Integer, GH_Boolean, GH_Number , GH_Time, ect], identify the type in order to specify the correct corresponding type within an external library.

Current Strategy

//'attributes' is a GH_Structure<IGH_Goo> with mixed data types
//'fs' is a FeatureSet defined by an external library 

//retrieve the IGH_Goo item to try and identify the type of
var typeItem = attributes.get_Branch(thisFieldPath)[thisFieldIndex];
string fieldName= "typeItem's DataTable Column Name";

//sequentially try and identify the type with 'if' statments
if (typeItem is Grasshopper.Kernel.Types.GH_Number)
    {
     fs.DataTable.Columns.Add(new DataColumn(fieldName, typeof(double)));
    }
else if (typeItem is Grasshopper.Kernel.Types.GH_Integer)
    {
      fs.DataTable.Columns.Add(new DataColumn(fieldName, typeof(int)));
    }
else
    {
       fs.DataTable.Columns.Add(new DataColumn(fieldName, typeof(string)));
    }

//-----------------------------------------------------------------------//
// after FeatureSet DataTable Columns have been made with the correct type
// iterate over attributes and converting them to correct type 
// then adding them to the DataTable

foreach (var thisAttribute in attributes.get_Branch(path))
  {
      // 1) check DataColumn type
      //  2) convert attribute to corisponding type 
      //  3) add converted attribute to DataRow
  if (fs.DataTable.Columns[fields[thisIndex]].DataType == typeof(double))
   {
     feature.DataRow[fields[thisIndex]] = Convert.ToDouble(thisAttribute.ToString());
   }
  if (fs.DataTable.Columns[fields[thisIndex]].DataType == typeof(int))
   {
     feature.DataRow[fields[thisIndex]] = Convert.ToInt32(thisAttribute.ToString());
   }
  else
   {
     feature.DataRow[fields[thisIndex]] = thisAttribute.ToString(); 
   }
 }

Imagined Alternative

I imagen there would be a way to retrieve the IGH_Goo’s Type and then look that type up in a Dictionary for its corresponding value. Perhaps “enum GH_IO.Types.GH_Types” could be used along with “Enum.GetName()” to help? Here is an example below:

//'attributes' is a GH_Structure<IGH_Goo> with mixed data types
var typeItem = attributes.get_Branch(thisFieldPath)[thisFieldIndex];

Dictionary<string, Type> typeMap = new Dictionary<string, Type> { 
      { "gh_int32",  typeof(short)    }, 
      { "gh_int64",  typeof(long)     }, 
      { "gh_double", typeof(double)   },
      { "gh_decimal",typeof(float)    },
      { "gh_date",   typeof(DateTime) }, 
      { "gh_guid",   typeof(string)   }, 
      { "gh_string", typeof(string)   }
 };

var fieldType = typeMap[ TypeOf(typeItem) ]; //The Magic 'TypeOf' Method I'm looking for...
string fieldName = "typeItem's DataTable Column Name";
fs.DataTable.Columns.Add(new DataColumn(fieldName, fieldType)); //'fs' is a FeatureSet defined by an external library 

Other Disscussions on this:


You could create a dictionary using System.Type as a key and some delegate as value, then call GetType() on the data and get the matching delegate to invoke?