0

I am linking one of the external resource at runetime in my code using something like below:

System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom("MyNice.dll");
            Type type = assembly.GetType("MyType");
            Tool = Activator.CreateInstance(type) as Tool;

Now as you can see that at the end of the object creation, it has to cast the resulting object into the tool class because there are a lot of references to the methods and properties of Tool class in my code that if it is no there then the code will error out on compile time.

Now, this is a bad situation because I wanted to remove the Dll from my references and have it loaded dynamically at runtime but at the same to there are pieces of my code that referes to and are dependent to the Tool assembly. How can I make it independent? Do I have to use reflection all over my code or there is any easy alternative out there?

for example:

if (Tool.ApplicationIsOpen)
                    return StatusResult.Success;

is there in the same class which assumes that Tool class already exists and will break if I remove it from my references folder.

Any suggesitons?

Lost
  • 12,007
  • 32
  • 121
  • 193

1 Answers1

1

I would suggest making shared DLL to reference from both projects that contains an interface in which Tool inherits.

In this shared project, make an interface such as ITool, that exposes the functionality you need for the consumer project.

Shared Project

public interface ITool
{
    void Something();
}

Separate Project

public class Tool : ITool
{
    public void Something()
    {
        // do something
    }
}

Consumer Project

System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom("MyNice.dll");
Type type = assembly.GetTypes().FirstOrDefault(t => t.IsAssignableFrom(typeof(ITool)));
ITool tool = Activator.CreateInstance(type) as ITool;

Now you can delete the reference to the project containing Tool, but you still need the reference to the shared project that contains ITool. If you truly don't want any references, then explore the reflection route, but be warned it'll probably be messy.

This strategy is the basis for many plugin systems. I'd recommend you check out some Dependency Injection (DI for short) libraries that can do a lot of this heavy lifting for you.

Here is a list of DI libraries: http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx Personally I've been using Ninject lately.

Some relevant links:

Community
  • 1
  • 1
Hack
  • 1,408
  • 10
  • 13
  • I am doing trying to do something similar of what you have just suggested. But here is the problem. The class instance that I am trying to instantiate has a lot of members Some of them are of Native type from that assembly. SO if I put an inter ITool in between, how would that know the types of datamembers which is exclusively native to the source assembly? – Lost Aug 22 '13 at 17:29
  • One more thing is that in your example "Separate Project" is our own prject but in my situation it is a plugin project and I do not have control over its code.... – Lost Aug 22 '13 at 17:48
  • Each native type from the assembly that Tool exposes as members would also need to have to have an interface in the shared project. I could make an example project if you like. Do you have access to change the code of Tool? – Hack Aug 22 '13 at 23:34
  • Again where would public class Tool : ITool { public void Something() { // do something } } reside? – Lost Aug 22 '13 at 23:46
  • Are you assuming that class Tool is their class which I can change to inherit from my interface?> – Lost Aug 22 '13 at 23:47
  • Yeah I was assuming you could change the code of Tool, if you can't, then this method won't work. How come you want to remove the reference to this project? – Hack Aug 22 '13 at 23:48
  • Some Business decision.... Nothing that I have control over... But I think someone must have faced something similar. I also tried using keyword dynamic in declaration of the the variable and it did not throw any error at the compile time but failed with bindingexeption at runtime.... here is the post that I recently posted: http://stackoverflow.com/questions/18392186/binding-exception-with-dynamic-keyword-in-c-sharp – Lost Aug 22 '13 at 23:51