Code:
/ FXUpdate3074 / FXUpdate3074 / 1.1 / DEVDIV / depot / DevDiv / releases / whidbey / QFE / ndp / fx / src / xsp / System / Web / Compilation / BaseCodeDomTreeGenerator.cs / 5 / BaseCodeDomTreeGenerator.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /********************************************** Class hierarchy: BaseCodeDomTreeGenerator BaseTemplateCodeDomTreeGenerator TemplateControlCodeDomTreeGenerator PageCodeDomTreeGenerator UserControlCodeDomTreeGenerator PageThemeCodeDomTreeGenerator ApplicationFileCodeDomTreeGenerator ***********************************************/ namespace System.Web.Compilation { using System.Text; using System.Runtime.Serialization.Formatters; using System.ComponentModel; using System; using System.Collections; using System.Collections.Specialized; using System.Reflection; using System.IO; using Microsoft.Win32; using System.Security.Cryptography; using System.Web.Caching; using System.Web.Util; using System.Web.UI; using System.Web.SessionState; using System.CodeDom; using System.CodeDom.Compiler; using Util = System.Web.UI.Util; using System.Web.Hosting; using System.Web.Profile; using System.Web.Configuration; using System.Globalization; internal abstract class BaseCodeDomTreeGenerator { protected CodeDomProvider _codeDomProvider; protected CodeCompileUnit _codeCompileUnit; private CodeNamespace _sourceDataNamespace; protected CodeTypeDeclaration _sourceDataClass; protected CodeTypeDeclaration _intermediateClass; private CompilerParameters _compilParams; protected StringResourceBuilder _stringResourceBuilder; protected bool _usingVJSCompiler; private static IDictionary _generatedColumnOffsetDictionary; private VirtualPath _virtualPath; // The constructors protected CodeConstructor _ctor; protected CodeTypeReferenceExpression _classTypeExpr; internal const string defaultNamespace = "ASP"; // Used for things that we don't want the user to see internal const string internalAspNamespace = "__ASP"; private const string initializedFieldName = "__initialized"; private const string _dummyVariable = "__dummyVar"; private const int _defaultColumnOffset = 4; private TemplateParser _parser; TemplateParser Parser { get { return _parser; } } // We generate different code for the designer protected bool _designerMode; internal void SetDesignerMode() { _designerMode = true; } private IDictionary _linePragmasTable; internal IDictionary LinePragmasTable { get { return _linePragmasTable; } } // Used to generate indexed into the LinePragmasTable private int _pragmaIdGenerator=1; private static bool _urlLinePragmas; static BaseCodeDomTreeGenerator() { CompilationSection config = RuntimeConfig.GetAppConfig().Compilation; _urlLinePragmas = config.UrlLinePragmas; } #if DBG private bool _addedDebugComment; #endif #if DBG protected void AppendDebugComment(CodeStatementCollection statements) { if (!_addedDebugComment) { _addedDebugComment = true; StringBuilder debugComment = new StringBuilder(); debugComment.Append("\r\n"); debugComment.Append("** DEBUG INFORMATION **"); debugComment.Append("\r\n"); statements.Add(new CodeCommentStatement(debugComment.ToString())); } } #endif internal /*public*/ CodeCompileUnit GetCodeDomTree(CodeDomProvider codeDomProvider, StringResourceBuilder stringResourceBuilder, VirtualPath virtualPath) { Debug.Assert(_codeDomProvider == null && _stringResourceBuilder == null); _codeDomProvider = codeDomProvider; _stringResourceBuilder = stringResourceBuilder; _virtualPath = virtualPath; // Build the data tree that needs to be compiled if (!BuildSourceDataTree()) return null; return _codeCompileUnit; } protected /*public*/ CompilerParameters CompilParams { get { return _compilParams; } } internal string GetInstantiatableFullTypeName() { // In updatable mode, we never build the final type, so return null if (PrecompilingForUpdatableDeployment) return null; return Util.MakeFullTypeName(_sourceDataNamespace.Name, _sourceDataClass.Name); } internal string GetIntermediateFullTypeName() { return Util.MakeFullTypeName(Parser.BaseTypeNamespace, _intermediateClass.Name); } /* * Set some fields that are needed for code generation */ protected BaseCodeDomTreeGenerator(TemplateParser parser) { _parser = parser; Debug.Assert(Parser.BaseType != null); } protected void ApplyEditorBrowsableCustomAttribute(CodeTypeMember member) { Debug.Assert(_designerMode, "This method should only be used in design mode."); // Generate EditorBrowsableAttribute to hide the generated methods from the tool // [EditorBrowsable(EditorBrowsableState.Never)] CodeAttributeDeclaration editorBrowsableAttribute = new CodeAttributeDeclaration(); editorBrowsableAttribute.Name = typeof(EditorBrowsableAttribute).FullName; editorBrowsableAttribute.Arguments.Add(new CodeAttributeArgument(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EditorBrowsableState)), "Never"))); member.CustomAttributes.Add(editorBrowsableAttribute); } ////// Create a name for the generated class /// protected virtual string GetGeneratedClassName() { string className; // If the user specified the class name, just use that if (Parser.GeneratedClassName != null) return Parser.GeneratedClassName; // Use the input file name to generate the class name className = _virtualPath.FileName; // Prepend the class name with the directory path within the app (DevDiv 42063) string appRelVirtualDir = _virtualPath.Parent.AppRelativeVirtualPathStringOrNull; if (appRelVirtualDir != null) { Debug.Assert(UrlPath.IsAppRelativePath(appRelVirtualDir)); className = appRelVirtualDir.Substring(2) + className; } // Change invalid chars to underscores className = Util.MakeValidTypeNameFromString(className); // Make it lower case to make it more predictable ( className = className.ToLowerInvariant(); // If it's the same as the base type name, prepend it with an underscore to prevent // a compile error. string baseTypeName = Parser.BaseTypeName != null ? Parser.BaseTypeName : Parser.BaseType.Name; if (StringUtil.EqualsIgnoreCase(className, baseTypeName)) { className = "_" + className; } return className; } internal static bool IsAspNetNamespace(string ns) { return (ns == defaultNamespace); } private bool PrecompilingForUpdatableDeployment { get { // For global.asax, this never applies if (IsGlobalAsaxGenerator) return false; return BuildManager.PrecompilingForUpdatableDeployment; } } private bool BuildSourceDataTree() { _compilParams = Parser.CompilParams; _codeCompileUnit = new CodeCompileUnit(); _codeCompileUnit.UserData["AllowLateBound"] = !Parser.FStrict; _codeCompileUnit.UserData["RequireVariableDeclaration"] = Parser.FExplicit; // Set a flag indicating if we're using the VJS compiler. See comment in BuildExtractMethod for more information. _usingVJSCompiler = (_codeDomProvider.FileExtension == ".jsl"); _sourceDataNamespace = new CodeNamespace(Parser.GeneratedNamespace); string generatedClassName = GetGeneratedClassName(); if (Parser.BaseTypeName != null) { Debug.Assert(Parser.CodeFileVirtualPath != null); // This is the case where the page has a CodeFile attribute CodeNamespace intermediateNamespace = new CodeNamespace(Parser.BaseTypeNamespace); _codeCompileUnit.Namespaces.Add(intermediateNamespace); _intermediateClass = new CodeTypeDeclaration(Parser.BaseTypeName); // Specify the base class in the UserData in case the CodeDom provider needs // to reflect on it when generating code from the CodeCompileUnit ( if (_designerMode) _intermediateClass.UserData["BaseClassDefinition"] = Parser.DefaultBaseType; else _intermediateClass.UserData["BaseClassDefinition"] = Parser.BaseType; intermediateNamespace.Types.Add(_intermediateClass); // Generate a partial class _intermediateClass.IsPartial = true; // Unless we're precompiling for updatable deployment, create the derived class if (!PrecompilingForUpdatableDeployment) { _sourceDataClass = new CodeTypeDeclaration(generatedClassName); // _sourceDataClass.BaseTypes.Add(new CodeTypeReference( Util.MakeFullTypeName(Parser.BaseTypeNamespace, Parser.BaseTypeName), CodeTypeReferenceOptions.GlobalReference)); _sourceDataNamespace.Types.Add(_sourceDataClass); } } else { // The page is not using code besides _intermediateClass = new CodeTypeDeclaration(generatedClassName); _intermediateClass.BaseTypes.Add(new CodeTypeReference(Parser.BaseType, CodeTypeReferenceOptions.GlobalReference)); _sourceDataNamespace.Types.Add(_intermediateClass); // There is only one class, so make both fields point to the same thing _sourceDataClass = _intermediateClass; } // Add the derived class namespace after the base partial class so C# parser // can still parse the code correctly in case the derived class contains error. // _codeCompileUnit.Namespaces.Add(_sourceDataNamespace); // We don't generate any code during updatable precompilation of a single (inline) page, // except for global.asax if (PrecompilingForUpdatableDeployment && Parser.CodeFileVirtualPath == null) return false; // Add metadata attributes to the class GenerateClassAttributes(); // In VB, always import Microsoft.VisualBasic ( if (_codeDomProvider is Microsoft.VisualBasic.VBCodeProvider) _sourceDataNamespace.Imports.Add(new CodeNamespaceImport("Microsoft.VisualBasic")); // Add all the namespaces if (Parser.NamespaceEntries != null) { foreach (NamespaceEntry entry in Parser.NamespaceEntries.Values) { // Create a line pragma if available CodeLinePragma linePragma; if (entry.VirtualPath != null) { linePragma = CreateCodeLinePragma(entry.VirtualPath, entry.Line); } else { linePragma = null; } CodeNamespaceImport nsi = new CodeNamespaceImport(entry.Namespace); nsi.LinePragma = linePragma; _sourceDataNamespace.Imports.Add(nsi); } } if (_sourceDataClass != null) { // We need to generate a global reference to avoid ambiguities ( string fullClassName = Util.MakeFullTypeName(_sourceDataNamespace.Name, _sourceDataClass.Name); CodeTypeReference classTypeRef = new CodeTypeReference(fullClassName, CodeTypeReferenceOptions.GlobalReference); // Since this is needed in several places, store it in a member variable _classTypeExpr = new CodeTypeReferenceExpression(classTypeRef); } // Add the implemented interfaces GenerateInterfaces(); // Build various properties, fields, methods BuildMiscClassMembers(); // Build the default constructors if (!_designerMode && _sourceDataClass != null) { _ctor = new CodeConstructor(); AddDebuggerNonUserCodeAttribute(_ctor); _sourceDataClass.Members.Add(_ctor); BuildDefaultConstructor(); } return true; } /* * Add metadata attributes to the class */ protected virtual void GenerateClassAttributes() { // If this is a debuggable page, generate a // CompilerGlobalScopeAttribute attribute (ASURT 33027) if (CompilParams.IncludeDebugInformation && _sourceDataClass != null) { CodeAttributeDeclaration attribDecl = new CodeAttributeDeclaration( "System.Runtime.CompilerServices.CompilerGlobalScopeAttribute"); _sourceDataClass.CustomAttributes.Add(attribDecl); } } /* * Generate the list of implemented interfaces */ protected virtual void GenerateInterfaces() { if (Parser.ImplementedInterfaces != null) { foreach (Type t in Parser.ImplementedInterfaces) { _intermediateClass.BaseTypes.Add(new CodeTypeReference(t)); } } } /* * Build first-time intialization statements */ protected virtual void BuildInitStatements(CodeStatementCollection trueStatements, CodeStatementCollection topLevelStatements) { } /* * Build the default constructor */ protected virtual void BuildDefaultConstructor() { _ctor.Attributes &= ~MemberAttributes.AccessMask; _ctor.Attributes |= MemberAttributes.Public; // private static bool __initialized; CodeMemberField initializedField = new CodeMemberField(typeof(bool), initializedFieldName); initializedField.Attributes |= MemberAttributes.Static; _sourceDataClass.Members.Add(initializedField); // if (__intialized == false) CodeConditionStatement initializedCondition = new CodeConditionStatement(); initializedCondition.Condition = new CodeBinaryOperatorExpression( new CodeFieldReferenceExpression( _classTypeExpr, initializedFieldName), CodeBinaryOperatorType.ValueEquality, new CodePrimitiveExpression(false)); this.BuildInitStatements(initializedCondition.TrueStatements, _ctor.Statements); initializedCondition.TrueStatements.Add(new CodeAssignStatement( new CodeFieldReferenceExpression( _classTypeExpr, initializedFieldName), new CodePrimitiveExpression(true))); // i.e. __intialized = true; _ctor.Statements.Add(initializedCondition); } /* * Build various properties, fields, methods */ protected virtual void BuildMiscClassMembers() { // Build the Profile property if (NeedProfileProperty) BuildProfileProperty(); // Skip the rest if we're only generating the intermediate class if (_sourceDataClass == null) return; // Build the injected properties from the global.asax
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XPathScanner.cs
- _OverlappedAsyncResult.cs
- RegistryConfigurationProvider.cs
- UpdateCompiler.cs
- Material.cs
- DataGridItemAutomationPeer.cs
- LinkAreaEditor.cs
- OdbcConnectionFactory.cs
- TraceLog.cs
- NativeMethods.cs
- ResetableIterator.cs
- TdsEnums.cs
- EntityConnectionStringBuilderItem.cs
- RuntimeEnvironment.cs
- TextPatternIdentifiers.cs
- ModelPropertyImpl.cs
- MarkedHighlightComponent.cs
- MultiSelector.cs
- KeyToListMap.cs
- Module.cs
- TextPatternIdentifiers.cs
- XmlAttributeHolder.cs
- XmlCountingReader.cs
- MatrixTransform3D.cs
- KeyGestureConverter.cs
- ExpandoClass.cs
- ToolboxItem.cs
- ProjectedWrapper.cs
- BrushMappingModeValidation.cs
- RightNameExpirationInfoPair.cs
- LinqMaximalSubtreeNominator.cs
- BitmapMetadataEnumerator.cs
- XomlCompilerHelpers.cs
- WindowsImpersonationContext.cs
- ItemsPresenter.cs
- MemberHolder.cs
- WebResourceAttribute.cs
- StrokeRenderer.cs
- MethodBody.cs
- WebPartMenuStyle.cs
- RegexCode.cs
- FocusWithinProperty.cs
- ListBoxItemAutomationPeer.cs
- WrappingXamlSchemaContext.cs
- BindUriHelper.cs
- HtmlUtf8RawTextWriter.cs
- AutomationTextAttribute.cs
- GPRECT.cs
- IndexedGlyphRun.cs
- ButtonBaseAdapter.cs
- ToolbarAUtomationPeer.cs
- AudioLevelUpdatedEventArgs.cs
- CurrentChangedEventManager.cs
- InvalidWorkflowException.cs
- XmlSchemaElement.cs
- CodeIdentifier.cs
- BitmapMetadataEnumerator.cs
- DeclarativeExpressionConditionDeclaration.cs
- ForeignKeyConstraint.cs
- DbProviderFactory.cs
- WaitForChangedResult.cs
- NumberFormatter.cs
- DataViewManager.cs
- WebColorConverter.cs
- CqlParser.cs
- SystemParameters.cs
- SqlUtils.cs
- PathGeometry.cs
- ObjectDisposedException.cs
- HtmlTableRowCollection.cs
- TypefaceCollection.cs
- Types.cs
- MaskedTextProvider.cs
- DateRangeEvent.cs
- DataGridViewEditingControlShowingEventArgs.cs
- Activity.cs
- UiaCoreProviderApi.cs
- DocumentEventArgs.cs
- AllMembershipCondition.cs
- SerializationEventsCache.cs
- ProfileSettingsCollection.cs
- WebEncodingValidatorAttribute.cs
- TextElementEnumerator.cs
- SortedDictionary.cs
- TypeValidationEventArgs.cs
- LinqExpressionNormalizer.cs
- SqlTypeConverter.cs
- MatchSingleFxEngineOpcode.cs
- _TimerThread.cs
- IndexerNameAttribute.cs
- BrushValueSerializer.cs
- SourceSwitch.cs
- CodePageUtils.cs
- HtmlTableCellCollection.cs
- StdRegProviderWrapper.cs
- CatalogZone.cs
- StrongNameIdentityPermission.cs
- CompiledQueryCacheEntry.cs
- CodeTypeParameterCollection.cs
- HtmlSelectionListAdapter.cs