mardi 18 août 2015

Casting Enumerator type with reflection

I am trying to replicate a functionality with reflection but end up getting

CreateFormatMethod.Invoke(typDbFormatClass, null)' threw an exception of type 'System.Reflection.TargetInvocationException' object {System.Reflection.TargetInvocationException}

Inner Exception Shows Object reference not set to an instance of an object.

I am trying to access a static method of abstract class here. Below is the direct reference code that works perfectly .

Binit.Core.Database.DbFormat format;
format = Binit.Core.Database.DbFormat.Create();
format.Dimension = 
   DbDoubleDimension.GetDimension((DbDimension)Enum.Parse(typeof(DbDimension), dimension));

Reflection Code which fails and shows targetinvocationexception

Assembly CoreDatabaseAssembly = Assembly.LoadFrom(Path.Combine(APath, @"Binit.Core.Database.dll"));
Type typDbFormatClass = CoreDatabaseAssembly.GetType("Binit.Core.Database.DbFormat");

MethodInfo createFormatMethod = typDbFormatClass.GetMethod("Create", BindingFlags.Static | BindingFlags.Public, null, new Type[] { }, null);
object objDbFormat = createFormatMethod.Invoke(typDbFormatClass, null);

Type typDbDimension = CoreDatabaseAssembly.GetType("Binit.Core.Database.DbDimension");

Type typDoubleDimensClass = CoreDatabaseAssembly.GetType("Binit.Core.Database.DbDoubleDimension");
MethodInfo DbDoubleDimensionMethod = typDoubleDimensClass.GetMethod("GetDimension", BindingFlags.Static | BindingFlags.Public, null, new Type[] { typDbDimension } , null);
object[] parametersDbDoubleDimen = new object[] { Enum.Parse(typDbDimension, dimension) };
object objDimension = DbDoubleDimensionMethod.Invoke(typDoubleDimensClass, parametersDbDoubleDimen);

i am not sure how to do the casting for below Enumerator with reflection and i doubt if this could be causing the issue

(DbDimension)Enum.Parse(typeof(DbDimension), dimension)

object[] parametersDbDoubleDimen = new object[] { Enum.Parse(typDbDimension, dimension) };





Aucun commentaire:

Enregistrer un commentaire