Add back most CreateInstance APIs to AppDomain and Activator (#18751)
authorMarco Rossignoli <marco.rossignoli@gmail.com>
Tue, 3 Jul 2018 04:38:33 +0000 (06:38 +0200)
committerJan Kotas <jkotas@microsoft.com>
Tue, 3 Jul 2018 04:38:33 +0000 (06:38 +0200)
* add ObjectHandle.cs

* add signature placeholder

* move CreateInstance from netfx

* move CreateInstanceFrom from netfx

* nit: visibility, text formatting

src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems
src/System.Private.CoreLib/shared/System/Runtime/Remoting/ObjectHandle.cs [new file with mode: 0644]
src/System.Private.CoreLib/src/System/Activator.cs

index 59a3074..19c48e8 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\Vector256.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\Vector256DebugView.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Intrinsics\X86\Enums.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Remoting\ObjectHandle.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Serialization\IDeserializationCallback.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Serialization\IFormatterConverter.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Serialization\IObjectReference.cs" />
diff --git a/src/System.Private.CoreLib/shared/System/Runtime/Remoting/ObjectHandle.cs b/src/System.Private.CoreLib/shared/System/Runtime/Remoting/ObjectHandle.cs
new file mode 100644 (file)
index 0000000..a47aaf9
--- /dev/null
@@ -0,0 +1,25 @@
+// 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.
+
+namespace System.Runtime.Remoting
+{
+    public class ObjectHandle : MarshalByRefObject        
+    {
+        private object _wrappedObject;
+
+        private ObjectHandle()
+        {
+        }
+
+        public ObjectHandle(object o)
+        {
+            _wrappedObject = o;
+        }
+
+        public object Unwrap()
+        {
+            return _wrappedObject;
+        }
+    }
+}
index 05b8f37..eb0e6d6 100644 (file)
@@ -4,6 +4,8 @@
 
 using System.Reflection;
 using System.Globalization;
+using System.Runtime.Remoting;
+using System.Threading;
 
 namespace System
 {
@@ -62,6 +64,160 @@ namespace System
             throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
         }
 
+        [System.Security.DynamicSecurityMethod]
+        public static ObjectHandle CreateInstance(string assemblyName, string typeName)
+        {
+            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
+            return CreateInstanceInternal(assemblyName,
+                                          typeName,
+                                          false,
+                                          Activator.ConstructorDefault,
+                                          null,
+                                          null,
+                                          null,
+                                          null,
+                                          ref stackMark);
+        }
+
+        [System.Security.DynamicSecurityMethod]
+        public static ObjectHandle CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
+        {
+            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
+            return CreateInstanceInternal(assemblyName,
+                                          typeName,
+                                          ignoreCase,
+                                          bindingAttr,
+                                          binder,
+                                          args,
+                                          culture,
+                                          activationAttributes,
+                                          ref stackMark);
+        }
+
+        [System.Security.DynamicSecurityMethod]
+        public static ObjectHandle CreateInstance(string assemblyName, string typeName, object[] activationAttributes)
+        {
+            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
+            return CreateInstanceInternal(assemblyName,
+                                          typeName,
+                                          false,
+                                          Activator.ConstructorDefault,
+                                          null,
+                                          null,
+                                          null,
+                                          activationAttributes,
+                                          ref stackMark);
+        }
+
+        private static ObjectHandle CreateInstanceInternal(string assemblyString,
+                                                           string typeName,
+                                                           bool ignoreCase,
+                                                           BindingFlags bindingAttr,
+                                                           Binder binder,
+                                                           object[] args,
+                                                           CultureInfo culture,
+                                                           object[] activationAttributes,
+                                                           ref StackCrawlMark stackMark)
+        {
+            Type type = null;
+            Assembly assembly = null;
+            if (assemblyString == null)
+            {
+                assembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
+            }
+            else
+            {
+                RuntimeAssembly assemblyFromResolveEvent;
+                AssemblyName assemblyName = RuntimeAssembly.CreateAssemblyName(assemblyString, out assemblyFromResolveEvent);
+                if (assemblyFromResolveEvent != null)
+                {
+                    // Assembly was resolved via AssemblyResolve event
+                    assembly = assemblyFromResolveEvent;
+                }
+                else if (assemblyName.ContentType == AssemblyContentType.WindowsRuntime)
+                {
+                    // WinRT type - we have to use Type.GetType
+                    type = Type.GetType(typeName + ", " + assemblyString, true /*throwOnError*/, ignoreCase);
+                }
+                else
+                {
+                    // Classic managed type
+                    assembly = RuntimeAssembly.InternalLoadAssemblyName(
+                        assemblyName, null, ref stackMark,
+                        true /*thrownOnFileNotFound*/);
+                }
+            }
+
+            if (type == null)
+            {
+                // It's classic managed type (not WinRT type)
+                if (assembly == null)
+                    return null;
+
+                type = assembly.GetType(typeName, true /*throwOnError*/, ignoreCase);
+            }
+
+            object o = Activator.CreateInstance(type,
+                                                bindingAttr,
+                                                binder,
+                                                args,
+                                                culture,
+                                                activationAttributes);
+
+            return (o != null) ? new ObjectHandle(o) : null;          
+        }
+
+        public static ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName)
+        {
+            return CreateInstanceFrom(assemblyFile, typeName, null);
+        }
+
+        public static ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
+        {
+            return CreateInstanceFromInternal(assemblyFile,
+                                              typeName,
+                                              ignoreCase,
+                                              bindingAttr,
+                                              binder,
+                                              args,
+                                              culture,
+                                              activationAttributes);
+        }
+
+        public static ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName, object[] activationAttributes)
+        {
+            return CreateInstanceFrom(assemblyFile,
+                                      typeName,
+                                      false,
+                                      Activator.ConstructorDefault,
+                                      null,
+                                      null,
+                                      null,
+                                      activationAttributes);
+        }
+
+        private static ObjectHandle CreateInstanceFromInternal(string assemblyFile,
+                                                               string typeName,
+                                                               bool ignoreCase,
+                                                               BindingFlags bindingAttr,
+                                                               Binder binder,
+                                                               object[] args,
+                                                               CultureInfo culture,
+                                                               object[] activationAttributes)
+        {
+            Assembly assembly = Assembly.LoadFrom(assemblyFile);
+            Type t = assembly.GetType(typeName, true, ignoreCase);
+
+            object o = Activator.CreateInstance(t,
+                                                bindingAttr,
+                                                binder,
+                                                args,
+                                                culture,
+                                                activationAttributes);
+
+            return (o != null) ? new ObjectHandle(o) : null;
+        }
+
         public static T CreateInstance<T>()
         {
             var rt = (RuntimeType)typeof(T);