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: 
If object type is not known in compile time,

To call a method defined on an object, you must use Reflection.

```
MethodInfo mInfo = classType.GetMethod("instrumentCommand");

mInfo.Invoke(instrument_, new Object[] { _parameters});

```
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()); 
  }
}

```