﻿// Decompiled with JetBrains decompiler
// Type: System.StubHelpers.SystemTypeMarshaler
// Assembly: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 255ABCDF-D9D6-4E3D-BAD4-F74D4CE3D7A8
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll

using System;
using System.Runtime;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security;

namespace System.StubHelpers
{
  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
  internal static class SystemTypeMarshaler
  {
    [SecurityCritical]
    [ForceTokenStabilization]
    internal static unsafe void ConvertToNative(Type managedType, TypeNameNative* pNativeType)
    {
      if (!Environment.IsWinRTSupported)
        throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_WinRT"));
      string sourceString;
      if (managedType != (Type) null)
      {
        if (managedType.GetType() != typeof (RuntimeType))
          throw new ArgumentException(Environment.GetResourceString("Argument_WinRTSystemRuntimeType", (object) managedType.GetType().ToString()));
        bool isPrimitive;
        string str = WinRTTypeNameConverter.ConvertToWinRTTypeName(managedType, out isPrimitive);
        if (str != null)
        {
          sourceString = str;
          pNativeType->typeKind = !isPrimitive ? TypeKind.Metadata : TypeKind.Primitive;
        }
        else
        {
          sourceString = managedType.AssemblyQualifiedName;
          pNativeType->typeKind = TypeKind.Projection;
        }
      }
      else
      {
        sourceString = "";
        pNativeType->typeKind = TypeKind.Projection;
      }
      Marshal.ThrowExceptionForHR(UnsafeNativeMethods.WindowsCreateString(sourceString, sourceString.Length, &pNativeType->typeName), new IntPtr(-1));
    }

    [ForceTokenStabilization]
    [SecurityCritical]
    internal static unsafe void ConvertToManaged(TypeNameNative* pNativeType, ref Type managedType)
    {
      if (!Environment.IsWinRTSupported)
        throw new PlatformNotSupportedException(Environment.GetResourceString("PlatformNotSupported_WinRT"));
      string typeName = WindowsRuntimeMarshal.HStringToString(pNativeType->typeName);
      if (string.IsNullOrEmpty(typeName))
        managedType = (Type) null;
      else if (pNativeType->typeKind == TypeKind.Projection)
      {
        managedType = Type.GetType(typeName, true);
      }
      else
      {
        bool isPrimitive;
        managedType = WinRTTypeNameConverter.GetTypeFromWinRTTypeName(typeName, out isPrimitive);
        if (isPrimitive != (pNativeType->typeKind == TypeKind.Primitive))
          throw new ArgumentException(Environment.GetResourceString("Argument_Unexpected_TypeSource"));
      }
    }

    [ForceTokenStabilization]
    [SecurityCritical]
    internal static unsafe void ClearNative(TypeNameNative* pNativeType)
    {
      IntPtr num = pNativeType->typeName;
      UnsafeNativeMethods.WindowsDeleteString(pNativeType->typeName);
    }
  }
}
