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
- SourceCollection.cs
- RenderData.cs
- LineUtil.cs
- WebDescriptionAttribute.cs
- PrinterSettings.cs
- StrokeCollection.cs
- ProxyManager.cs
- HeaderCollection.cs
- CodeChecksumPragma.cs
- GridViewCommandEventArgs.cs
- ExtendedPropertyCollection.cs
- ZipFileInfo.cs
- SymbolType.cs
- CodeAttributeDeclaration.cs
- CloudCollection.cs
- SQLStringStorage.cs
- PathParser.cs
- PeerObject.cs
- ContentPresenter.cs
- XmlHierarchicalEnumerable.cs
- parserscommon.cs
- MemoryMappedFileSecurity.cs
- Propagator.JoinPropagator.cs
- IdentityNotMappedException.cs
- Model3DCollection.cs
- DPTypeDescriptorContext.cs
- TypeElement.cs
- EventPrivateKey.cs
- MetadataArtifactLoaderFile.cs
- UnorderedHashRepartitionStream.cs
- RenderContext.cs
- ResXDataNode.cs
- RegexWorker.cs
- ValueHandle.cs
- HttpCachePolicyElement.cs
- IndexedEnumerable.cs
- XmlEventCache.cs
- ContainerTracking.cs
- StrongNamePublicKeyBlob.cs
- ResourcePool.cs
- QilScopedVisitor.cs
- KnownIds.cs
- Transform3DGroup.cs
- UnsafeNativeMethodsPenimc.cs
- CompiledQuery.cs
- indexingfiltermarshaler.cs
- IndicShape.cs
- EditingMode.cs
- GeometryGroup.cs
- Border.cs
- TemplateBuilder.cs
- QilTargetType.cs
- FontUnitConverter.cs
- FastEncoderWindow.cs
- Int32Collection.cs
- HtmlTitle.cs
- EventRecord.cs
- DataTemplate.cs
- OracleEncoding.cs
- ScrollChrome.cs
- configsystem.cs
- HttpProtocolImporter.cs
- DataRelationCollection.cs
- MembershipValidatePasswordEventArgs.cs
- UpdatableWrapper.cs
- diagnosticsswitches.cs
- EventOpcode.cs
- DispatcherTimer.cs
- IDictionary.cs
- TextReturnReader.cs
- AuthenticationServiceManager.cs
- StringSorter.cs
- FontFamily.cs
- XmlSchemaChoice.cs
- FixedTextContainer.cs
- BamlBinaryReader.cs
- ExecutedRoutedEventArgs.cs
- RtfToXamlLexer.cs
- DesignerPerfEventProvider.cs
- MenuEventArgs.cs
- AnimatedTypeHelpers.cs
- FamilyCollection.cs
- BaseDataBoundControl.cs
- FtpWebRequest.cs
- PageThemeCodeDomTreeGenerator.cs
- TemplateBindingExtensionConverter.cs
- HitTestWithGeometryDrawingContextWalker.cs
- TypeRestriction.cs
- MemoryFailPoint.cs
- BufferedStream.cs
- AnchoredBlock.cs
- RC2.cs
- GeometryCollection.cs
- httpapplicationstate.cs
- StringResourceManager.cs
- LinkTarget.cs
- TimeoutValidationAttribute.cs
- DataTrigger.cs
- IisTraceWebEventProvider.cs
- WorkingDirectoryEditor.cs