5
votes

I'm trying to create a C# WinRT component for use in metro style applications (win8) and am having trouble with projected types.

It seems the IVector<> and IMap<> data types are inaccessible due to their protection level?

My Sample WinMD Library has one class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation.Collections;

namespace ClassLibrary1
{
    public sealed class Class1
    {
        public IVector<string> foo()
        {
            return new List<string>();
        }
    }
}

I get the following compiler errors:

Inconsistent accessibility: return type 'Windows.Foundation.Collections.IVector<string>' is less accessible than method 'ClassLibrary1.Class1.foo()'

'Windows.Foundation.Collections.IVector<string>' is inaccessible due to its protection level

What am I doing wrong?

EDIT:

Ah ha!

Turns out I should not be using the WinRT type names directly, but using their translated .NET names instead.

The correct code looks like this:

namespace ClassLibrary1
{
    public sealed class Class1
    {
        public IList<string> foo()
        {
            return new List<string>();
        }
    }
}
1
This is a WinRT component which uses native APIs at it's core. So it needs to project List<T> -> IVector<T>. See msdn.microsoft.com/en-us/library/windows/apps/… for more details. The mapping table is about halfway down the page. - Ty Norton

1 Answers

6
votes

IIterable<T> is projected to .NET as IEnumerable<T>, IVector<T> is projected as IList<T>, and IMap<T> is projected as IDictionary<T>. Projection goes both ways - both for consumption and for authoring - so you should simply always be using the projected versions of those interfaces in your .NET code. When you declare a member as returning IList<T>, it will show up as IVector<T> from WinRT perspective (e.g. from C++/CX). Similarly, if you implement IDictionary on your class, it'll show up as implementing IMap in C++/CX.

If I remember correctly, you should see all types already mapped as they should be when you use Object Browser for a .NET project.