Question: I am having some trouble with assemblies and DLL's.

instrument\_ is declared as an object and I'm creating an instance of "PP150" from the dll whose path is specified by path\_.

```
string className = ContineoProperties.getSingleton().getClassName(path_);
assembly_ = Assembly.LoadFrom(path_);
Type classType = assembly_.GetType("Instrument." + className);
instrument_ = Activator.CreateInstance(classType);

```

Later I to call the method `isntrument_.instrumentCommand(cmd.getCommandName())`

The error I get is with when i call the method.

> 
> 'object' does not contain a definition for 'instrumentCommand'
> 
> 
> 

The isntrument\_ is created fine. its just the method call that's giving me a problem. The method does exist in the "PP150.dll". Do I need some DLLImport to allow it to recognize it as a function? 

Thanks, 
P

Answer: 
The compiler is never going to recognize the methods on a type that you are loading via reflection (e.g. using `Assembly.GetType()` and `Activator.CreateInstance()`). Unless you have the type metadata available at build time, you will always get that error if you try to call methods that are not defined on Object itself.

You have two options for making that kind of method call. Both of them require you to give up type safety, the only difference is the amount of work required. In both cases, if you make a mistake, the compiler will **not** tell you -- you will get a runtime exception instead.

1. Declare instrument\_ as `dynamic` instead of `object`. This, obviously, only works in .NET 4.0, but it accomplishes exactly what you're trying to do. The method call will be dispatched at runtime, so as long as the instance that instrument\_ references **actually has** a method call with the appropriate name, it will work.
2. Use reflection to call the method. You're already using reflection to load the type, so you are halfway there. You would need to add something like this:

```
// The array of types is the parameter list; assuming instrumentCommand takes
// a string it would look like this:
MethodInfo method = classType.GetMethod("instrumentCommand", new Type[] { typeof(string) });
method.Invoke(instrument_, new object[] { cmd.getCommandName() });

```
This happens because [`Activator.CreateInstance`](http://msdn.microsoft.com/en-us/library/wccyzw83.aspx) returns an `object`. I would create a separate DLL for the `interface` which is implemented by the class you want to instantiate. Both the DLL containing this class, and the executable should reference the DLL containing the interface. This way you could cast the `object` returned by `Activator.CreateInstance` to the interface, and call its methods: 

IInstrument.dll:

```
interface IInstrument
{
  void instrumentCommand(string cmd);
}

```

Instrument.dll (add IInstrument.dll as reference):

```
class Instrument : IInstrument
{
  public void instrumentCommand(string cmd)
  {
    // ... implementation ...
  }
}

```

InstrumentApp.exe (add IInstrument.dll as reference):

```
class Program
{
  public static void Main()
  {
    // ... load Instrument.dll into assembly object ...
    // ... load the type from the assembly ...
    IInstrument instrument_ = (IInstrument)Activator.CreateInstance(classType);
    instrument_.instrumentCommand(cmd.getCommandName()); 
  }
}

```