The Labs \ Source Viewer \ SSCLI \ System.Configuration \ FactoryRecord

  1. //------------------------------------------------------------------------------
  2. // <copyright file="FactoryRecord.cs" company="Microsoft">
  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. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Configuration
  16. {
  17.     using System.Configuration.Internal;
  18.     using System.Collections;
  19.     using System.Collections.Specialized;
  20.     using System.Collections.Generic;
  21.     using System.Configuration;
  22.     using System.Text;
  23.     using System.Threading;
  24.     using System.Reflection;
  25.     using System.Xml;
  26.    
  27.     [System.Diagnostics.DebuggerDisplay("FactoryRecord {ConfigKey}")]
  28.     internal class FactoryRecord : IConfigErrorInfo
  29.     {
  30.         private const int Flag_AllowLocation = 1;
  31.         // Does the factory allow location directives?
  32.         private const int Flag_RestartOnExternalChanges = 2;
  33.         // Restart on external changes?
  34.         private const int Flag_RequirePermission = 4;
  35.         // Does access to the section require unrestricted ConfigurationPermission?
  36.         private const int Flag_IsGroup = 8;
  37.         // factory represents a group
  38.         private const int Flag_IsFromTrustedConfigRecord = 16;
  39.         // Factory is defined in trusted config record
  40.         private const int Flag_IsFactoryTrustedWithoutAptca = 32;
  41.         // Factory is from trusted assembly without aptca
  42.         private const int Flag_IsUndeclared = 64;
  43.         // Factory is not declared - either implicit or unrecognized
  44.         private string _configKey;
  45.         // full config key = group name + section name
  46.         private string _group;
  47.         // group name
  48.         private string _name;
  49.         // section name
  50.         private SimpleBitVector32 _flags;
  51.         // factory flags
  52.         private string _factoryTypeName;
  53.         // the factory's type name
  54.         private ConfigurationAllowDefinition _allowDefinition;
  55.         // the allowed definition
  56.         private ConfigurationAllowExeDefinition _allowExeDefinition;
  57.         // the allowed Exe definition
  58.         private string _filename;
  59.         // filename of the factory type name
  60.         private int _lineNumber;
  61.         // line number of the factory type name
  62.         private object _factory;
  63.         // the created factory
  64.         private List<ConfigurationException> _errors;
  65.         // errors
  66.         // constructor used for Clone()
  67.         FactoryRecord(string configKey, string group, string name, object factory, string factoryTypeName, SimpleBitVector32 flags, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, string filename, int lineNumber,
  68.         ICollection<ConfigurationException> errors)
  69.         {
  70.            
  71.             _configKey = configKey;
  72.             _group = group;
  73.             _name = name;
  74.             _factory = factory;
  75.             _factoryTypeName = factoryTypeName;
  76.             _flags = flags;
  77.             _allowDefinition = allowDefinition;
  78.             _allowExeDefinition = allowExeDefinition;
  79.             _filename = filename;
  80.             _lineNumber = lineNumber;
  81.             AddErrors(errors);
  82.         }
  83.        
  84.         // constructor used for group
  85.         internal FactoryRecord(string configKey, string group, string name, string factoryTypeName, string filename, int lineNumber)
  86.         {
  87.             _configKey = configKey;
  88.             _group = group;
  89.             _name = name;
  90.             _factoryTypeName = factoryTypeName;
  91.             IsGroup = true;
  92.             _filename = filename;
  93.             _lineNumber = lineNumber;
  94.         }
  95.        
  96.         // constructor used for a section
  97.         internal FactoryRecord(string configKey, string group, string name, string factoryTypeName, bool allowLocation, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, bool restartOnExternalChanges, bool requirePermission, bool isFromTrustedConfigRecord,
  98.         bool isUndeclared, string filename, int lineNumber)
  99.         {
  100.            
  101.             _configKey = configKey;
  102.             _group = group;
  103.             _name = name;
  104.             _factoryTypeName = factoryTypeName;
  105.             _allowDefinition = allowDefinition;
  106.             _allowExeDefinition = allowExeDefinition;
  107.             AllowLocation = allowLocation;
  108.             RestartOnExternalChanges = restartOnExternalChanges;
  109.             RequirePermission = requirePermission;
  110.             IsFromTrustedConfigRecord = isFromTrustedConfigRecord;
  111.             IsUndeclared = isUndeclared;
  112.             _filename = filename;
  113.             _lineNumber = lineNumber;
  114.         }
  115.        
  116.         // by cloning we contain a single copy of the strings referred to in the factory and section records
  117.         internal FactoryRecord CloneSection(string filename, int lineNumber)
  118.         {
  119.             return new FactoryRecord(_configKey, _group, _name, _factory, _factoryTypeName, _flags, _allowDefinition, _allowExeDefinition, filename, lineNumber,
  120.             Errors);
  121.         }
  122.        
  123.         // by cloning we contain a single copy of the strings referred to in the factory and section records
  124.         internal FactoryRecord CloneSectionGroup(string factoryTypeName, string filename, int lineNumber)
  125.         {
  126.             if (_factoryTypeName != null) {
  127.                 factoryTypeName = _factoryTypeName;
  128.             }
  129.            
  130.             return new FactoryRecord(_configKey, _group, _name, _factory, factoryTypeName, _flags, _allowDefinition, _allowExeDefinition, filename, lineNumber,
  131.             Errors);
  132.         }
  133.        
  134.         internal string ConfigKey {
  135.             get { return _configKey; }
  136.         }
  137.        
  138.         internal string Group {
  139.             get { return _group; }
  140.         }
  141.        
  142.         internal string Name {
  143.             get { return _name; }
  144.         }
  145.        
  146.         internal object Factory {
  147.             get { return _factory; }
  148.             set { _factory = value; }
  149.         }
  150.        
  151.         internal string FactoryTypeName {
  152.             get { return _factoryTypeName; }
  153.             set { _factoryTypeName = value; }
  154.         }
  155.        
  156.         internal ConfigurationAllowDefinition AllowDefinition {
  157.             get { return _allowDefinition; }
  158.             set { _allowDefinition = value; }
  159.         }
  160.        
  161.         internal ConfigurationAllowExeDefinition AllowExeDefinition {
  162.             get { return _allowExeDefinition; }
  163.             set { _allowExeDefinition = value; }
  164.         }
  165.        
  166.         internal bool AllowLocation {
  167.             get { return _flags[Flag_AllowLocation]; }
  168.             set { _flags[Flag_AllowLocation] = value; }
  169.         }
  170.        
  171.         internal bool RestartOnExternalChanges {
  172.             get { return _flags[Flag_RestartOnExternalChanges]; }
  173.             set { _flags[Flag_RestartOnExternalChanges] = value; }
  174.         }
  175.        
  176.         internal bool RequirePermission {
  177.             get { return _flags[Flag_RequirePermission]; }
  178.             set { _flags[Flag_RequirePermission] = value; }
  179.         }
  180.        
  181.         internal bool IsGroup {
  182.             get { return _flags[Flag_IsGroup]; }
  183.             set { _flags[Flag_IsGroup] = value; }
  184.         }
  185.        
  186.         internal bool IsFromTrustedConfigRecord {
  187.             get { return _flags[Flag_IsFromTrustedConfigRecord]; }
  188.             set { _flags[Flag_IsFromTrustedConfigRecord] = value; }
  189.         }
  190.        
  191.         internal bool IsUndeclared {
  192.             get { return _flags[Flag_IsUndeclared]; }
  193.             set { _flags[Flag_IsUndeclared] = value; }
  194.         }
  195.        
  196.         internal bool IsFactoryTrustedWithoutAptca {
  197.             get {
  198.                 Debug.Assert(_factory != null, "_factory != null");
  199.                 return _flags[Flag_IsFactoryTrustedWithoutAptca];
  200.             }
  201.            
  202.             set { _flags[Flag_IsFactoryTrustedWithoutAptca] = value; }
  203.         }
  204.        
  205.         // This is used in HttpConfigurationRecord.EnsureSectionFactory() to give file and line source
  206.         // when a section handler type is invalid or cannot be loaded.
  207.         public string Filename {
  208.             get { return _filename; }
  209.             set { _filename = value; }
  210.         }
  211.        
  212.         public int LineNumber {
  213.             get { return _lineNumber; }
  214.             set { _lineNumber = value; }
  215.         }
  216.        
  217.         internal bool HasFile {
  218.             get { return _lineNumber >= 0; }
  219.         }
  220.        
  221.         internal bool IsEquivalentType(IInternalConfigHost host, string typeName)
  222.         {
  223.             try {
  224.                 if (_factoryTypeName == typeName)
  225.                     return true;
  226.                
  227.                 Type t1;
  228.                 Type t2;
  229.                
  230.                 if (host != null) {
  231.                     t1 = TypeUtil.GetTypeWithReflectionPermission(host, typeName, false);
  232.                     t2 = TypeUtil.GetTypeWithReflectionPermission(host, _factoryTypeName, false);
  233.                 }
  234.                 else {
  235.                     t1 = TypeUtil.GetTypeWithReflectionPermission(typeName, false);
  236.                     t2 = TypeUtil.GetTypeWithReflectionPermission(_factoryTypeName, false);
  237.                 }
  238.                
  239.                 return (t1 != null) && (t1 == t2);
  240.             }
  241.             catch {
  242.             }
  243.            
  244.             return false;
  245.         }
  246.        
  247.         internal bool IsEquivalentSectionGroupFactory(IInternalConfigHost host, string typeName)
  248.         {
  249.             if (typeName == null || _factoryTypeName == null)
  250.                 return true;
  251.            
  252.             return IsEquivalentType(host, typeName);
  253.         }
  254.        
  255.         internal bool IsEquivalentSectionFactory(IInternalConfigHost host, string typeName, bool allowLocation, ConfigurationAllowDefinition allowDefinition, ConfigurationAllowExeDefinition allowExeDefinition, bool restartOnExternalChanges, bool requirePermission)
  256.         {
  257.            
  258.             if (allowLocation != this.AllowLocation || allowDefinition != this.AllowDefinition || allowExeDefinition != this.AllowExeDefinition || restartOnExternalChanges != this.RestartOnExternalChanges || requirePermission != this.RequirePermission) {
  259.                
  260.                 return false;
  261.             }
  262.            
  263.             return IsEquivalentType(host, typeName);
  264.         }
  265.        
  266.         //
  267.         // Errors associated with the parse of a factory.
  268.         //
  269.         internal List<ConfigurationException> Errors {
  270.             get { return _errors; }
  271.         }
  272.        
  273.         internal bool HasErrors {
  274.             get { return ErrorsHelper.GetHasErrors(_errors); }
  275.         }
  276.        
  277.         internal void AddErrors(ICollection<ConfigurationException> coll)
  278.         {
  279.             ErrorsHelper.AddErrors(ref _errors, coll);
  280.         }
  281.        
  282.         internal void ThrowOnErrors()
  283.         {
  284.             ErrorsHelper.ThrowOnErrors(_errors);
  285.         }
  286.     }
  287. }

Developer Fusion