COM style aggregation in C#

G

Gagan

I have a third-party COM library which has objects that supports
aggregation. By aggregation I mean what is mentioned here at this MSDN
link:

http://msdn2.microsoft.com/en-us/li...cpconinheritanceaggregationcontainmentanchor2

Till now I have been developing in C++ to aggregate objects form that
third-party library so that I could customize their behavior without
having to implement all the interfaces that object implements. But now
we are porting our code to C# and we need to crack this 'how to
implement COM style aggregation in C#" nut.

It is clear to me that I need to use COM interop services - by
referencing the COM library in my C# project which will create a
wrapper assembly. Also I need to implement the interface of the COM
object that I am aggregating in my .NET object - much the same as I do
in C++. But I am utterly clueless as to how to do the aggregation
magic that is performed by C++ ATL macro
COM_INTERFACE_ENTRY_AGGREGATE_BLIND.
From the MSDN documentation and newsgroups I get the idea that
aggregation is not only possible in .NET, but it is actually easier.
Has anyone done this before? I will really really appreciate if
someone can share a piece of sample code.

Thanks!
 
N

Nicholas Paldino [.NET/C# MVP]

Gagan,

In your wrapper object, you will want to implement the interfaces that
you want to aggregate. You also need to hold onto an instance of the object
that you are going to aggregate the calls to You would create that in your
constructor (most likely). Then in the methods that implement the interface
on your wrapper that are aggregated, you just make the call to the reference
your wrapper is holding.
 
A

Anthony Jones

Nicholas Paldino said:
Gagan,

In your wrapper object, you will want to implement the interfaces that
you want to aggregate. You also need to hold onto an instance of the object
that you are going to aggregate the calls to You would create that in your
constructor (most likely). Then in the methods that implement the interface
on your wrapper that are aggregated, you just make the call to the reference
your wrapper is holding.

IOW you can't use COM style aggregation, you can only delegate. The wrapper
object must have knowledge of all required interfaces and implement all
methods albiet many just delegating to the inner objects implementation.

I was trying to think of how aggregation could be performed but 1.) I can't
see how and 2.) it was a fudge anyway to work round the fact that COM didn't
really support inheritance.
 

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