1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
6 using System.Reflection;
7 using System.Runtime.CompilerServices;
9 namespace System.Runtime.InteropServices.WindowsRuntime
12 [Guid("60D27C8D-5F61-4CCE-B751-690FAE66AA53")]
13 [WindowsRuntimeImport]
14 internal interface IManagedActivationFactory
16 void RunClassConstructor();
19 // A ManangedActivationFactory provides the IActivationFactory implementation for managed types which are
20 // constructable via Windows Runtime. Implementation of specialized factory and static WinRT interfaces is
21 // provided using VM functionality (see Marshal.InitializeWinRTFactoryObject for details).
23 // In order to be activatable via the ManagedActivationFactory type, the type must be decorated with either
24 // ActivatableAttribute, or StaticAttribute.
26 [ClassInterface(ClassInterfaceType.None)]
27 internal sealed class ManagedActivationFactory : IActivationFactory, IManagedActivationFactory
31 internal ManagedActivationFactory(Type type)
34 throw new ArgumentNullException(nameof(type));
36 // Check whether the type is "exported to WinRT", i.e. it is declared in a managed .winmd and is decorated
37 // with at least one ActivatableAttribute or StaticAttribute.
38 if (!(type is RuntimeType) || !type.IsExportedToWindowsRuntime)
39 throw new ArgumentException(SR.Format(SR.Argument_TypeNotActivatableViaWindowsRuntime, type), nameof(type));
44 // Activate an instance of the managed type by using its default constructor.
45 public object ActivateInstance()
49 return Activator.CreateInstance(m_type)!;
51 catch (MissingMethodException)
53 // If the type doesn't expose a default constructor, then we fail with E_NOTIMPL
54 throw new NotImplementedException();
56 catch (TargetInvocationException e)
58 throw e.InnerException!;
62 // Runs the class constructor
63 // Currently only Jupiter use this to run class constructor in order to
64 // initialize DependencyProperty objects and do necessary work
65 void IManagedActivationFactory.RunClassConstructor()
67 RuntimeHelpers.RunClassConstructor(m_type.TypeHandle);