Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / xsp / System / Web / Compilation / AppSettingsExpressionBuilder.cs / 1305376 / AppSettingsExpressionBuilder.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Compilation { using System; using System.Security.Permissions; using System.CodeDom; using System.ComponentModel; using System.Configuration; using System.Diagnostics; using System.Web.UI; [ExpressionPrefix("AppSettings")] [ExpressionEditor("System.Web.UI.Design.AppSettingsExpressionEditor, " + AssemblyRef.SystemDesign)] public class AppSettingsExpressionBuilder : ExpressionBuilder { public override bool SupportsEvaluate { get { return true; } } public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { if (entry.DeclaringType == null || entry.PropertyInfo == null) { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim())); } else { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim()), new CodeTypeOfExpression(entry.DeclaringType), new CodePrimitiveExpression(entry.PropertyInfo.Name)); } } public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { return GetAppSetting(entry.Expression, target.GetType(), entry.PropertyInfo.Name); } public static object GetAppSetting(string key) { string value = ConfigurationManager.AppSettings[key]; if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } public static object GetAppSetting(string key, Type targetType, string propertyName) { string value = ConfigurationManager.AppSettings[key]; if (targetType != null) { PropertyDescriptor propDesc = TypeDescriptor.GetProperties(targetType)[propertyName]; if (propDesc != null) { if (propDesc.PropertyType != typeof(string)) { TypeConverter converter = propDesc.Converter; if (converter.CanConvertFrom(typeof(string))) { return converter.ConvertFrom(value); } else { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_convertible, value, propDesc.PropertyType.Name, propDesc.Name)); } } } } if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- namespace System.Web.Compilation { using System; using System.Security.Permissions; using System.CodeDom; using System.ComponentModel; using System.Configuration; using System.Diagnostics; using System.Web.UI; [ExpressionPrefix("AppSettings")] [ExpressionEditor("System.Web.UI.Design.AppSettingsExpressionEditor, " + AssemblyRef.SystemDesign)] public class AppSettingsExpressionBuilder : ExpressionBuilder { public override bool SupportsEvaluate { get { return true; } } public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { if (entry.DeclaringType == null || entry.PropertyInfo == null) { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim())); } else { return new CodeMethodInvokeExpression( new CodeTypeReferenceExpression(this.GetType()), "GetAppSetting", new CodePrimitiveExpression(entry.Expression.Trim()), new CodeTypeOfExpression(entry.DeclaringType), new CodePrimitiveExpression(entry.PropertyInfo.Name)); } } public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) { return GetAppSetting(entry.Expression, target.GetType(), entry.PropertyInfo.Name); } public static object GetAppSetting(string key) { string value = ConfigurationManager.AppSettings[key]; if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } public static object GetAppSetting(string key, Type targetType, string propertyName) { string value = ConfigurationManager.AppSettings[key]; if (targetType != null) { PropertyDescriptor propDesc = TypeDescriptor.GetProperties(targetType)[propertyName]; if (propDesc != null) { if (propDesc.PropertyType != typeof(string)) { TypeConverter converter = propDesc.Converter; if (converter.CanConvertFrom(typeof(string))) { return converter.ConvertFrom(value); } else { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_convertible, value, propDesc.PropertyType.Name, propDesc.Name)); } } } } if (value == null) { throw new InvalidOperationException(SR.GetString(SR.AppSetting_not_found, key)); } return value; } } } // 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
- ErrorHandlingReceiver.cs
- InkCollectionBehavior.cs
- WorkflowServiceHost.cs
- ArglessEventHandlerProxy.cs
- SecurityTokenRequirement.cs
- EditorPartChrome.cs
- TrackBarRenderer.cs
- Renderer.cs
- AliasGenerator.cs
- ValueTypeFixupInfo.cs
- ListBindingHelper.cs
- SectionInput.cs
- StylusOverProperty.cs
- RequestCacheValidator.cs
- PropertyIDSet.cs
- ChangePasswordDesigner.cs
- HashSetEqualityComparer.cs
- figurelengthconverter.cs
- AuthorizationRule.cs
- ColorInterpolationModeValidation.cs
- _Semaphore.cs
- FrameworkName.cs
- CompressedStack.cs
- CSharpCodeProvider.cs
- ProfileEventArgs.cs
- TreeViewAutomationPeer.cs
- BaseParagraph.cs
- CodeSubDirectoriesCollection.cs
- ConnectivityStatus.cs
- TypeContext.cs
- CodeMethodReturnStatement.cs
- BinarySerializer.cs
- ServicePointManager.cs
- LocalizedNameDescriptionPair.cs
- DataGridBeginningEditEventArgs.cs
- FocusTracker.cs
- ChainedAsyncResult.cs
- XmlSchemaIdentityConstraint.cs
- DllNotFoundException.cs
- UnknownWrapper.cs
- SqlDataSourceStatusEventArgs.cs
- DSASignatureDeformatter.cs
- DataReceivedEventArgs.cs
- XmlLanguageConverter.cs
- TransformedBitmap.cs
- TextDecoration.cs
- DefaultBinder.cs
- ToolStripGripRenderEventArgs.cs
- ImageMapEventArgs.cs
- UncommonField.cs
- ReflectionHelper.cs
- DecoderFallback.cs
- Point3DCollection.cs
- FragmentQueryKB.cs
- SID.cs
- ByteKeyFrameCollection.cs
- ContainerParagraph.cs
- NameValueConfigurationElement.cs
- StorageModelBuildProvider.cs
- FixedSOMFixedBlock.cs
- DelegatedStream.cs
- ValidationPropertyAttribute.cs
- UICuesEvent.cs
- RightsManagementInformation.cs
- GuidTagList.cs
- ContentPresenter.cs
- BaseHashHelper.cs
- AutomationPeer.cs
- arabicshape.cs
- StylusPlugin.cs
- MergablePropertyAttribute.cs
- AssociationTypeEmitter.cs
- StickyNoteAnnotations.cs
- BackoffTimeoutHelper.cs
- WindowsRebar.cs
- TextElementCollectionHelper.cs
- PKCS1MaskGenerationMethod.cs
- XmlWriterDelegator.cs
- DiagnosticTrace.cs
- CharacterBuffer.cs
- CustomError.cs
- Viewport2DVisual3D.cs
- _SslStream.cs
- QilPatternFactory.cs
- XmlDeclaration.cs
- GridViewRow.cs
- ValidationPropertyAttribute.cs
- CompoundFileStreamReference.cs
- BrowserTree.cs
- ChangeConflicts.cs
- SqlBooleanMismatchVisitor.cs
- Grammar.cs
- StructuralType.cs
- ResourceContainer.cs
- DataGridColumn.cs
- TreeViewEvent.cs
- OleDbException.cs
- CombinedGeometry.cs
- DataGridViewEditingControlShowingEventArgs.cs
- HttpProfileBase.cs