3

I have a TestClass.cs file that contains an interface, and a class, much like this:

namespace CPierce.CSharpBridge
{
    [System.Runtime.InteropServices.Guid("3D08DF02-EFBA-4A65-AD84-B08ADEADBEEF")]
    public interface ICSide
    {
        // interface definition omitted...
    }
    [System.Runtime.InteropServices.Guid("CBC04D81-398B-4B03-A3D1-C6D5DEADBEEF")]
    public partial class CSide : ICSide
    {
        // class definition omitted...
    }
}

When I compile this at the command line, and run regasm on it:

csc /debug /t:library TestClass.cs 
regasm TestClass.dll /tlb:TestClass.tlb

I get a nice, big .tlb file suitable for including in a C++ project elsewhere....

 10/27/2011  01:50 PM             3,616 TestClass.tlb

When I put TestClass.cs into a "Class Project" in Visual Studio, compile it, run regasm, the resulting .tlb is pathetic and nearly useless -- it has no interface, no method signatures, etc...

[Compiled TestClass.cs as part of Project "ClassProject" in Visual Studio]
regasm ClassProject.dll /tlb:ClassProject.dll

10/27/2011  01:58 PM             1,132 ClassProject.tlb

This is the same C# code in both cases, one being compiled with Visual Studio one at the command line, giving me completely different results.

What gives?

--

Update: Hans suggests that the [ComVisible(true)] attribute missing is causing the problem. Tried it, and it worked. But that still doesn't answer the question, why? Why do I get different results based on which compile method I use?

Clinton Pierce
  • 12,859
  • 15
  • 62
  • 90

2 Answers2

2

If you create a new Class Library in Visual Studio, the default AssemblyInfo.cs file includes the line:

[assembly: ComVisible(false)]

The command-line command you're using is only compiling your TestClass.cs file, so you get the default setting for ComVisible (which, judging from the available evidence, is probably true). When you compile from the IDE, you include AssemblyInfo.cs as well, so its explicit setting overrides the compiler's default.

Joe White
  • 94,807
  • 60
  • 220
  • 330
0

Also check if your class has functions of accessor type public.

In our case, the project was working fine when it was being used from within the solution but when we extracted the logic to create a DLL, it stopped creating the TLB file with no indication on why...

So if you have a class like so,

public class tlbuser{    
    private void functionTLB(){
        //function code
    }
    // rest of the class code
}

Ensure it is changed to:

public class tlbuser{    
    public void functionTLB(){
        //function code
    }
    // rest of the class code
}