mardi 5 mars 2019

Reflection: Instance object is assigned to null

first of all I want to explaine my aim: I'm trying to write a modular application and because of that I wrote a MainApplication which loads the modules (e.g. ModulesForMainApplication). My purpose is that the MainApplication is usable even one or more modules are not available. Because of that I created the following project structure:

I created a Solution wich contains two projects: 1. MainApplication 2. ModuleForMainApplication

In "ModuleForMainApplication" I defined a test method which only return a string.

public string RetrunSimpleString()
{
    return "!";
}

As further information: "ModuleForMainApplication" has a reference to "MainApplication" because I need to use a class of the MainApplication (in order to do not double implement them).

After searchin in the internet and adpapting codesnippeds for my aim I'm stuck at a issue. The below shown code thorws a "System.MissingMethodException". Working with DLLs is new for me so I'm right now out of ideas what causes this issue.

Assembly ModuleForMainApplicationAssembly = Assembly.LoadFrom(@"\Path\to\dll\MailProtectionComparison.dll");
Type ModuleForMainApplicationType = ModuleForMainApplicationAssembly.GetType("ModuleForMainApplication.Classes.ModuleForMainApplication");
object ModuleForMainApplicationInstance = Activator.CreateInstance(ModuleForMainApplicationType);
string x = (string)MailProtectionComparisonType.InvokeMember("RetrunSimpleString", BindingFlags.InvokeMethod, null, MailProtectionComparisonInstance, null);

To get sure that the type which i try to instance isn't null I checked "ModuleForMainApplicationType". The debugger shows the following:

ModuleForMainApplicationType    {Name = "ModuleForMainApplication" FullName = "ModuleForMainApplication.Classes.ModuleForMainApplication"}  System.Type {System.RuntimeType}

For now I would think this looks like expected an the type got assinged right. The issue is the creation of the instance because this refferences to null and I don't know why. The exception is thrown for the last line but the issue is cause by the null refferenced object. To come to my question: Where is my issue and why does "ModuleForMainApplicationInstance" references to null?





Aucun commentaire:

Enregistrer un commentaire