Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Runtime methods cannot be generic - Reasons, Workarounds, Alternatives?

Here is an Interface, taken from my attempt to port MemBus, an Event Aggregator I maintain, to the Windows runtime world:

public interface ISubscriber
{
    IDisposable Subscribe<M>(Action<M> subscription);

    #if WINRT 
    [Windows.Foundation.Metadata.DefaultOverload]
    #endif
    IDisposable Subscribe(object subscriber);

    IObservable<M> Observe<M>();
}

What I am getting is a Compiler error:

"error WME1031: 'MemBus.ISubscriber.Subscribe<M>(System.Action<M>)' is a generic method. Windows Runtime methods cannot be generic."

I have only suspicions as to why this is the case, if anybody can clarify, please do.

My main question is: How are we supposed to deal with this when porting code to the Windows runtime?

A lot of higher Level functionality happens with generics. In fact, we use generic classes in the Windows Runtime (e.g. List<T>). How is a RT component supposed to expose generic types, and if not, is the only alternative available to go back to the ways of writing .NET 1.1 code, i.e. object in, object out and do casts?

like image 321
flq Avatar asked Sep 04 '12 16:09

flq


2 Answers

You need to choose a Class Library template from the Windows Store templates (not the Windows Runtime Component template).

Screenshot VS2012

This will allow you to use the full C# language in a Windows Store app, but won't allow other languages like C++ and JS to consume the assembly (as they don't support generics in the same way that C# does).

like image 177
Brendan Forster Avatar answered Oct 21 '22 07:10

Brendan Forster


The short story is that JavasScript does not know how to handle generics, so a WinRT component cannot use them. See WinRT reason for disallowing custom generic types or interfaces for a longer discussion.

like image 30
Jeff Brand Avatar answered Oct 21 '22 08:10

Jeff Brand