Re: How to expose generic classes to COM?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Thank you Peter for your answer. Yes, it seems that we will need to go that
route.

To avoid delegating all the methods we are planning to do something like:

public class MyMatrixInt : MyMatrix<int>, IMatrixInt
{
}

public class MyMatrixString : MyMatrix<string>, IMatrixString
{
}

Best regards,
Patricio.

""Peter Huang" [MSFT]" <v-phuang@xxxxxxxxxxxxxxxxxxxx> wrote in message
news:3nck73XpGHA.2028@xxxxxxxxxxxxxxxxxxxxxxxx
Hi Patricio,

Based on my testing, it is hard to do that.
Because IDL did not support Generic, it is hard to know how many class we
should generate for the generics class.
e.g.
For the interface IMatrix<T>, we did not know how many class should be
expose to COM and generate in the TLB file, because the generics is done
in
the compile time.
IMatrix<int>,IMatrix<UInt>,IMatrix<Some cusomtized data type>..........

For your scenario, I think you may have to write four interface(no
generics
and implement them in the same class)
But the same implement detailed can be in the same class, and the four
interface implement can be delegated to the class implement detailed.
Here is some code snippet for your reference only.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace GenericsClassCOM
{
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMatrixInt
{
int Test(int i);
}
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IMatrixString
{
string Test(string s);
}
[ClassInterface(ClassInterfaceType.None)]
public class MyMatrix : IMatrixInt,IMatrixString
{
#region IMatrixInt Members

public int Test(int i)
{
return (new MatrixClass<int>()).Test(i);
}

#endregion

#region IMatrixString Members

public string Test(string s)
{
return (new MatrixClass<string>()).Test(s);
}

#endregion
}

internal class MatrixClass<T>
{
public T Test(T t)
{
//Concrete implement.
//
//
return t;
}
}
}



Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.



.