[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

creation of a generic C#-UserControl over Com

01binbasti

9/26/2007 1:36:00 PM

Hello,



We have a problem with the creation of a generic C#-UserControl over Com.



We provided a basis class for our controls, which contains generics and
derives from UserControl. The actual Com-Visible-control, which we want to
use in the native-application, derives from this generic control, sets
generic accordingly and is Com-Visible. The assembly with this control has
been registered.



The native-application creates an instance of this control by using
CoCreateInstance included in Ole32. After that we check the support for
_Object from mscorlib (Id: {65074F7F-63C0-304E-AF0A-D51741CB4A8D}). The
created instance supports it.



After this check we call GetType() and get an access violation.

Why?



We hope someone can help us to solve this problem or at least understand it.



If we use a basis class without generics all will work fine.



The complete code is attached.



Bye. Sebastian.



Sources as Text:



Delphi:



procedure TForm1.btnTestClick(Sender: TObject);

var

lObj: _Object;

lType: _Type;

Instance: IUnknown;



begin

Instance :=
CreateComObject(StringToGUID('{B1CC6F34-8FEF-4f86-B704-1174CCF6422E}'));

if Supports(Instance, _Object, lObj) then

begin

try

lType := lObj.GetType();

if lType <> nil then

ShowMessage(IntToStr(lType.GetHashCode()));

except

on e: Exception do

ShowMessage(e.Message);

end;

end;

end;



C#:

namespace Control

{

public interface ITestIntf<T>

{

T GetData();

}



public abstract class TestControl<T> : UserControl, ITestIntf<T>

{

public abstract T GetData();

}



[ComVisible(true)]

[Guid("B1CC6F34-8FEF-4f86-B704-1174CCF6422E")]

[ClassInterface(ClassInterfaceType.None)]

public class MyTestControl : TestControl<string>

{

public override string GetData()

{

return "Hello";

}

}

}