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.
System.NullReferenceException
InnerException null
Stacktrace
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at AB.INIT.stringToValue(String valueInUnits, String dimension, String sourceUnit, String destinationUnit)
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) };
The dbFormat Class is as below.
namespace Binit.Core.Database
{
public abstract class DbFormat
{
protected static DbFormat m_internalDbFormat;
protected DbFormat();
public static DbFormat Create();
public static DbFormat Create(DbFormat other);
}
}