Delegate constraint restriction

N

Nick Hall

As my first delve into the version 2.0 framework I am upgrading an
application which registers/unregisters ActiveX components. One of the
things I wanted to do was update the code which handles the registration of
DLLs (previously it had used the technique of creating an in-memory type
with suitable P/Invoke declarations).

I have created a wrapper round the LoadLibrary/GetProcAddress DLL calls
which seems to be working fine and provides the following method to access
the required function: -

Public Overloads Function GetProcedure(ByVal procedureName As String, ByVal
procedureType As Type) As [Delegate]

It struck me that this would be a good place to make use of the new Generics
feature as the current version is not particularly type-safe. It would be
nice if the function could derive the proper delegate type from the passed
variable so I wanted to create the following: -

Public Overloads Sub GetProcedure(Of T As [Delegate])(ByVal procedureName As
String, <Out()> ByRef procedureDelegate As T)

(Note the parameter is passed ByRef rather than as a function return in
order to take advantage of type inference)

VB does not like this at all. It complains that "'Delegate' cannot be used
as a type constraint". It seems also that you cannot use any user-defined
delegates in the constraint either (the obvious one to try being
MultiCastDelegate from which virtually all other delegates are derived). I
cannot find any reference in the documentation for such a restriction.

At the end of the day I have a usable way round this problem (this is not
live code in any case). However I am interested in knowing why this
restriction has been put in place (on the face of it I cannot see a sensible
reason for it).

Anyone have any ideas?

Regards,

Nick Hall
 

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