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
- SystemIPv4InterfaceProperties.cs
- CloudCollection.cs
- RTLAwareMessageBox.cs
- TextOutput.cs
- SqlDataSourceSelectingEventArgs.cs
- ColorPalette.cs
- CounterCreationDataCollection.cs
- TrackingMemoryStream.cs
- AtomPub10CategoriesDocumentFormatter.cs
- PeerCollaborationPermission.cs
- ScriptMethodAttribute.cs
- Nullable.cs
- ImageBrush.cs
- ColumnMapTranslator.cs
- MdiWindowListItemConverter.cs
- VariableDesigner.xaml.cs
- SqlConnectionHelper.cs
- ExceptionRoutedEventArgs.cs
- sqlcontext.cs
- Memoizer.cs
- UnsafeNativeMethods.cs
- QueryAccessibilityHelpEvent.cs
- BindableAttribute.cs
- UserControlBuildProvider.cs
- DecimalConstantAttribute.cs
- Pen.cs
- UserControlCodeDomTreeGenerator.cs
- StylusButtonEventArgs.cs
- KeyboardDevice.cs
- PingReply.cs
- ValueType.cs
- RootBrowserWindow.cs
- FamilyMapCollection.cs
- PrintPageEvent.cs
- prefixendpointaddressmessagefiltertable.cs
- BindingNavigatorDesigner.cs
- InfoCardRSAOAEPKeyExchangeFormatter.cs
- HandlerFactoryCache.cs
- PipelineModuleStepContainer.cs
- PointAnimation.cs
- Wizard.cs
- SkewTransform.cs
- RichTextBoxAutomationPeer.cs
- COAUTHIDENTITY.cs
- FontFamily.cs
- LineServicesRun.cs
- SafeThemeHandle.cs
- RawStylusInputReport.cs
- ThicknessAnimationBase.cs
- ItemContainerGenerator.cs
- SimplePropertyEntry.cs
- SafeMarshalContext.cs
- PtsCache.cs
- IntranetCredentialPolicy.cs
- StylusOverProperty.cs
- AssociationTypeEmitter.cs
- DateTimeConstantAttribute.cs
- AssociationType.cs
- EnvironmentPermission.cs
- MetafileHeaderWmf.cs
- MenuCommand.cs
- ProfileBuildProvider.cs
- PageHandlerFactory.cs
- Localizer.cs
- BufferedGraphicsContext.cs
- XsdValidatingReader.cs
- FormatException.cs
- IERequestCache.cs
- LowerCaseStringConverter.cs
- XLinq.cs
- ObjectDataSourceFilteringEventArgs.cs
- FixedDocumentPaginator.cs
- ColorConvertedBitmapExtension.cs
- ValueTypeFixupInfo.cs
- UnsupportedPolicyOptionsException.cs
- CreateUserErrorEventArgs.cs
- SweepDirectionValidation.cs
- CodeFieldReferenceExpression.cs
- InvokeBinder.cs
- Invariant.cs
- SoapFault.cs
- ArrayElementGridEntry.cs
- RowParagraph.cs
- ToolStripItemEventArgs.cs
- QueryStringHandler.cs
- InputProviderSite.cs
- CharEntityEncoderFallback.cs
- UdpSocketReceiveManager.cs
- SingleStorage.cs
- CommandPlan.cs
- UniqueEventHelper.cs
- CodeAssignStatement.cs
- WindowsGrip.cs
- SHA256Managed.cs
- KnownAssemblyEntry.cs
- TraceXPathNavigator.cs
- XmlStreamStore.cs
- OneOfTypeConst.cs
- Vars.cs
- InvokeHandlers.cs