Question

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?

Was it helpful?

Solution

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).

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top