From: Marco Rossignoli Date: Tue, 3 Jul 2018 04:38:33 +0000 (+0200) Subject: Add back most CreateInstance APIs to AppDomain and Activator (#18751) X-Git-Tag: accepted/tizen/unified/20190422.045933~1758 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c7c50c5cd54b91c1f982fda8e5c8b21aead3f352;p=platform%2Fupstream%2Fcoreclr.git Add back most CreateInstance APIs to AppDomain and Activator (#18751) * add ObjectHandle.cs * add signature placeholder * move CreateInstance from netfx * move CreateInstanceFrom from netfx * nit: visibility, text formatting --- diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 59a3074..19c48e8 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -496,6 +496,7 @@ + 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 index 0000000..a47aaf9 --- /dev/null +++ b/src/System.Private.CoreLib/shared/System/Runtime/Remoting/ObjectHandle.cs @@ -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; + } + } +} diff --git a/src/System.Private.CoreLib/src/System/Activator.cs b/src/System.Private.CoreLib/src/System/Activator.cs index 05b8f37..eb0e6d6 100644 --- a/src/System.Private.CoreLib/src/System/Activator.cs +++ b/src/System.Private.CoreLib/src/System/Activator.cs @@ -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() { var rt = (RuntimeType)typeof(T);