The Labs \ Source Viewer \ SSCLI \ System \ Activator

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. ////////////////////////////////////////////////////////////////////////////////
  16. ////////////////////////////////////////////////////////////////////////////////
  17. //
  18. // Activator is an object that contains the Activation (CreateInstance/New)
  19. // methods for late bound support.
  20. //
  21. //
  22. //
  23. //
  24. namespace System
  25. {
  26.    
  27.     using System;
  28.     using System.Reflection;
  29.     using System.Runtime.Remoting;
  30.     using System.Runtime.Remoting.Activation;
  31.     using Message = System.Runtime.Remoting.Messaging.Message;
  32.     using CultureInfo = System.Globalization.CultureInfo;
  33.     using Evidence = System.Security.Policy.Evidence;
  34.     using StackCrawlMark = System.Threading.StackCrawlMark;
  35.     using System.Runtime.InteropServices;
  36.     using System.Security.Permissions;
  37.     using AssemblyHashAlgorithm = System.Configuration.Assemblies.AssemblyHashAlgorithm;
  38.    
  39.     // Only statics, does not need to be marked with the serializable attribute
  40.     [ClassInterface(ClassInterfaceType.None)]
  41.     [ComDefaultInterface(typeof(_Activator))]
  42.     [System.Runtime.InteropServices.ComVisible(true)]
  43.     public sealed class Activator : _Activator
  44.     {
  45.         internal const int LookupMask = 255;
  46.         internal const BindingFlags ConLookup = (BindingFlags)(BindingFlags.Instance | BindingFlags.Public);
  47.         internal const BindingFlags ConstructorDefault = BindingFlags.Instance | BindingFlags.Public | BindingFlags.CreateInstance;
  48.        
  49.         // This class only contains statics, so hide the worthless constructor
  50.         private Activator()
  51.         {
  52.         }
  53.        
  54.         // CreateInstance
  55.         // The following methods will create a new instance of an Object
  56.         // Full Binding Support
  57.         // For all of these methods we need to get the underlying RuntimeType and
  58.         // call the Impl version.
  59.         public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture)
  60.         {
  61.             return CreateInstance(type, bindingAttr, binder, args, culture, null);
  62.         }
  63.        
  64.         public static object CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes)
  65.         {
  66.             if (type == null)
  67.                 throw new ArgumentNullException("type");
  68.            
  69.             if (type is System.Reflection.Emit.TypeBuilder)
  70.                 throw new NotSupportedException(Environment.GetResourceString("NotSupported_CreateInstanceWithTypeBuilder"));
  71.            
  72.             // If they didn't specify a lookup, then we will provide the default lookup.
  73.             if ((bindingAttr & (BindingFlags)LookupMask) == 0)
  74.                 bindingAttr |= Activator.ConstructorDefault;
  75.            
  76.             if (activationAttributes != null && activationAttributes.Length > 0) {
  77.                 // If type does not derive from MBR
  78.                 // throw notsupportedexception
  79.                 if (type.IsMarshalByRef) {
  80.                     // The fix below is preventative.
  81.                     //
  82.                     if (!(type.IsContextful)) {
  83.                         if (activationAttributes.Length > 1 || !(activationAttributes[0] is UrlAttribute))
  84.                             throw new NotSupportedException(Environment.GetResourceString("NotSupported_NonUrlAttrOnMBR"));
  85.                     }
  86.                 }
  87.                 else
  88.                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_ActivAttrOnNonMBR"));
  89.             }
  90.            
  91.             RuntimeType rt = type.UnderlyingSystemType as RuntimeType;
  92.            
  93.             if (rt == null)
  94.                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
  95.            
  96.             return rt.CreateInstanceImpl(bindingAttr, binder, args, culture, activationAttributes);
  97.         }
  98.        
  99.         public static object CreateInstance(Type type, params object[] args)
  100.         {
  101.             return CreateInstance(type, Activator.ConstructorDefault, null, args, null, null);
  102.         }
  103.        
  104.         public static object CreateInstance(Type type, object[] args, object[] activationAttributes)
  105.         {
  106.             return CreateInstance(type, Activator.ConstructorDefault, null, args, null, activationAttributes);
  107.         }
  108.        
  109.         public static object CreateInstance(Type type)
  110.         {
  111.             return Activator.CreateInstance(type, false);
  112.         }
  113.        
  114. /*
  115.         * Create an instance using the name of type and the assembly where it exists. This allows
  116.         * types to be created remotely without having to load the type locally.
  117.         */       
  118.        
  119.         public static ObjectHandle CreateInstance(string assemblyName, string typeName)
  120.         {
  121.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  122.             return CreateInstance(assemblyName, typeName, false, Activator.ConstructorDefault, null, null, null, null, null, ref stackMark
  123.             );
  124.         }
  125.        
  126.        
  127.         public static ObjectHandle CreateInstance(string assemblyName, string typeName, object[] activationAttributes)
  128.         {
  129.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  130.             return CreateInstance(assemblyName, typeName, false, Activator.ConstructorDefault, null, null, null, activationAttributes, null, ref stackMark
  131.             );
  132.         }
  133.        
  134.         public static object CreateInstance(Type type, bool nonPublic)
  135.         {
  136.             if (type == null)
  137.                 throw new ArgumentNullException("type");
  138.            
  139.             RuntimeType rt = type.UnderlyingSystemType as RuntimeType;
  140.            
  141.             if (rt == null)
  142.                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
  143.            
  144.             return rt.CreateInstanceImpl(!nonPublic);
  145.         }
  146.        
  147.         static internal object InternalCreateInstanceWithNoMemberAccessCheck(Type type, bool nonPublic)
  148.         {
  149.             if (type == null)
  150.                 throw new ArgumentNullException("type");
  151.            
  152.             RuntimeType rt = type.UnderlyingSystemType as RuntimeType;
  153.            
  154.             if (rt == null)
  155.                 throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "type");
  156.            
  157.             return rt.CreateInstanceImpl(!nonPublic, false, false);
  158.         }
  159.        
  160.         public static T CreateInstance<T>()
  161.         {
  162.             bool bNeedSecurityCheck = true;
  163.             bool bCanBeCached = false;
  164.             RuntimeMethodHandle mh = RuntimeMethodHandle.EmptyHandle;
  165.             return (T)RuntimeTypeHandle.CreateInstance(typeof(T) as RuntimeType, true, true, ref bCanBeCached, ref mh, ref bNeedSecurityCheck);
  166.         }
  167.        
  168.        
  169.         public static ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName)
  170.         {
  171.             return CreateInstanceFrom(assemblyFile, typeName, null);
  172.         }
  173.        
  174.        
  175.         public static ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName, object[] activationAttributes)
  176.         {
  177.             return CreateInstanceFrom(assemblyFile, typeName, false, Activator.ConstructorDefault, null, null, null, activationAttributes, null);
  178.         }
  179.        
  180.        
  181.         public static ObjectHandle CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityInfo)
  182.         {
  183.             StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
  184.             return CreateInstance(assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityInfo, ref stackMark
  185.             );
  186.         }
  187.        
  188.         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
  189.         )
  190.         {
  191.             Assembly assembly;
  192.             if (assemblyName == null)
  193.                 assembly = Assembly.nGetExecutingAssembly(ref stackMark);
  194.             else
  195.                 assembly = Assembly.InternalLoad(assemblyName, securityInfo, ref stackMark, false);
  196.            
  197.             Log(assembly != null, "CreateInstance:: ", "Loaded " + assembly.FullName, "Failed to Load: " + assemblyName);
  198.             if (assembly == null)
  199.                 return null;
  200.            
  201.             Type t = assembly.GetType(typeName, true, ignoreCase);
  202.            
  203.             object o = Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes);
  204.            
  205.             Log(o != null, "CreateInstance:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName);
  206.             if (o == null)
  207.                 return null;
  208.             else {
  209.                 ObjectHandle Handle = new ObjectHandle(o);
  210.                 return Handle;
  211.             }
  212.         }
  213.        
  214.        
  215.         public static ObjectHandle CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityInfo)
  216.         {
  217.             Assembly assembly = Assembly.LoadFrom(assemblyFile, securityInfo);
  218.             Type t = assembly.GetType(typeName, true, ignoreCase);
  219.            
  220.             object o = Activator.CreateInstance(t, bindingAttr, binder, args, culture, activationAttributes);
  221.            
  222.             Log(o != null, "CreateInstanceFrom:: ", "Created Instance of class " + typeName, "Failed to create instance of class " + typeName);
  223.             if (o == null)
  224.                 return null;
  225.             else {
  226.                 ObjectHandle Handle = new ObjectHandle(o);
  227.                 return Handle;
  228.             }
  229.         }
  230.        
  231.         //
  232.         // This API is designed to be used when a host needs to execute code in an AppDomain
  233.         // with restricted security permissions. In that case, we demand in the client domain
  234.         // and assert in the server domain because the server domain might not be trusted enough
  235.         // to pass the security checks when activating the type.
  236.         //
  237.        
  238.         [PermissionSetAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
  239.         public static ObjectHandle CreateInstance(AppDomain domain, string assemblyName, string typeName)
  240.         {
  241.             if (domain == null)
  242.                 throw new ArgumentNullException("domain");
  243.             return domain.InternalCreateInstanceWithNoSecurity(assemblyName, typeName);
  244.         }
  245.        
  246.         [PermissionSetAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
  247.         public static ObjectHandle CreateInstance(AppDomain domain, string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityAttributes
  248.         )
  249.         {
  250.             if (domain == null)
  251.                 throw new ArgumentNullException("domain");
  252.             return domain.InternalCreateInstanceWithNoSecurity(assemblyName, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
  253.         }
  254.        
  255.         //
  256.         // This API is designed to be used when a host needs to execute code in an AppDomain
  257.         // with restricted security permissions. In that case, we demand in the client domain
  258.         // and assert in the server domain because the server domain might not be trusted enough
  259.         // to pass the security checks when activating the type.
  260.         //
  261.        
  262.         [PermissionSetAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
  263.         public static ObjectHandle CreateInstanceFrom(AppDomain domain, string assemblyFile, string typeName)
  264.         {
  265.             if (domain == null)
  266.                 throw new ArgumentNullException("domain");
  267.             return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile, typeName);
  268.         }
  269.        
  270.         [PermissionSetAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
  271.         public static ObjectHandle CreateInstanceFrom(AppDomain domain, string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder binder, object[] args, CultureInfo culture, object[] activationAttributes, Evidence securityAttributes
  272.         )
  273.         {
  274.             if (domain == null)
  275.                 throw new ArgumentNullException("domain");
  276.             return domain.InternalCreateInstanceFromWithNoSecurity(assemblyFile, typeName, ignoreCase, bindingAttr, binder, args, culture, activationAttributes, securityAttributes);
  277.         }
  278.        
  279.        
  280.         // This method is a helper method and delegates to the remoting
  281.         // services to do the actual work.
  282.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  283.         public static object GetObject(Type type, string url)
  284.         {
  285.             return GetObject(type, url, null);
  286.         }
  287.        
  288.         // This method is a helper method and delegates to the remoting
  289.         // services to do the actual work.
  290.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.RemotingConfiguration)]
  291.         public static object GetObject(Type type, string url, object state)
  292.         {
  293.             if (type == null)
  294.                 throw new ArgumentNullException("type");
  295.             return RemotingServices.Connect(type, url, state);
  296.         }
  297.        
  298.         [System.Diagnostics.Conditional("_DEBUG")]
  299.         private static void Log(bool test, string title, string success, string failure)
  300.         {
  301.             if (test)
  302.                 Message.DebugOut(title + success + "\n");
  303.             else
  304.                 Message.DebugOut(title + failure + "\n");
  305.         }
  306.        
  307.         void _Activator.GetTypeInfoCount(out uint pcTInfo)
  308.         {
  309.             throw new NotImplementedException();
  310.         }
  311.        
  312.         void _Activator.GetTypeInfo(uint iTInfo, uint lcid, IntPtr ppTInfo)
  313.         {
  314.             throw new NotImplementedException();
  315.         }
  316.        
  317.         void _Activator.GetIDsOfNames(        [In()]
  318. ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  319.         {
  320.             throw new NotImplementedException();
  321.         }
  322.        
  323.         void _Activator.Invoke(uint dispIdMember,         [In()]
  324. ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  325.         {
  326.             throw new NotImplementedException();
  327.         }
  328.     }
  329. }

Developer Fusion