Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Activator.cs / 1305376 / Activator.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // // Activator is an object that contains the Activation (CreateInstance/New) // methods for late bound support. // // // // namespace System { using System; using System.Reflection; using System.Runtime.Remoting; #if FEATURE_REMOTING using System.Runtime.Remoting.Activation; using Message = System.Runtime.Remoting.Messaging.Message; #endif using System.Security; using CultureInfo = System.Globalization.CultureInfo; using Evidence = System.Security.Policy.Evidence; using StackCrawlMark = System.Threading.StackCrawlMark; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Security.Permissions; using AssemblyHashAlgorithm = System.Configuration.Assemblies.AssemblyHashAlgorithm; using System.Runtime.Versioning; using System.Diagnostics.Contracts; // Only statics, does not need to be marked with the serializable attribute [ClassInterface(ClassInterfaceType.None)] [ComDefaultInterface(typeof(_Activator))] [System.Runtime.InteropServices.ComVisible(true)] public sealed class Activator : _Activator { internal const int LookupMask = 0x000000FF; internal const BindingFlags ConLookup = (BindingFlags) (BindingFlags.Instance | BindingFlags.Public); internal const BindingFlags ConstructorDefault= BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance; // This class only contains statics, so hide the worthless constructor private Activator() { } // CreateInstance // The following methods will create a new instance of an Object // Full Binding Support // For all of these methods we need to get the underlying RuntimeType and // call the Impl version. [System.Security.SecuritySafeCritical] // auto-generated static public Object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) { return CreateInstance(type, bindingAttr, binder, args, culture, null); } [System.Security.SecuritySafeCritical] // auto-generated static public Object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) { if ((object)type == null) throw new ArgumentNullException("type"); Contract.EndContractBlock(); #if !FEATURE_REFLECTION_EMIT_REFACTORING if (type is System.Reflection.Emit.TypeBuilder) throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder")); #endif //!FEATURE_REFLECTION_EMIT_REFACTORING // If they didn't specify a lookup, then we will provide the default lookup. if ((bindingAttr & (BindingFlags) LookupMask) == 0) bindingAttr |= Activator.ConstructorDefault; if (activationAttributes != null && activationAttributes.Length > 0){ // If type does not derive from MBR // throw notsupportedexception #if FEATURE_REMOTING if(type.IsMarshalByRef){ // The fix below is preventative. // if(!(type.IsContextful)){ if(activationAttributes.Length > 1 || !(activationAttributes[0] is UrlAttribute)) throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR")); } } else #endif throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR" )); } RuntimeType rt = type.UnderlyingSystemType as RuntimeType; if (rt == null) throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"),"type"); return rt.CreateInstanceImpl(bindingAttr,binder,args,culture,activationAttributes); } [System.Security.SecuritySafeCritical] // auto-generated static public Object CreateInstance(Type type, params Object[] args) { return CreateInstance(type, Activator.ConstructorDefault, null, args, null, null); } [System.Security.SecuritySafeCritical] // auto-generated static public Object CreateInstance(Type type, Object[] args, Object[] activationAttributes) { return CreateInstance(type, Activator.ConstructorDefault, null, args, null, activationAttributes); } [System.Security.SecuritySafeCritical] // auto-generated static public Object CreateInstance(Type type) { return Activator.CreateInstance(type, false); } /* * Create an instance using the name of type and the assembly where it exists. This allows * types to be created remotely without having to load the type locally. */ [System.Security.SecuritySafeCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable static public ObjectHandle CreateInstance(String assemblyName, String typeName) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return CreateInstance(assemblyName, typeName, false, Activator.ConstructorDefault, null, null, null, null, null, ref stackMark); } [System.Security.SecuritySafeCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable static public ObjectHandle CreateInstance(String assemblyName, String typeName, Object[] activationAttributes) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return CreateInstance(assemblyName, typeName, false, Activator.ConstructorDefault, null, null, null, activationAttributes, null, ref stackMark); } static public Object CreateInstance(Type type, bool nonPublic) { if ((object)type == null) throw new ArgumentNullException("type"); Contract.EndContractBlock(); RuntimeType rt = type.UnderlyingSystemType as RuntimeType; if (rt == null) throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type"); return rt.CreateInstanceDefaultCtor(!nonPublic); } [System.Security.SecuritySafeCritical] // auto-generated static public T CreateInstance() { RuntimeType rt = typeof(T) as RuntimeType; // This is a hack to maintain compatibility with V2. Without this we would throw a NotSupportedException for void[]. // Array, Ref, and Pointer types don't have default constructors. if (rt.HasElementType) throw new MissingMethodException(Environment.GetResourceString("Arg_NoDefCTor")); // The caller must have access to T already. Otherwise it cannot even reference this instantiation. // Skip the CreateInstanceCheckThis call to avoid perf cost and to maintain compatibility with V2 (throwing the same exceptions). return (T) rt.CreateInstanceDefaultCtor(true /*publicOnly*/, true /*skipVisibilityChecks*/, true /*skipCheckThis*/, true /*fillCache*/); } [System.Security.SecuritySafeCritical] // auto-generated [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] static public ObjectHandle CreateInstanceFrom(String assemblyFile, String typeName) { return CreateInstanceFrom(assemblyFile, typeName, null); } [System.Security.SecuritySafeCritical] // auto-generated [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] static public ObjectHandle CreateInstanceFrom(String assemblyFile, String typeName, Object[] activationAttributes) { return CreateInstanceFrom(assemblyFile, typeName, false, Activator.ConstructorDefault, null, null, null, activationAttributes); } [System.Security.SecuritySafeCritical] // auto-generated [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable [Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstance which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")] static public ObjectHandle CreateInstance(String assemblyName, String typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo) { StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller; return CreateInstance(assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityInfo, ref stackMark); } [SecuritySafeCritical] [MethodImplAttribute(MethodImplOptions.NoInlining)] // Methods containing StackCrawlMark local var has to be marked non-inlineable 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 CreateInstance(assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, null, ref stackMark); } [System.Security.SecurityCritical] // auto-generated static internal ObjectHandle CreateInstance(String assemblyName, String typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, ref StackCrawlMark stackMark) { #if FEATURE_CAS_POLICY if (securityInfo != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit")); } #endif // FEATURE_CAS_POLICY Assembly assembly; if(assemblyName == null) assembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark); else assembly = RuntimeAssembly.InternalLoad(assemblyName, securityInfo, ref stackMark, false); Log(assembly != null, "CreateInstance:: ", "Loaded " + assembly.FullName, "Failed to Load: " + assemblyName); if(assembly == null) return null; Type t = assembly.GetType(typeName, true, ignoreCase); Object o = Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes); Log(o != null, "CreateInstance:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName); if(o == null) return null; else { ObjectHandle Handle = new ObjectHandle(o); return Handle; } } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] [Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")] static public ObjectHandle CreateInstanceFrom(String assemblyFile, String typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo) { #if FEATURE_CAS_POLICY if (securityInfo != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit")); } #endif // FEATURE_CAS_POLICY return CreateInstanceFromInternal(assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityInfo); } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] 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, null); } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] [SecuritySafeCritical] private static ObjectHandle CreateInstanceFromInternal(String assemblyFile, String typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo) { #if FEATURE_CAS_POLICY Contract.Assert(AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled || securityInfo == null); #endif // FEATURE_CAS_POLICY #pragma warning disable 618 Assembly assembly = Assembly.LoadFrom(assemblyFile, securityInfo); #pragma warning restore 618 Type t = assembly.GetType(typeName, true, ignoreCase); Object o = Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes); Log(o != null, "CreateInstanceFrom:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName); if(o == null) return null; else { ObjectHandle Handle = new ObjectHandle(o); return Handle; } } // // This API is designed to be used when a host needs to execute code in an AppDomain // with restricted security permissions. In that case, we demand in the client domain // and assert in the server domain because the server domain might not be trusted enough // to pass the security checks when activating the type. // [System.Security.SecurityCritical] // auto-generated_required public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName) { if (domain == null) throw new ArgumentNullException("domain"); Contract.EndContractBlock(); return domain.InternalCreateInstanceWithNoSecurity(assemblyName, typeName); } [System.Security.SecurityCritical] // auto-generated_required [Obsolete("Methods which use evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstance which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")] public static ObjectHandle CreateInstance (AppDomain domain, string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes) { if (domain == null) throw new ArgumentNullException("domain"); Contract.EndContractBlock(); #if FEATURE_CAS_POLICY if (securityAttributes != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit")); } #endif // FEATURE_CAS_POLICY return domain.InternalCreateInstanceWithNoSecurity(assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes); } [SecurityCritical] public static ObjectHandle CreateInstance(AppDomain domain, string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes) { if (domain == null) throw new ArgumentNullException("domain"); Contract.EndContractBlock(); return domain.InternalCreateInstanceWithNoSecurity(assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, null); } // // This API is designed to be used when a host needs to execute code in an AppDomain // with restricted security permissions. In that case, we demand in the client domain // and assert in the server domain because the server domain might not be trusted enough // to pass the security checks when activating the type. // [System.Security.SecurityCritical] // auto-generated_required [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName) { if (domain == null) throw new ArgumentNullException("domain"); Contract.EndContractBlock(); return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile, typeName); } [System.Security.SecurityCritical] // auto-generated_required [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] [Obsolete("Methods which use Evidence to sandbox are obsolete and will be removed in a future release of the .NET Framework. Please use an overload of CreateInstanceFrom which does not take an Evidence parameter. See http://go.microsoft.com/fwlink/?LinkID=155570 for more information.")] public static ObjectHandle CreateInstanceFrom (AppDomain domain, string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes) { if (domain == null) throw new ArgumentNullException("domain"); Contract.EndContractBlock(); #if FEATURE_CAS_POLICY if (securityAttributes != null && !AppDomain.CurrentDomain.IsLegacyCasPolicyEnabled) { throw new NotSupportedException(Environment.GetResourceString("NotSupported_RequiresCasPolicyImplicit")); } #endif // FEATURE_CAS_POLICY return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes); } [SecurityCritical] [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public static ObjectHandle CreateInstanceFrom(AppDomain domain, string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes) { if (domain == null) throw new ArgumentNullException("domain"); Contract.EndContractBlock(); return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, null); } #if FEATURE_COMINTEROP #if FEATURE_CLICKONCE [System.Security.SecuritySafeCritical] // auto-generated public static ObjectHandle CreateInstance (ActivationContext activationContext) { AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager; if (domainManager == null) domainManager = new AppDomainManager(); return domainManager.ApplicationActivator.CreateInstance(activationContext); } [System.Security.SecuritySafeCritical] // auto-generated public static ObjectHandle CreateInstance (ActivationContext activationContext, string[] activationCustomData) { AppDomainManager domainManager = AppDomain.CurrentDomain.DomainManager; if (domainManager == null) domainManager = new AppDomainManager(); return domainManager.ApplicationActivator.CreateInstance(activationContext, activationCustomData); } #endif // FEATURE_CLICKONCE [System.Security.SecuritySafeCritical] // auto-generated [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public static ObjectHandle CreateComInstanceFrom(String assemblyName, String typeName) { return CreateComInstanceFrom(assemblyName, typeName, null, AssemblyHashAlgorithm.None); } [System.Security.SecuritySafeCritical] // auto-generated [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public static ObjectHandle CreateComInstanceFrom(String assemblyName, String typeName, byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm) { Assembly assembly = Assembly.LoadFrom(assemblyName, hashValue, hashAlgorithm); Type t = assembly.GetType(typeName, true, false); Object[] Attr = t.GetCustomAttributes(typeof(ComVisibleAttribute),false); if (Attr.Length > 0) { if (((ComVisibleAttribute)Attr[0]).Value == false) throw new TypeLoadException(Environment.GetResourceString( "Argument_TypeMustBeVisibleFromCom" )); } Log(assembly != null, "CreateInstance:: ", "Loaded " + assembly.FullName, "Failed to Load: " + assemblyName); if(assembly == null) return null; Object o = Activator.CreateInstance(t, Activator.ConstructorDefault, null, null, null, null); Log(o != null, "CreateInstance:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName); if(o == null) return null; else { ObjectHandle Handle = new ObjectHandle(o); return Handle; } } #endif // FEATURE_COMINTEROP #if FEATURE_REMOTING // This method is a helper method and delegates to the remoting // services to do the actual work. [System.Security.SecurityCritical] // auto-generated_required static public Object GetObject(Type type, String url) { return GetObject(type, url, null); } // This method is a helper method and delegates to the remoting // services to do the actual work. [System.Security.SecurityCritical] // auto-generated_required static public Object GetObject(Type type, String url, Object state) { if (type == null) throw new ArgumentNullException("type"); Contract.EndContractBlock(); return RemotingServices.Connect(type, url, state); } #endif [System.Diagnostics.Conditional("_DEBUG")] private static void Log(bool test, string title, string success, string failure) { #if FEATURE_REMOTING if(test) BCLDebug.Trace("REMOTE", "{0}{1}", title, success); else BCLDebug.Trace("REMOTE", "{0}{1}", title, failure); #endif } void _Activator.GetTypeInfoCount(out uint pcTInfo) { throw new NotImplementedException(); } void _Activator.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo) { throw new NotImplementedException(); } void _Activator.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId) { throw new NotImplementedException(); } void _Activator.Invoke(uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr) { throw new NotImplementedException(); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AutoResetEvent.cs
- BasicHttpMessageSecurityElement.cs
- RSAProtectedConfigurationProvider.cs
- ToolStripPanel.cs
- InstanceData.cs
- DocumentGridPage.cs
- TrimSurroundingWhitespaceAttribute.cs
- SqlConnectionPoolGroupProviderInfo.cs
- UserCancellationException.cs
- GridProviderWrapper.cs
- BasicBrowserDialog.cs
- XsdSchemaFileEditor.cs
- BindingSource.cs
- WizardStepBase.cs
- ScaleTransform3D.cs
- CompilerTypeWithParams.cs
- Variable.cs
- EdgeProfileValidation.cs
- DesignerDataStoredProcedure.cs
- ImmutableObjectAttribute.cs
- InheritanceContextChangedEventManager.cs
- FormsAuthenticationUser.cs
- AdornerHitTestResult.cs
- Int16AnimationUsingKeyFrames.cs
- StringStorage.cs
- FrugalList.cs
- ScaleTransform.cs
- ServiceModelSecurityTokenRequirement.cs
- SolidBrush.cs
- SQLMoney.cs
- PagesSection.cs
- ResourceManagerWrapper.cs
- XmlLinkedNode.cs
- documentsequencetextview.cs
- isolationinterop.cs
- CommonXSendMessage.cs
- AnnotationService.cs
- DataListItemCollection.cs
- DataFormats.cs
- XMLDiffLoader.cs
- DataGridViewComboBoxEditingControl.cs
- ContentPlaceHolder.cs
- RSACryptoServiceProvider.cs
- DiscoveryMessageSequenceCD1.cs
- Typography.cs
- AttributeInfo.cs
- BuilderPropertyEntry.cs
- MappingItemCollection.cs
- TextRunTypographyProperties.cs
- SystemParameters.cs
- recordstatefactory.cs
- APCustomTypeDescriptor.cs
- SqlClientFactory.cs
- ConnectionPoolRegistry.cs
- DynamicPropertyReader.cs
- Membership.cs
- SparseMemoryStream.cs
- ContainerVisual.cs
- AspNetSynchronizationContext.cs
- SafeNativeMethods.cs
- Repeater.cs
- ToolStripArrowRenderEventArgs.cs
- NumberSubstitution.cs
- MaskedTextProvider.cs
- DataGridPageChangedEventArgs.cs
- WeakEventTable.cs
- __Error.cs
- HttpCookieCollection.cs
- ButtonFieldBase.cs
- XPathExpr.cs
- SqlDependencyListener.cs
- WebServiceParameterData.cs
- MouseEvent.cs
- NativeMethods.cs
- CombinedGeometry.cs
- InstanceData.cs
- PixelFormatConverter.cs
- exports.cs
- TextCompositionEventArgs.cs
- LocatorBase.cs
- ControlBuilderAttribute.cs
- RuntimeHandles.cs
- SoapMessage.cs
- NonParentingControl.cs
- MeshGeometry3D.cs
- TextOutput.cs
- DefaultValidator.cs
- _HTTPDateParse.cs
- XmlEventCache.cs
- ContractMapping.cs
- VerticalConnector.xaml.cs
- ConnectionAcceptor.cs
- FontStyleConverter.cs
- DictionaryContent.cs
- JavaScriptString.cs
- ControlAdapter.cs
- SafeBitVector32.cs
- IArgumentProvider.cs
- dsa.cs
- WsatRegistrationHeader.cs