Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / xsp / System / Web / Compilation / AppSettingsExpressionBuilder.cs / 1 / 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)] [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] 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)] [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] 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
- ObjectManager.cs
- TableCell.cs
- TableAdapterManagerGenerator.cs
- QueryRewriter.cs
- CorrelationManager.cs
- ClickablePoint.cs
- EntityDataSourceSelectedEventArgs.cs
- SchemaLookupTable.cs
- ResourceManagerWrapper.cs
- DbConnectionPool.cs
- handlecollector.cs
- CodeCatchClauseCollection.cs
- CommandConverter.cs
- NTAccount.cs
- Calendar.cs
- BaseTemplateBuildProvider.cs
- XmlTextReaderImpl.cs
- BitmapEffectDrawingContextWalker.cs
- AssemblyBuilder.cs
- EdmToObjectNamespaceMap.cs
- XmlElementAttributes.cs
- CodeTypeReferenceExpression.cs
- FSWPathEditor.cs
- DynamicRendererThreadManager.cs
- XmlSchemas.cs
- DBCSCodePageEncoding.cs
- ListSourceHelper.cs
- InputEventArgs.cs
- OptimizedTemplateContent.cs
- EditorZone.cs
- DesignTimeTemplateParser.cs
- Renderer.cs
- TrimSurroundingWhitespaceAttribute.cs
- EntityDataSourceEntitySetNameItem.cs
- ConnectionPoint.cs
- metrodevice.cs
- FlowDocument.cs
- EdmValidator.cs
- NativeMethods.cs
- UserControlFileEditor.cs
- SyntaxCheck.cs
- HtmlFormParameterReader.cs
- ArraySubsetEnumerator.cs
- SharedHttpsTransportManager.cs
- HMACMD5.cs
- BamlTreeMap.cs
- ListViewTableRow.cs
- WindowsGraphicsCacheManager.cs
- ConnectionStringEditor.cs
- DbBuffer.cs
- DynamicFilter.cs
- DesignerDataTable.cs
- BitArray.cs
- CompatibleComparer.cs
- ContractListAdapter.cs
- WebPartZone.cs
- RegexMatchCollection.cs
- DataGridViewCellValueEventArgs.cs
- NativeMethods.cs
- WebPartDescriptionCollection.cs
- ClosableStream.cs
- AuthenticationSection.cs
- MethodCallExpression.cs
- ZipFileInfoCollection.cs
- Scene3D.cs
- RoutingUtilities.cs
- EntityConnection.cs
- DataChangedEventManager.cs
- Compiler.cs
- ADConnectionHelper.cs
- ExpressionBuilderContext.cs
- SynchronizedDispatch.cs
- ItemCheckedEvent.cs
- FactoryMaker.cs
- ScaleTransform3D.cs
- TransportOutputChannel.cs
- CompositeKey.cs
- Interlocked.cs
- DataBoundControlDesigner.cs
- EventMappingSettingsCollection.cs
- ControlDesigner.cs
- SecurityRequiresReviewAttribute.cs
- Expander.cs
- BaseCAMarshaler.cs
- CommandExpr.cs
- _ConnectionGroup.cs
- XomlSerializationHelpers.cs
- ConnectivityStatus.cs
- LinqDataView.cs
- DesignerRegion.cs
- SafeProcessHandle.cs
- Overlapped.cs
- ResourceExpressionBuilder.cs
- Stack.cs
- AdjustableArrowCap.cs
- RegexMatch.cs
- SecurityTokenProvider.cs
- PerformanceCounterManager.cs
- MouseGestureValueSerializer.cs
- FillBehavior.cs