Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / 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
- OleDbConnectionPoolGroupProviderInfo.cs
- LinkTarget.cs
- DesignerHierarchicalDataSourceView.cs
- AssemblyNameProxy.cs
- COM2PictureConverter.cs
- TreeIterators.cs
- DataControlLinkButton.cs
- RichTextBoxAutomationPeer.cs
- HostingEnvironmentSection.cs
- TextCharacters.cs
- CfgParser.cs
- PropertyPathConverter.cs
- DBCSCodePageEncoding.cs
- XmlILOptimizerVisitor.cs
- BuildResult.cs
- SymLanguageVendor.cs
- ForwardPositionQuery.cs
- EnumConverter.cs
- WebBrowser.cs
- SecurityState.cs
- ScrollProviderWrapper.cs
- ListViewDeletedEventArgs.cs
- TrustSection.cs
- StyleModeStack.cs
- TypedRowGenerator.cs
- UseLicense.cs
- WorkflowClientDeliverMessageWrapper.cs
- Package.cs
- updatecommandorderer.cs
- ViewSimplifier.cs
- SafeNativeMethods.cs
- CodeConditionStatement.cs
- _NegotiateClient.cs
- SerialPinChanges.cs
- CodeGenHelper.cs
- Configuration.cs
- Constant.cs
- SQLBinaryStorage.cs
- MergeFailedEvent.cs
- DocumentPropertiesDialog.cs
- PersonalizationStateQuery.cs
- ScrollContentPresenter.cs
- PolicyLevel.cs
- TypeListConverter.cs
- DataPagerField.cs
- WebEventTraceProvider.cs
- MetabaseSettingsIis7.cs
- Tile.cs
- securitymgrsite.cs
- SqlParameter.cs
- PropertyNames.cs
- mansign.cs
- DBParameter.cs
- ResponseStream.cs
- WebColorConverter.cs
- TextTreeUndoUnit.cs
- UxThemeWrapper.cs
- EntityDesignerDataSourceView.cs
- Transform.cs
- VirtualPathData.cs
- TraceInternal.cs
- XPathScanner.cs
- ReadWriteSpinLock.cs
- PhysicalAddress.cs
- ObjectListGeneralPage.cs
- SoapSchemaExporter.cs
- QuaternionIndependentAnimationStorage.cs
- XPathNodeList.cs
- CharacterMetricsDictionary.cs
- EncoderExceptionFallback.cs
- DataFormats.cs
- Rotation3D.cs
- HttpRuntime.cs
- PagedDataSource.cs
- ResourceManager.cs
- Attributes.cs
- HttpCookiesSection.cs
- _NestedSingleAsyncResult.cs
- DataGridViewComponentPropertyGridSite.cs
- ReflectPropertyDescriptor.cs
- TabControlToolboxItem.cs
- BuildManager.cs
- SystemInformation.cs
- CacheHelper.cs
- InputMethodStateTypeInfo.cs
- FrugalList.cs
- BindingValueChangedEventArgs.cs
- Tuple.cs
- RelationshipManager.cs
- PrimarySelectionAdorner.cs
- __ComObject.cs
- PipelineModuleStepContainer.cs
- Token.cs
- HWStack.cs
- SpanIndex.cs
- NamedPipeChannelFactory.cs
- MarshalDirectiveException.cs
- StrokeIntersection.cs
- RegexRunnerFactory.cs
- SmiMetaData.cs