C# class forward declaration, possible?

G

Guest

I'm generating a TLB from one of my assemblies that contains an interface
declaration. The class that implements this interface is in another assembly.
I import this tlb in my C++ code to create instances of my C# classes. The
problem I have is that I need to specify the class in my CoCreate call in C++.

If I try to generate a TLB for the assembly that does contains the class and
interface implementation, I end up with a circular dependency problem between
my C++ and C# projects.

Is there any way I can declare the C# class along with the interface (a bit
like a IDL file) in that first assembly so the class is defined in the TLB?

Thanks,
 
N

Nicholas Paldino [.NET/C# MVP]

François,

I don't think that you need to change your C# code or the TLB produced
by calling TLBEXP. Rather, you need change how they are referenced in your
C++. If you are using the #import statement, then you can use the exclude
attribute to indicate what types to exclude (in this case, the interface
definition), as well as other attributes to control how the type information
is imported.

Hope this helps.
 
G

Guest

Hi Nicholas and thanks for your quick reply,

So I guess the answer for forward declaration in C# is no?? :)

The info you provided does not help me. The circular dependency is not a
*real* dependency because one of the dependency is for creating a COM objetc.

We had this same problem in C++ before: we have a utils dll that all other
dll have a real dependency on. At some point, we needed to call one of our
own COM object from this utils.dll Since the dll that hosted the COM object
depended on the utils dll, we couldn't just create a dependency in DevStudio.
Instead, we created a new project that contained the IDL file of the COM dll
and both projects depended (in DevStudio) on this IDL project. So the build
sequence was:

- the IDL project
=> creates the COM header file (interface + class definition)
- the Utils project (uses COM objects from next project)
- the COM implementation project (depends on Utils)

I have the exact same problem now (but with four Dlls, two in C# and two in
C++)

In C#, I have the interface in an assembly so it acts as an equivalent to
the IDL project above, except, because there is no class definition in the
resulting TLB, the next project in line can't compile.

If I could just add a class declaration to this assembly, my problem would
be solved, can it be done in C#?

Thanks for your support, it's really appreciated,


Nicholas Paldino said:
François,

I don't think that you need to change your C# code or the TLB produced
by calling TLBEXP. Rather, you need change how they are referenced in your
C++. If you are using the #import statement, then you can use the exclude
attribute to indicate what types to exclude (in this case, the interface
definition), as well as other attributes to control how the type information
is imported.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

François Côté said:
I'm generating a TLB from one of my assemblies that contains an interface
declaration. The class that implements this interface is in another
assembly.
I import this tlb in my C++ code to create instances of my C# classes. The
problem I have is that I need to specify the class in my CoCreate call in
C++.

If I try to generate a TLB for the assembly that does contains the class
and
interface implementation, I end up with a circular dependency problem
between
my C++ and C# projects.

Is there any way I can declare the C# class along with the interface (a
bit
like a IDL file) in that first assembly so the class is defined in the
TLB?

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top