none
System.TypeLoadException in Type.GetType( for any type ) RRS feed

  • Question

  •  public class Column
        {
            public Object[] Values { get; set; }
            public Type ColumnType { get; set; }

            public Column()
            {
            }

            public Column(Object[] values, string typeName)
            {
                int count = values.Count();
                ColumnType = Type.GetType(typeName, true);
    • Edited by dvlprJacob Tuesday, October 17, 2017 10:34 AM
    Tuesday, October 17, 2017 10:33 AM

Answers

  • Hello dvlprJacob,

    For the following statement:

    return new CSV_Table(this.ColNames, results, this.GetColumnTypes());

    you should pay attention to the parameters of CSV_Table constructor. The constructor have handled the pass parameters base on the fixed collection format.

    Before parameters are passed you should transfer them to ruled format. Base on your code I have edited some code and it works well.

    public string[] GetColumnTypes()
            {
                string[] typeNames = new string[Columns.Count()];
                for (int i = 0; i < Columns.Count(); i++)
                {
                    var ab=Columns[i].ValueType.ToString();
                    switch (ab) {
                        case "System.DateTime":
                            typeNames[i] = "Date";
                            break;
                        case "System.Int32":
                            typeNames[i] = "Integer";
                            break;
                        case "System.String":
                            typeNames[i] = "String";
                            break;
                    }
                }
                return typeNames;
            }
    
    private List<object[]> ChangeFormat(List<object[]> results)
            {
              
                List<Object[]> list = new List<object[]>();
    
                for (int i = 0; i < results[0].Length; i++){
                    object[] ob = new object[results.Count];
                    for (int j = 0; j < results.Count; j++) {
                        
                        ob[j]= results[j].GetValue(i);                   
                    }
                    list.Add(ob);
                }
                return list;
            }
    

    and replace the below code in you find method

     return new CSV_Table(this.ColNames, ChangeFormat(results), GetColumnTypes());

    Result

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by dvlprJacob Wednesday, January 17, 2018 5:09 PM
    Wednesday, October 25, 2017 11:36 AM
    Moderator

All replies

  • public class CSV_Table
        {
            public string[] ColNames { get; set; }
            public Column[] Columns { get; set; }

            public CSV_Table(string[] colNames, List<Object[]> values, string[] typeNames)
            {
                ColNames = colNames;
                Columns = new Column[values.Count()];
                int i = 0;
                foreach (var column in values)
                {
                    Columns[i] = new Column(column, typeNames[i]);
                    i++;
                }
            }
        }
    Tuesday, October 17, 2017 10:34 AM
  •  private static void Main(string[] args)
            {
                string[] names = { "Name String", "Age Int", "BDate DateTime" };
                Object[] Names = new object[] { "First name", "Last name" };
                Object[] Ages = new object[] { 14, 53 };
                Object[] BDate = new object[] { "12.12.2000", "30.09.1990" };
                CSV_Table t = new CSV_Table(names, new List<object[]>() { Names, Ages, BDate }, new string[] { "String", "Int", "DateTime" });
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(t.Columns[i].GetType() + "  ___  " + t.Columns[i].ColumnType.ToString());
                }
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(t.Columns[i].Values[1]);
                }
                Console.ReadKey();
            }

    Tuesday, October 17, 2017 10:35 AM
  • When calling Type.GetType(), the type must be the full qualified name, including all namespaces.

    E.g

    Type t1 = Type.GetType("System.String", true); 
    Type t2 = Type.GetType("System.Int32", true);

    Tuesday, October 17, 2017 10:47 AM

  • all corrected, works, but at a conclusion again NullReferenceExc
    • Edited by dvlprJacob Tuesday, October 17, 2017 11:41 AM
    Tuesday, October 17, 2017 11:35 AM
  • string[] names = { "Name String", "Age Int", "BDate DateTime" };
                Object[] Names = new object[] { "First name", "Last name" };
                Object[] Ages = new object[] { 14, 53 };
                Object[] BDate = new object[] { "12.12.2000", "30.09.1990" };
                CSV_Table t = new CSV_Table(names, new List<object[]>() { Names, Ages, BDate }, new string[] { "String", "Integer", "Date" });
                for (int i = 0; i < 3; i++)
                {
                    Console.Write(t.Columns[i].GetType());
                    Console.WriteLine("  ___  " + t.Columns[i].ValueType.ToString());
                }
                for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(t.Columns[i].Values[1]);
                }
    Tuesday, October 17, 2017 11:41 AM
  • On Line Console.WriteLine(t.Columns[i].Values[1]);

    Tuesday, October 17, 2017 11:42 AM
  • public class CSV_Table
        {
            public string[] ColNames { get; set; }
            public Column[] Columns { get; set; }

            public CSV_Table(string[] colNames, List<Object[]> values, string[] typeNames)
            {
                ColNames = colNames;
                Columns = new Column[values.Count()];
                int i = 0;
                foreach (var column in values)
                {
                    Columns[i] = new Column(column, typeNames[i]);
                    i++;
                }
            }
        }
    Tuesday, October 17, 2017 11:42 AM
  • public class Column
        {
            public Object[] Values { get; set; }
            public Type ValueType { get; set; }

            public Column()
            {
            }

            public Column(Object[] values, string typeName)
            {
                int count = values.Count();
                switch (typeName)
                {
                    case "String":
                        ValueType = Type.GetType("System.String", true);
                        Values = values as String[];
                        break;

                    case "Date":
                        ValueType = Type.GetType("System.DateTime", true);
                        Values = values;
                        break;

                    case "Float":
                        ValueType = Type.GetType("System.Double", true);
                        Values = values;
                        break;

                    case "Integer":
                        ValueType = Type.GetType("System.Int32", true);
                        Values = values;
                        break;
                }
    Tuesday, October 17, 2017 11:42 AM
  • The problem is in the constructor of your Column class.

    You can't just cast an object array into a string array using 'as'. The 'as' operator also, by definition, will attempt to cast if it can but returns null otherwise.

    This is why you get a null reference later on when attempting to access  the value.

    Change the line:

    Values = values as String[];

    to

    Values = values.Cast<string>().ToArray();
    

    Tuesday, October 17, 2017 12:09 PM
  • And what about DateTime

    Tuesday, October 17, 2017 12:23 PM
  • Not sure what you mean.

    I realised your casting of values to a string[] is pointless anyway, as you are then placing it into the Values property, which is an object[] anyway (hence casting it back)!

    You are not casting any of the other arrays, and for the same reason it would be pointless to do so if all you are going to is assign them to back the Values property which is of type object[].

    Tuesday, October 17, 2017 12:37 PM
  • Hi dvlprJacob,

    Please use the code block option to add the code.

    In your code have you ever assigning value to your Column class?

    What I am seeing is that from Main method your are calling only CSV_Table class where you are passing value and in CSV_Table class you are calling Column class and try to access the value. Probably that is why you are getting null reference exception.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]



    Tuesday, October 17, 2017 3:12 PM
    Moderator
  • There is a CSV file where each column is of a certain type, the type is specified in the header of the table 

    Tuesday, October 17, 2017 8:28 PM
  • (Ex name String; age Integer; bdate Date; ... any_column Float)

    Tuesday, October 17, 2017 8:28 PM
  • I need to design a program to work with this file (parser and classes for working with data that is parsed)
    Tuesday, October 17, 2017 8:29 PM
  • I wanted to make a generic class, but it did not work out that way, friends suggested using Object [] to store a column of a certain type
    Tuesday, October 17, 2017 8:36 PM
  • using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CSV
    {
        public class Column
        {
            public Object[] Values { get; set; }
            public Type ValueType { get; set; }
    
            public Column()
            {
            }
    
            public Column(Object[] values, string typeName)
            {
                if (values == null)
                    throw new ArgumentNullException("Object[] values is null");
                if (typeName == null)
                    throw new ArgumentNullException("string typeNames is null");
    
                int count = values.Count();
                switch (typeName)
                {
                    case "String":
                        ValueType = Type.GetType("System.String", true);
                        Values = values.Cast<string>().ToArray();
                        break;
    
                    case "Date":
                        ValueType = Type.GetType("System.DateTime", true);
                        Values = new Object[count];
                        int i = 0;
                        foreach (var el in values)
                        {
                            Values[i] = el;
                            i++;
                        }
                        break;
    
                    case "Float":
                        ValueType = Type.GetType("System.Double", true);
                        Values = new Object[count];
                        i = 0;
                        foreach (var el in values)
                        {
                            Values[i] = el;
                            i++;
                        }
                        break;
    
                    case "Integer":
                        ValueType = Type.GetType("System.Int32", true);
                        Values = new Object[count];
                        i = 0;
                        foreach (var el in values)
                        {
                            Values[i] = el;
                            i++;
                        }
                        break;
                }
                //for (int i = 0; i < values.Count(); i++)
                //{
                //    Values[i] = Convert.ChangeType(values[i], Type.GetType(typeName, true));
                //}
            }
    
            public override string ToString()
            {
                string temp = "";
                foreach (var el in Values)
                {
                    temp += el.ToString() + " ";
                }
                return String.Format("{0}", temp);
            }
        }
    }
    
    namespace CSV
    {
        public class CSV_Table
        {
            public string[] ColNames { get; set; }
            public Column[] Columns { get; set; }
    
            public CSV_Table(string[] colNames, List<Object[]> values, string[] typeNames)
            {
                ColNames = colNames;
                Columns = new Column[values.Count()];
                int i = 0;
                foreach (var column in values)
                {
                    Columns[i] = new Column(column, typeNames[i]);
                    i++;
                }
            }
    
            public Column GetColumn(int index)
            {
                return this.Columns.ElementAt(index);
            }
    
            public Column GetColumn(string columnName)
            {
                int count = ColNames.Count();
                for (int i = 0; i < count; i++)
                {
                    if (ColNames[i] == columnName)
                        return Columns[i];
                }
                return null;
            }
        }
    }
    
    namespace CSV
    {
        public class CSV_Parser
        {
            public CSV_Table Table { get; set; }
    
            public Object[] Find(object value, string column)
            {
                return null;
            }
    
            public Object[] Find(object value, int columnIndex)
            {
                return null;
            }
    
            public CSV_Parser(string fileName)
            {
                string curPath = Directory.GetCurrentDirectory();
                using (StreamReader sr = new StreamReader(curPath + fileName))
                {
                    try
                    {
                        // Считаем шапку таблицы, распарсим на названия колонок и типы значений
    
                        string[] tableHead = sr.ReadLine().Split(';');
                        var c = tableHead.Count();
                        string[] typeNames = new string[c];
                        string[] colNames = new string[c];
    
                        colNames[0] = tableHead[0].TakeWhile(e => e != ' ') as string;
                        typeNames[0] = tableHead[0].SkipWhile(e => e != ' ') as string;
    
                        for (int i = 1; i < c; i++)
                        {
                            colNames[0] = tableHead[i].Remove(0, 0).TakeWhile(e => e != ' ') as string;
                            typeNames[0] = tableHead[i].Remove(0, 0).SkipWhile(e => e != ' ') as string;
                        }
    
                        // Считаем значения в один массив
    
                        List<Object> values = new List<Object>();
                        string line = "";
                        while (!sr.EndOfStream)
                        {
                            line = sr.ReadLine();
                            values.Add(line);
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        return;
                    }
                }
            }
        }
    }

    Tuesday, October 17, 2017 8:38 PM
  • Hello dvlprJacob,

    If your issue is solved please Mark as answer to the appropriate answer so that it will help other members to find solution if they faces similar issue.Your understanding and cooperation will be grateful.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 18, 2017 6:45 AM
    Moderator
  • It works more and less, but the search does not yield results
    private static void Main(string[] args)
            {
                string[] names = { "Name String", "Age Int", "BDate DateTime" };
                Object[] Names = new object[] { "First name", "Last name", "Jahn" };
                Object[] Ages = new object[] { 14, 53, 23 };
                Object[] BDate = new object[] { "12.12.2000", "30.09.1990", "12.12.2000" };
                CSV_Table t = new CSV_Table(names, new List<object[]>() { Names, Ages, BDate }, new string[] { "String", "Integer", "Date" });
                for (int i = 0; i < t.Columns[0].Values.Count(); i++)
                {
                    for (int j = 0; j < t.Columns.Count(); j++)
                        Console.Write(t.Columns[j].Values[i] + "  ");
                    Console.WriteLine();
                }
                if (new DateTime(2000, 12, 12) == Convert.ToDateTime(t.Columns[2].Values[0]))
                    Console.WriteLine("true");
                //---------------------------------
    
                var bdate = Convert.ToDateTime(BDate[0]);
                var T = t.Find("BDate DateTime", (object)bdate);
                if (T.Columns[0].Values.Count() != 0)
                {
                    Console.Write(T.Columns[0].Values[0] + " birth ");
                    Console.WriteLine(T.Columns[2].Values[0]);
                    for (int i = 0; i < t.Columns[0].Values.Count(); i++)
                    {
                        for (int j = 0; j < t.Columns.Count(); j++)
                            Console.Write(t.Columns[j].Values[i] + "  ");
                        Console.WriteLine();
                    }
                }
                else
                    Console.WriteLine("Empty result");
                Console.ReadKey();
            }

    Wednesday, October 18, 2017 11:15 AM
  • although the results in the test should be 2
    public Object[] GetRow(int index)
            {
                int n = ColNames.Count();
                Object[] temp = new Object[n];
                for (int i = 0; i < n; i++)
                {
                    temp[i] = Columns[i].Values[index];
                }
                return temp;
            }
    
    public string[] GetColumnTypes()
            {
                string[] typeNames = new string[Columns.Count()];
                for (int i = 0; i < Columns.Count(); i++)
                {
                    typeNames[i] = Columns[i].ValueType.ToString();
                }
                return typeNames;
            }
    
    public CSV_Table Find(string columnName, object searchValue)
            {
                int colIndex = -1;
                this.GetColumnIndex(columnName, out colIndex);
                if (colIndex == -1)
                    throw new Exception(String.Format("Column with name {0} is not exist on table.", columnName));
    
                List<object[]> results = new List<object[]>();
                int cols = this.ColNames.Count();
                int rows = this.Columns[0].Values.Count();
                for (int i = 0; i < rows; i++)
                {
                    var temp = Convert.ChangeType(this.Columns[colIndex].Values[i], this.Columns[colIndex].ValueType);
                    if (temp == searchValue)
                    {
                        Console.WriteLine("faund");
                        results.Add(this.GetRow(i));
                    }
                }
                if (results.Count() == 0)
                    throw new Exception("not faund");
                return new CSV_Table(this.ColNames, results, this.GetColumnTypes());
            }
    
            public void GetColumnIndex(string colName, out int index)
            {
                int count = ColNames.Count();
                for (int i = 0; i < count; i++)
                {
                    if (ColNames[i] == colName)
                    {
                        index = i;
                        return;
                    }
                }
                index = -1;
                return;
            }

    Wednesday, October 18, 2017 11:18 AM
  • Find method is work, i use 
    for (int i = 0; i < rows; i++)
                {
                    var temp = Convert.ChangeType(this.Columns[colIndex].Values[i], this.Columns[colIndex].ValueType);
                    bool isa = Object.Equals(temp, searchValue);
                    if (isa)
                    {
                        results.Add(this.GetRow(i));
                    }
                    isa = false;
                }
    result is not null and correct when i it return on line 
    return new CSV_Table(this.ColNames, results, this.GetColumnTypes());
    But in Main function on line 
    CSV_Table T = t.Find("BDate DateTime", (object)bdate);
    T is null, what is the problem, help, please
    Wednesday, October 18, 2017 11:35 AM
  • Hello dvlprJacob,

    For the following statement:

    return new CSV_Table(this.ColNames, results, this.GetColumnTypes());

    you should pay attention to the parameters of CSV_Table constructor. The constructor have handled the pass parameters base on the fixed collection format.

    Before parameters are passed you should transfer them to ruled format. Base on your code I have edited some code and it works well.

    public string[] GetColumnTypes()
            {
                string[] typeNames = new string[Columns.Count()];
                for (int i = 0; i < Columns.Count(); i++)
                {
                    var ab=Columns[i].ValueType.ToString();
                    switch (ab) {
                        case "System.DateTime":
                            typeNames[i] = "Date";
                            break;
                        case "System.Int32":
                            typeNames[i] = "Integer";
                            break;
                        case "System.String":
                            typeNames[i] = "String";
                            break;
                    }
                }
                return typeNames;
            }
    
    private List<object[]> ChangeFormat(List<object[]> results)
            {
              
                List<Object[]> list = new List<object[]>();
    
                for (int i = 0; i < results[0].Length; i++){
                    object[] ob = new object[results.Count];
                    for (int j = 0; j < results.Count; j++) {
                        
                        ob[j]= results[j].GetValue(i);                   
                    }
                    list.Add(ob);
                }
                return list;
            }
    

    and replace the below code in you find method

     return new CSV_Table(this.ColNames, ChangeFormat(results), GetColumnTypes());

    Result

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by dvlprJacob Wednesday, January 17, 2018 5:09 PM
    Wednesday, October 25, 2017 11:36 AM
    Moderator