Exemple #1
0
 internal static T[] ConvertToVector <T>(object In)
 {
     T[] V;
     try
     {
         object[] InVec;
         if (In.GetType() == typeof(object) || In.GetType() == typeof(T))
         {
             V    = new T[1];
             V[0] = ConvertTo <T>(In);
             return(V);
         }
         else if (In.GetType() == typeof(object[]))
         {
             InVec = (object[])In;
             int length = InVec.GetLength(0);
             V = new T[length];
             for (int i = 0; i < length; i++)
             {
                 V[i] = ConvertTo <T>(InVec[i]);
             }
             return(V);
         }
         else if (In.GetType() == typeof(object[, ]))
         {
             object[,] InM = (object[, ])In;
             int rows = InM.GetLength(0);
             V = new T[rows];
             for (int i = 0; i < rows; i++)
             {
                 V[i] = ConvertTo <T>(InM[i, 0]);
             }
             return(V);
         }
         else
         {
             errorMessages.AddFirst("Could not convert input to vector of type " + typeof(T).ToString());
             return(null);
         }
     }
     catch (Exception)
     {
         errorMessages.AddFirst("Could not convert input to vector of type " + typeof(T).ToString());
         return(null);
     }
 }
 private static T[] ConvertToVector <T>(object In)
 {
     T[] V;
     try
     {
         object[] InVec;
         if (In.GetType() == typeof(object) || In.GetType() == typeof(T))
         {
             V    = new T[1];
             V[0] = ConvertTo <T>(In);
             return(V);
         }
         else if (In.GetType() == typeof(object[]))
         {
             InVec = (object[])In;
             int length = InVec.GetLength(0);
             V = new T[length];
             for (int i = 0; i < length; i++)
             {
                 V[i] = ConvertTo <T>(InVec[i]);
             }
             return(V);
         }
         else if (In.GetType() == typeof(object[, ]))
         {
             object[,] InM = (object[, ])In;
             int rows = InM.GetLength(0);
             V = new T[rows];
             for (int i = 0; i < rows; i++)
             {
                 V[i] = ConvertTo <T>(InM[i, 0]);
             }
             return(V);
         }
         else
         {
             throw new InvalidOperationException("Could not convert input to vector of type " + typeof(T).ToString());
         }
     }
     catch (Exception e)
     {
         throw new InvalidOperationException("Could not convert input to vector of type " + typeof(T).ToString(), e);
     }
 }