0

Unable to solve this issue and desperately need help. My project is calling a dll with below class. I need to use the static method using reflection but unable to make it work due to an exception "Object reference not set to an instance of an object"

namespace Binit.Core.Database
{

public abstract class DbFormat
{
    protected static DbFormat m_internalDbFormat;
    public static DbFormat Create()
     {
          return m_internalDbFormat.internalCreate();
     }
}
}

Below code works

Binit.Core.Database.DbFormat format;
format = Binit.Core.Database.DbFormat.Create();

Below code using reflection fails

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(null, null);

Exception:

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

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)

m_internalDbFormat is null when i am using reflection . But when i do direct reference code it doesn't become null even though i am executing only create method. I believe it means the references are loaded using direct reference and using reflection i may need to set them explicity. it seems Binit.Core.Database.Implementation is the dll that is referenced in the project doing direct reference. so i might need to invoke this explicity . Any ideas.

battech
  • 803
  • 2
  • 13
  • 25
  • are you sure you want to pass null,null to createFormatMethod. I guess the method expects some non null parameter and if your code without reflection works fine try using createFormatMethod.Invoke() – nikhil mehta Aug 19 '15 at 06:39
  • Where do you set `m_internalDbFormat`? My guess: in a static constructor which is not run when you call it through reflection (and the class was not accessed before). – Henrik Aug 19 '15 at 07:22
  • I am trying hard to find it out . But there isn't a static constructor – battech Aug 19 '15 at 07:28
  • @battech then, from where you are setting m_internalDbFormat ?? – Rahul R. Aug 19 '15 at 07:40
  • i did a detailed search to find out that Binit.Core.Database.Implementation.DbFormatImpl class is initializing the m_internalDbFormat. I had added Binit.Core.Database.Implementation.dll to my project when i used direct reference. With reflection i am not sure why it is not automatically initialized. – battech Aug 19 '15 at 10:03

0 Answers0