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
- DesignRelationCollection.cs
- FormView.cs
- EtwTrackingParticipant.cs
- BitmapEffectInputConnector.cs
- StsCommunicationException.cs
- ForceCopyBuildProvider.cs
- GenericTextProperties.cs
- TreeViewCancelEvent.cs
- InvokeSchedule.cs
- Profiler.cs
- HtmlElementCollection.cs
- StyleTypedPropertyAttribute.cs
- XmlSignatureProperties.cs
- RefExpr.cs
- COM2TypeInfoProcessor.cs
- DataTableCollection.cs
- entityreference_tresulttype.cs
- BuildProviderAppliesToAttribute.cs
- CodeGen.cs
- PeerName.cs
- SizeConverter.cs
- SystemNetworkInterface.cs
- MatrixTransform.cs
- ReadOnlyCollection.cs
- HeaderLabel.cs
- TextRangeBase.cs
- AccessDataSource.cs
- VariableExpressionConverter.cs
- CorrelationTokenInvalidatedHandler.cs
- RootBuilder.cs
- GridViewHeaderRowPresenterAutomationPeer.cs
- DynamicDiscoveryDocument.cs
- FileIOPermission.cs
- EntityDataSourceDataSelection.cs
- StringDictionary.cs
- SqlNodeAnnotations.cs
- _Events.cs
- RewritingValidator.cs
- IRCollection.cs
- XhtmlBasicFormAdapter.cs
- Transform3DGroup.cs
- BitmapImage.cs
- ShutDownListener.cs
- DataControlFieldHeaderCell.cs
- UserMapPath.cs
- ResXResourceReader.cs
- Errors.cs
- TypeNameConverter.cs
- FormViewDeleteEventArgs.cs
- CodeCastExpression.cs
- ReaderOutput.cs
- TypeHelpers.cs
- Rect.cs
- SubMenuStyle.cs
- CompilerGeneratedAttribute.cs
- Parallel.cs
- TableAutomationPeer.cs
- SaveFileDialog.cs
- CalendarDateRangeChangingEventArgs.cs
- DiscoveryClientProtocol.cs
- FontCacheUtil.cs
- HtmlUtf8RawTextWriter.cs
- ExecutionScope.cs
- HitTestParameters3D.cs
- LogSwitch.cs
- TokenCreationException.cs
- ColorContext.cs
- InvalidFilterCriteriaException.cs
- InvalidProgramException.cs
- ListQueryResults.cs
- JsonWriter.cs
- ErrorHandler.cs
- ToolStripTextBox.cs
- NodeFunctions.cs
- BaseCodePageEncoding.cs
- RegexTree.cs
- RequiredFieldValidator.cs
- RelationshipConverter.cs
- RadioButton.cs
- ClientProxyGenerator.cs
- ZipIOZip64EndOfCentralDirectoryLocatorBlock.cs
- SafeNativeMethodsCLR.cs
- MorphHelpers.cs
- InputMethodStateChangeEventArgs.cs
- ConfigurationSectionGroup.cs
- ParamArrayAttribute.cs
- InputMethod.cs
- BooleanSwitch.cs
- SQLDouble.cs
- HyperLink.cs
- DependencyObjectProvider.cs
- ErrorLog.cs
- HtmlShim.cs
- BamlVersionHeader.cs
- DataSetUtil.cs
- CharAnimationUsingKeyFrames.cs
- EntityTypeBase.cs
- ColorContext.cs
- FormCollection.cs
- CacheOutputQuery.cs