Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type.Assembly gives wrong assembly?

I want to create a reference to the Excel assembly and use this code:

[Test]
public void ExcelTest()
{
    Assembly current = this.GetType().Assembly;
    Assembly excel = typeof(Microsoft.Office.Interop.Excel.Workbook).Assembly;
    Assert.AreEqual(current.FullName, excel.FullName); // Why is excel the same as current assembly?
}

[Test]
public void MscorlibTest()
{
    Assembly mscorlib = typeof(string).Assembly;
    Assert.IsTrue(mscorlib.FullName.StartsWith("mscorlib"));

    Assembly current = this.GetType().Assembly;
    Assert.AreNotEqual(mscorlib.FullName,current.FullName); // As expected
}

Why is excel the same as my current assembly?

What is a nice way to get the Excel assembly?

Could not find my dumb when reading the docs

like image 280
Johan Larsson Avatar asked Nov 29 '25 19:11

Johan Larsson


1 Answers

Excel, among others, is reached via a COM interop type. It is an interface, decorated with GUID's and stuff. When you use it, it uses COM to communicate with a running Excel application. So your reference is not Excel itself, but merely a thin COM communication layer.

COM interop types may be embedded into your assembly, that seems to be the case here. You can specify that as a property of the assembly reference in your project.

See for example: http://msdn.microsoft.com/en-us/library/dd997297%28v=vs.110%29.aspx or http://msdn.microsoft.com/en-us/library/ee317478.aspx

like image 67
fejesjoco Avatar answered Dec 03 '25 12:12

fejesjoco