Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / 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; } } }
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PenLineCapValidation.cs
- SelectedGridItemChangedEvent.cs
- OperatingSystem.cs
- VBCodeProvider.cs
- sqlpipe.cs
- XmlName.cs
- InvokeMethodActivity.cs
- TraceSection.cs
- QilValidationVisitor.cs
- BufferAllocator.cs
- RadioButton.cs
- DbConvert.cs
- OperationCanceledException.cs
- FileReservationCollection.cs
- RelationshipWrapper.cs
- TemplateEditingService.cs
- ReadOnlyCollectionBase.cs
- FamilyTypefaceCollection.cs
- DataTrigger.cs
- ScrollEvent.cs
- MethodImplAttribute.cs
- ColumnMapVisitor.cs
- SafeLocalMemHandle.cs
- DataTable.cs
- KeyPressEvent.cs
- GridViewDeletedEventArgs.cs
- TrustSection.cs
- CookieProtection.cs
- SQLInt64.cs
- SelectionEditor.cs
- XmlSiteMapProvider.cs
- CustomErrorsSectionWrapper.cs
- ObjectListShowCommandsEventArgs.cs
- ByteConverter.cs
- HandleCollector.cs
- ApplicationSettingsBase.cs
- altserialization.cs
- ConsumerConnectionPointCollection.cs
- Tag.cs
- Selection.cs
- Margins.cs
- DataGridViewSelectedRowCollection.cs
- ManualResetEvent.cs
- AutomationPropertyInfo.cs
- BinHexDecoder.cs
- DocumentViewerBaseAutomationPeer.cs
- ApplicationFileCodeDomTreeGenerator.cs
- CalendarTable.cs
- DateTimeFormatInfo.cs
- MetricEntry.cs
- HandleRef.cs
- Run.cs
- MDIControlStrip.cs
- ItemType.cs
- FontWeights.cs
- Stream.cs
- SystemDropShadowChrome.cs
- TextServicesHost.cs
- FontUnit.cs
- URI.cs
- EventRoute.cs
- OleAutBinder.cs
- PageParserFilter.cs
- SpellerInterop.cs
- Helper.cs
- CompositeActivityCodeGenerator.cs
- RoutedCommand.cs
- UnmanagedMemoryStream.cs
- ChangeBlockUndoRecord.cs
- Rfc4050KeyFormatter.cs
- ConsumerConnectionPointCollection.cs
- BindingManagerDataErrorEventArgs.cs
- StringPropertyBuilder.cs
- TemplateFactory.cs
- ClientSettings.cs
- ExpressionStringBuilder.cs
- _MultipleConnectAsync.cs
- _ListenerResponseStream.cs
- Point3D.cs
- ObjectManager.cs
- OracleBinary.cs
- ClientRuntimeConfig.cs
- BaseInfoTable.cs
- GenericTypeParameterBuilder.cs
- CommandConverter.cs
- ComponentResourceKey.cs
- DataViewSettingCollection.cs
- LineGeometry.cs
- FontFamily.cs
- DrawItemEvent.cs
- ConstrainedGroup.cs
- ViewManager.cs
- PageRequestManager.cs
- HttpHandlerAction.cs
- ObjectNotFoundException.cs
- ContentType.cs
- XmlSchemaException.cs
- ResourceReader.cs
- XamlContextStack.cs
- tooltip.cs