Hi, have been searching for an answer for this for quite a while,I would be greatfull for any help.
We are developing a codebase that of components that we wouldlike to be able to write in MC++ or C# depending on thesituation. These components are accessed through a standard setof interfaces that we have defined in C#. We are using"explicit implementation" for these interfaces as thesecomponents may implement many interfaces, often with similarmethod/property names, as well as for containment.
What I would like to do is have the components appear exacly thesame from the outside, but they currently do not. The problemis that when I explicitly implement an interface in C#, themethods of the interfaces are not accessible from the classinstance (desired result). On the other hand, when I implementthe same interface in MC++, the methods are accessible from theclass instance. (would like to avoid this)
Example code:
C# component:
__________________________________________________ ________________
namespace Foo
{
public interface IBar
{
void DoStuff();
}
public class Foo : IBar
{
Foo()
{
}
void IBar.DoStuff()
{
// Do Stuff Here
}
}
}
MC++ component:
__________________________________________________ ________________
// Foo2.h
namespace Foo
{
public __gc class CFoo2 : public IBar
{
CFoo2();
void IBar::DoStuff();
}
}
// Foo2.cpp
#include "Foo2.h"
CFoo2::CFoo2()
{
}
CFoo2::IBar::DoStuff()
{
// Do Stuff here
}
C# consumer of component:
__________________________________________________ ________________
using namespace Foo;
namespace user
{
public class User
{
User()
{
// Using the C# component
Foo CSComponent = new Foo();
//CSComponent.DoStuff(); // Compiler error
((IBar)CSComponent).DoStuff(); // No problem thisworks
// Using the MC++ component
Foo2 MCppComponent = new MCppComponent();
MCppComponent.DoStuff(); // This works but Iwish it wouldn't
((IBar)MCppComponent.DoStuff(); // Add of course thisworks
}
}
}
Is it possible to get the same behaviour, or do C# and MCpp treatexplicit interface implementation differently?
As a side note, I would appreciate any recommendation for goodliterature, websites for managed C++. Through all of myresearch on the web it appears that the documentation on MC++ israther slim, whereas for C# there seems to be a wealth ofinformation.
Much appreciated,
Peter
--------------------------------
From: Peter Insley
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>21c6H3XlqEWiJaWVQUeT/A==</Id>