Click to See Complete Forum and Search --> : Templated CLI Base Class + Derived class = C# Intellisense Unhappy??


ebrandt
July 26th, 2007, 10:33 PM
Hi-

I have run into a behavior in CLI that I do not understand and would prefer not to happen. I don't know if this could be classified as a VS2005 bug, or if there is an explanation for it. If this is expected, I'd like to know a workaround...

I have a CLI Assembly (dll) with the following code:

namespace CLILib {
class Native
{
public:
int a;
};

template<class N>
public ref class Base
{
public:
int Mult(int a, int b) { return a * b; }
virtual int Mult2(int a, int b) { return a * b; }
};

public ref class Derived : Base<Native>
{
public:
int Add(int c, int d) { return c + d; }
};
}

So, basically, I have a base class that is templated, and a derived class that derives from it.

When I use Derived in C#, two things are a problem:

1. Mult() and Mult2() do not appear in the intellisense list

2. I get a compiler error calling Mult() "CLILib.Derived does not contain a definition for 'Mult'

Here is my C# code that produces these two problems:

using CLILib;
namespace CSConsole
{
class Program
{
static void Main(string[] args)
{
Derived d = new Derived();
d.Add(4, 5);
d.Mult(6, 7); // Mult does not appear in intellisense, and generates compiler error!
d.Mult2(6, 7); // Mult2 does not appear in intellisense, but is callable!
}
}
}

Now, if I remove the template from Base, everything works just fine - Intellisense shows Add, Mult, and Mult2 for class Derived.

The closest thing to an explanation I have found is in the second half of the 3rd bullet point on this page:

http://msdn2.microsoft.com/en-us/library/ms177213(vs.80).aspx

specifically, the text “If a template is not instantiated, it’s not emitted in the metadata. If a template is instantiated, only referenced member functions will appear in metadata.”

However, if I look at CLILib in the object browser (which as I understand it browses the metadata), I can see Mult and Mult2.

So, can anyone explain this behavior, and more improtantly, share with me how to fix it so that

a) I see the base class functions in the intellisense AND
b) I can call base class functions that are non-virtual.

Thanks in advance,
Eric.

PS. I know the code example above is not even using the template parameter, but I have simplified things as much as possible to still demonstrate the problem. In my 'real' code, I use the template parameter.

JamesSchumacher
July 27th, 2007, 06:32 PM
See the generic keyword when dealing with managed templates. It probably has to do with something that Microsoft overlooked on intellisense, which would probably mean that they are leaning to make 'template' for native only, and have generic be for CLI types.

It's probably wise to use template only for native code, and generic for CLI.