Expose members of contained class

H

hvj

Hi all,

Is it possible to 'automatically' expose the members of a contained
class in C# (just as with COM) that implements some interface that
that the containing class wants to implement by delegating to the
contained class? Off course it is possible to implement all the
properties and methods in the containing class by calling the same
property on the contained class, but if it is possible I would like to
avoid that.

I thought maybe with the new possibilities with dynamic methods, class
extension etc this might be possible.

Another possibility would be generating the code that implements the
properties and methods. A sollution in that direction would also be
appreciated.

Regards,
Henk
 
P

Pavel Minaev

Is it possible to 'automatically' expose the members of a contained
class in C# (just as with COM) that implements some interface that
that the containing class wants to implement by delegating to the
contained class? Off course it is possible to implement all the
properties and methods in the containing class by calling the same
property on the contained class, but if it is possible I would like to
avoid that.

If you mean COM aggregation, then no, there isn't anything analogous
in C# (or in CLR, for that matter). Not in the upcoming 4.0, either
(at least judging by the declared feature list).

The only .NET language I'm aware of that provides such a capability is
Delphi Prism (aka Chrome, aka Oxygene) - see
http://prismwiki.codegear.com/en/Implements_(keyword)#Implements_to_Delegate_Entire_Interfaces
Another possibility would be generating the code that implements the
properties and methods. A sollution in that direction would also be
appreciated.

I see two possibilities here.

First is to use PostSharp. I'm not so sure all bits would connect
here, as PostSharp runs after compilation, so your class would be
treated as abstract by the C# compiler (as it doesn't implement the
interface at that point), which obviously complicates things.

Another is to use your own code generator, and probably partial
classes in separate code files. You can create a Visual Studio custom
tool for that - VS will run it wherever your hand-coded .cs file
changes, so you can regenerate the autogenerated part as needed.
This article explains the details:

http://visualstudiomagazine.com/features/article.aspx?editorialsid=2635

If you use VS, it is probably the best option, since it will also
enable full intellisense support, and prevent VS editor from reporting
unimplemented interface members as errors. On the other hand, outside
VS (e.g. if you just use MSBuild directly), you might want to write
the code generator as an MSBuild task, and run it before compilation.
 
H

hvj

Pavel and Pete,

Very much thanks for the elaborate answers you have provided me with!
It's good to know I am not missing something obvious and thanks for
the links to code generation. Guys like you are gold for the .NET
community!

Regards,
Henk
 

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