Tuesday, September 23, 2014

How to fix Could not load type 'xxxxxxxxxxxxxxxx' from assembly using Reflection in C#

when I use reflection to determine the object type(class, method, fields) in run time, the  unhandled

exception of type 'System.TypeLoadException' was always thrown. i scratch my head to figure it out that

I must use the object's full name including namespace to load object type.


In My case

namespace DefectTrackerTest
{
    public class CurrencyConverter
    {
        private double _value;

        [DefectTrackerAttribute(1042,"AP", "2012/02/11","Changed float param to double")]
        public CurrencyConverter(double value)
        {
            _value = value;
        }
      }
}



when i need to load object CurrencyConverter. i must use DefectTrackerTest.CurrencyConverter
as the class name in the following code

      Assembly assembly = Assembly.LoadFrom(assemblyPath);
      Type type = assembly.GetType(className, true, true);

No comments:

Post a Comment