summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ManagedActivationFactory.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ManagedActivationFactory.cs')
-rw-r--r--src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ManagedActivationFactory.cs76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ManagedActivationFactory.cs b/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ManagedActivationFactory.cs
new file mode 100644
index 0000000000..3e93428d26
--- /dev/null
+++ b/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/ManagedActivationFactory.cs
@@ -0,0 +1,76 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+//
+
+using System;
+using System.Diagnostics.Contracts;
+using System.Reflection;
+using System.Runtime.InteropServices;
+using System.Security;
+using System.Runtime.CompilerServices;
+
+namespace System.Runtime.InteropServices.WindowsRuntime
+{
+ [ComImport]
+ [Guid("60D27C8D-5F61-4CCE-B751-690FAE66AA53")]
+ [WindowsRuntimeImport]
+ internal interface IManagedActivationFactory
+ {
+ void RunClassConstructor();
+ }
+
+ // A ManangedActivationFactory provides the IActivationFactory implementation for managed types which are
+ // constructable via Windows Runtime. Implementation of specialized factory and static WinRT interfaces is
+ // provided using VM functionality (see Marshal.InitializeWinRTFactoryObject for details).
+ //
+ // In order to be activatable via the ManagedActivationFactory type, the type must be decorated with either
+ // ActivatableAttribute, or StaticAttribute.
+ [ComVisible(true)]
+ [ClassInterface(ClassInterfaceType.None)]
+ internal sealed class ManagedActivationFactory : IActivationFactory, IManagedActivationFactory
+ {
+ private Type m_type;
+
+ [SecurityCritical]
+ internal ManagedActivationFactory(Type type)
+ {
+ if (type == null)
+ throw new ArgumentNullException("type");
+
+ // Check whether the type is "exported to WinRT", i.e. it is declared in a managed .winmd and is decorated
+ // with at least one ActivatableAttribute or StaticAttribute.
+ if (!(type is RuntimeType) || !type.IsExportedToWindowsRuntime)
+ throw new ArgumentException(Environment.GetResourceString("Argument_TypeNotActivatableViaWindowsRuntime", type), "type");
+
+ m_type = type;
+ }
+
+ // Activate an instance of the managed type by using its default constructor.
+ public object ActivateInstance()
+ {
+ try
+ {
+ return Activator.CreateInstance(m_type);
+ }
+ catch (MissingMethodException)
+ {
+ // If the type doesn't expose a default constructor, then we fail with E_NOTIMPL
+ throw new NotImplementedException();
+ }
+ catch (TargetInvocationException e)
+ {
+ throw e.InnerException;
+ }
+ }
+
+ // Runs the class constructor
+ // Currently only Jupiter use this to run class constructor in order to
+ // initialize DependencyProperty objects and do necessary work
+ void IManagedActivationFactory.RunClassConstructor()
+ {
+ RuntimeHelpers.RunClassConstructor(m_type.TypeHandle);
+ }
+ }
+}