views:

209

answers:

2

I am trying to load an assembly dynamically and create an variable of its type:

Assembly Ass= Assembly.LoadFrom(@"d:\abc\microsoft.office.interop.excel.dll");
foreach(Type Excel Assembly.Gettypes())
{
    // here now  Type contains
    // Excel.nameSpace="microsoft.officce.interop.excel"

    // now i need to creae an variable of  type "Excel"

    microsoft.officce.interop.excel.applicationClass excel= null;
    // something like this 
    //Here  Excel is my nameSpace
    Excel.officce.interop.excel.applicationClass excel= null
}

I am working on this from past 2days any help how i can declare my variable of type Excel( which is the type i need to create)

any help would be great thank you

+2  A: 

I'm not certain this will work for that assembly, but in general you can do this by getting the type from the assembly then create the instance using Activator.CreateInstance:

Type type = assembly.GetType("MyType");

object instanceOfMyType = Activator.CreateInstance(type);

you'll need to know the name of the type you want to create ('Excel.Application'?)

you can also get the type directly from the dll if you know the path to it:

Activator.CreateInstance(assmblyFileName, typeName) 

I'm not sure this will work for this assembly as this is a COM interop assembly, so I think you might have to use COM to access it, but it might.

EDIT:

you are probably better using the the documented methods for doing this:

Target Office Applications Through Primary Interop Assemblies should be a good place to start

Sam Holder
A: 

Why not add reference to the assembly? Also at first glance you have a spelling error, you spelt office in microsoft.officce.interop with 2 c's which could be why if you copied and pasted the code.

Here is how I use the excel COM:

using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excelApp = new Excel.ApplicationClass();

Edit:

Also I can see you have namespace problems. The namespace Excel does not exist as you commented out the Excel namespace before, therefore the following on its own should work.

Microsoft.Office.Interop.Excel.ApplicationClass excel = null;

without the:

Excel.officce.interop.excel.applicationClass excel= null
LnDCobra