Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.NET/LINK USER GUIDE Comment: Calling static generic C# method

27 views
Skip to first unread message

wollmich

unread,
Nov 21, 2009, 3:41:23 AM11/21/09
to
Hi,

I've a problem to call a static generic C# method from Mathematica and
I don't find any example on the web or in your user guide.
http://www.wolfram.com/learningcenter/tutorialcollection/NETLinkUserGuide/NETLinkUserGuide.pdf

Calling a non static class (this works):
Type1 = LoadNETType["DummyLib.Class1"];
Object1 = NETNew[Type1];
Object1@Method1[];

Calling a static method in a static class (this works):
Type2 = LoadNETType["DummyLib.Class2"];
Class2`Method2[];

Calling a non static but generic class (this works):
Type3 = LoadNETType["DummyLib.Class3`1[[DummyLib.Class1]]"];
Object3 = NETNew[Type3];
Object3@Method3[];

Calling a static generic method in a static class (THIS DOESN'T
WORK!!!!) I've no idea how to call it???
The method could look like that in C#:
C#-Code:
namespace DummyLib
{
public static class Class4
{
public static T Method4<T>()
where T : new()
{
return new T();
}
}
}

With NETTypeInfo the method is listet, but I don't no idea how to pass
the type parameter?
Type4 = LoadNETType["DummyLib.Class4"];
NETTypeInfo[Type4];

Can somebody please help me. Thanks.

Regards Wollmich

wollmich

unread,
Nov 24, 2009, 5:47:16 AM11/24/09
to
On 21 Nov., 09:41, wollmich <wollm...@gmail.com> wrote:
> Hi,
>
> I've a problem to call a static generic C# method from Mathematica and
> I don't find any example on the web or in your user guide.http://www.wolf=
ram.com/learningcenter/tutorialcollection/NETLinkUserG...

I've a solution which works:

NETCallGenericStaticMethod[classtype_ method_, type_, arguments___] :=
Module[{m1, m2, t1, t2},
t1 = GetTypeObject[classtype];
m1 = t1@GetMethod[method];
t2 = GetTypeObject[type];
m2 = m1@MakeGenericMethod[{t2}];
Return[m2@Invoke[Null, arguments]]];

Any other idea?

Reagrs Wollmich

Todd Gayley

unread,
Nov 25, 2009, 11:03:55 PM11/25/09
to
Reagrs,

Your solution is exactly correct, and you can give yourself some
well-deserved congratulations for figuring it out. You can do
arbitrarily complicated machinations with generics in .NET/Link
already, but .NET/Link really needs some added syntax for making
generics easier to use, as well as more documentation.

One complication for your NETCallGenericStaticMethod function is that
the GetMethod call fails if the static method has overloads. You can
work around this by calling GetMethods[] and then iterating through
the list to find the one you want. I give an example of this, as well
as some more generics-related techniques, in a notebook I wrote a
little while ago called "Using LINQ from Mathematica." It is of
interest to anyone working with generics in .NET/Link, not
necessarily using LINQ. I have sent this to you separately, and I'd
be happy to send it to anyone else who is interested. Its content is
being incorporated into the .NET/Link documentation for future
versions of Mathematica.

Todd Gayley
Wolfram Research


At 04:47 AM 11/24/2009, wollmich wrote:
>On 21 Nov., 09:41, wollmich <wollm...@gmail.com> wrote:

> > Hi,
> >
> > I've a problem to call a static generic C# method from Mathematica and

> > I don't find any example on the web or in your user guide.http://www.wolf=
>ram.com/learningcenter/tutorialcollection/NETLinkUserG...

fd

unread,
Nov 27, 2009, 6:29:17 AM11/27/09
to
On Nov 26, 3:03 pm, Todd Gayley <tgay...@wolfram.com> wrote:
> Reagrs,
>
> Your solution is exactly correct, and you can give yourself some
> well-deserved congratulations for figuring it out. You can do
> arbitrarily complicated machinations with generics in .NET/Link
> already, but .NET/Link really needs some added syntax for making
> generics easier to use, as well as more documentation.
>
> One complication for your NETCallGenericStaticMethod function is that
> the GetMethod call fails if the static method has overloads. You can
> work around this by calling GetMethods[] and then iterating through
> the list to find the one you want. I give an example of this, as well
> as some more generics-related techniques, in a notebook I wrote a
> little while ago called "Using LINQ from Mathematica." It is of
> interest to anyone working with generics in .NET/Link, not
> necessarily using LINQ. I have sent this to you separately, and I'd
> be happy to send it to anyone else who is interested. Its content is
> being incorporated into the .NET/Link documentation for future
> versions of Mathematica.
>
> Todd Gayley
> Wolfram Research
>
> At 04:47 AM 11/24/2009, wollmich wrote:
>
> >On 21 Nov., 09:41, wollmich <wollm...@gmail.com> wrote:
> > > Hi,
>
> > > I've a problem to call a static generic C# method from Mathematica and
> > > I don't find any example on the web or in your user guide.http://www.wolfram.com/learningcenter/tutorialcollection/NETLinkUserG...

First, Congrats to wollmich, I tried to tackle this problem but I gave
up, well done!!

I use NET/Link quite a lot to help testing our applications and I
would be interested in obtaining the document todd has mentioned, if
possible, can I have a copy?

Best
Felipe


0 new messages