C#/WinrtRT での WinRT 型の .NET マッピング

この記事では、.NET 6 (以降) を対象とするデスクトップ アプリの Windows ランタイム (WinRT) 型と .NET 型の間で C#/WinRT が行うマッピングの一覧を示します。 これらのアプリでは、Visual Studio IntelliSense は、Windows ランタイム型ではなく .NET 型を表示します。 たとえば、Windows ランタイム メソッドが IVector<string> 型のパラメーターを受け取る場合、IntelliSense は IList<string> 型のパラメーターを表示します。 同様に、C#/WinRT を使用して作成された WinRT コンポーネントでは、メンバーのシグネチャで .NET 型を使用します。 C#/WinRT を使用して Windows ランタイム コンポーネントを生成すると、.NET 型は対応する WinRT 型に変換されます。

C#/WinRT カスタム型マッピングは、Windows SDK または WinUI 3 の型によって分類されます (WinUI 3 は Windows App SDK の一部です)。 Windows SDK マッピングの WinRT 型は、Windows.* 名前空間に存在し、WinUI 3 マッピングの WinRT 型は、Microsoft.UI.Xaml.* 名前空間に存在します。 C#/WinRT が WinRT 型に対して行うカスタム型マッピングには、次の 2 つの理由があります。

  • .NET 型にマップする WinRT 型 (名前や名前空間が異なるもの)。 これらのカスタム マッピングは、WinRT 型を既存の .NET と同等の型にマッピングするために使用されます。 また、マッピングが別の型に対して行われる場合があります (値型がクラス型にマップされるなど)。

  • 同じ名前と名前空間を持つ .NET 型にマップされる WinRT 型。 これらのカスタム マッピングは、一般にパフォーマンスや強化の理由から使用され、C# で直接実装されます。 WinRT と .NET で同じ名前空間名と型名を持つ型のほとんどは、構造体 (または列挙型など、構造体に関連付けられている型) です。 WinRT では、構造体にはフィールド以外にメンバーがなく、また、.NET で非表示にするヘルパー型が必要です。 これらの構造体の .NET バージョンには、非表示のヘルパー型の機能を提供するプロパティとメソッドがあります (例: Windows.UI.Color)。

Note

UWP アプリのコンテキストでの WinRT 型と .NET 型間のマッピングの一覧については、UWP での WinRT 型の .NET マッピングに関するページを参照してください。

Windows SDK での WinRT 型のマッピング

名前や名前空間が異なる型

WinRT の型/名前空間 .NET 型/名前空間
DateTime (Windows.Foundation) DateTimeOffset (System)
EventHandler<T> (Windows.Foundation) EventHandler<T> (System)
EventRegistrationToken (Windows.Foundation) EventRegistrationToken (WinRT)
HResult (Windows.Foundation) Exception (System)
IClosable (Windows.Foundation) IDisposable (System)
IReference<T> (Windows.Foundation) Nullable<T> (System)
TimeSpan (Windows.Foundation) TimeSpan (System)
Uri (Windows.Foundation) Uri (System)
IIterable<T> (Windows.Foundation.Collections) IEnumerable<T> (System.Collections.Generic)
IIterator<T> (Windows.Foundation.Collections) IEnumerator<T> (System.Collections.Generic)
IMap<K,V> (Windows.Foundation.Collections) IDictionary<TKey,TValue> (System.Collections.Generic)
IMapView<K,V> (Windows.Foundation.Collections) IReadOnlyDictionary<TKey,TValue> (System.Collections.Generic)
IKeyValuePair<K,V> (Windows.Foundation.Collections) KeyValuePair<TKey,TValue> (System.Collections.Generic)
IVector<T> (Windows.Foundation.Collections) IList<T> (System.Collections.Generic)
IVectorView<T> (Windows.Foundation.Collections) IReadOnlyList<T> (System.Collections.Generic)
AttributeTargets (Windows.Foundation.Metadata) AttributeTargets (System)
AttributeUsageAttribute (Windows.Foundation.Metadata) AttributeUsageAttribute (System)
Matrix3x2 (Windows.Foundation.Numerics) Matrix3x2 (System.Numerics)
Matrix4x4 (Windows.Foundation.Numerics) Matrix4x4 (System.Numerics)
Plane (Windows.Foundation.Numerics) Plane (System.Numerics)
Quaternion (Windows.Foundation.Numerics) Quaternion (System.Numerics)
Vector2 (Windows.Foundation.Numerics) Vector2 (System.Numerics)
Vector3 (Windows.Foundation.Numerics) Vector3 (System.Numerics)
Vector4 (Windows.Foundation.Numerics) Vector4 (System.Numerics)
IBindableIterable (Windows.UI.Xaml.Interop) IEnumerable (System.Collections)
IBindableVector (Windows.UI.Xaml.Interop) IList (System.Collections)
TypeName (Windows.UI.Xaml.Interop) Type (System)

名前と名前空間が同じ型

Type 名前空間
IPropertyValue Windows.Foundation
IReferenceArray<T> Windows.Foundation
Point Windows.Foundation
矩形 Windows.Foundation
サイズ Windows.Foundation
Color Windows.UI
TypeKind Windows.UI.Xaml.Interop

WinUI での WinRT 型のマッピング

名前や名前空間が異なる型

WinRT の型/名前空間 .NET 型/名前空間
INotifyCollectionChanged (Microsoft.UI.Xaml.Data) INotifyCollectionChanged (System.Collections.Specialized)
NotifyCollectionChangedEventHandler (Microsoft.UI.Xaml.Data) NotifyCollectionChangedEventHandler (System.Collections.Specialized)
NotifyCollectionChangedEventArgs (Microsoft.UI.Xaml.Data) NotifyCollectionChangedEventArgs (System.Collections.Specialized)
NotifyCollectionChangedAction (Microsoft.UI.Xaml.Data) NotifyCollectionChangedAction (System.Collections.Specialized)
DataErrorsChangedEventArgs (Microsoft.UI.Xaml.Data) DataErrorsChangedEventArgs (System.ComponentModel)
INotifyDataErrorInfo (Microsoft.UI.Xaml.Data) INotifyDataErrorInfo (System.ComponentModel)
INotifyPropertyChanged (Microsoft.UI.Xaml.Data) INotifyPropertyChanged (System.ComponentModel)
PropertyChangedEventHandler (Microsoft.UI.Xaml.Data) PropertyChangedEventHandler (System.ComponentModel)
PropertyChangedEventArgs (Microsoft.UI.Xaml.Data) PropertyChangedEventArgs (System.ComponentModel)
ICommand (Microsoft.UI.Xaml.Input) ICommand (System.Windows.Input)
IXamlServiceProvider (Microsoft.UI.Xaml) IServiceProvider (System)

名前と名前空間が同じ型

Type 名前空間
CornerRadius Microsoft.UI.Xaml
Duration Microsoft.UI.Xaml
DurationType Microsoft.UI.Xaml
GridLength Microsoft.UI.Xaml
GridUnitType Microsoft.UI.Xaml
厚さ Microsoft.UI.Xaml
GeneratorPosition Microsoft.UI.Xaml.Controls.Primitives
Matrix Microsoft.UI.Xaml.Media
KeyTime Microsoft.UI.Xaml.Media.Animation
RepeatBehavior Microsoft.UI.Xaml.Media.Animation
RepeatBehaviorType Microsoft.UI.Xaml.Media.Animation
Matrix3D (Microsoft.UI.Xaml.Media.Media3D