Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- BamlStream.cs
- DataServiceException.cs
- EventItfInfo.cs
- ScriptIgnoreAttribute.cs
- UIHelper.cs
- UnSafeCharBuffer.cs
- RectAnimationClockResource.cs
- CustomErrorsSection.cs
- cookiecontainer.cs
- TypeUsageBuilder.cs
- MailSettingsSection.cs
- WSHttpBindingBaseElement.cs
- ProcessInfo.cs
- VisualTransition.cs
- ShaperBuffers.cs
- _AcceptOverlappedAsyncResult.cs
- HandlerBase.cs
- JsonUriDataContract.cs
- HiddenFieldDesigner.cs
- HtmlTitle.cs
- UrlMapping.cs
- JournalEntryListConverter.cs
- Menu.cs
- PasswordBox.cs
- WmpBitmapEncoder.cs
- Baml2006ReaderContext.cs
- CompilationRelaxations.cs
- WindowHideOrCloseTracker.cs
- EntityDataSourceStatementEditorForm.cs
- ServiceControllerDesigner.cs
- ChangeBlockUndoRecord.cs
- util.cs
- LocalizationParserHooks.cs
- AsmxEndpointPickerExtension.cs
- AccessDataSourceWizardForm.cs
- Rectangle.cs
- Transform3DCollection.cs
- CompilationPass2TaskInternal.cs
- WindowsGraphics2.cs
- SingleAnimation.cs
- WinFormsSecurity.cs
- indexingfiltermarshaler.cs
- SemanticBasicElement.cs
- Membership.cs
- HttpInputStream.cs
- InputLanguageManager.cs
- CodeLinePragma.cs
- TypeForwardedToAttribute.cs
- SerializationFieldInfo.cs
- CustomErrorCollection.cs
- InfiniteTimeSpanConverter.cs
- DataSet.cs
- ClientRolePrincipal.cs
- ListViewDeleteEventArgs.cs
- Control.cs
- InvokePattern.cs
- AnnotationObservableCollection.cs
- VectorAnimationBase.cs
- InvokeSchedule.cs
- CqlQuery.cs
- ClientSideQueueItem.cs
- ToolStripItemImageRenderEventArgs.cs
- EdmRelationshipRoleAttribute.cs
- HitTestParameters.cs
- OdbcConnectionHandle.cs
- ProjectionPlanCompiler.cs
- PackagingUtilities.cs
- UpDownBase.cs
- TypeSchema.cs
- BindStream.cs
- DispatchWrapper.cs
- BufferAllocator.cs
- ConfigXmlCDataSection.cs
- DataGridViewCellValidatingEventArgs.cs
- SecurityTraceRecordHelper.cs
- ToolStripDropDownItem.cs
- PasswordTextNavigator.cs
- LoadedOrUnloadedOperation.cs
- WebPartMenuStyle.cs
- XComponentModel.cs
- AsnEncodedData.cs
- Propagator.ExtentPlaceholderCreator.cs
- StaticFileHandler.cs
- ByteStream.cs
- FormParameter.cs
- ApplicationSettingsBase.cs
- HtmlTableCellCollection.cs
- PrintDocument.cs
- WrappedIUnknown.cs
- XmlIncludeAttribute.cs
- ThrowHelper.cs
- HandleRef.cs
- CacheAxisQuery.cs
- TcpTransportBindingElement.cs
- ArrangedElement.cs
- DesignerTransaction.cs
- Figure.cs
- TextMessageEncodingBindingElement.cs
- TextElementEditingBehaviorAttribute.cs
- ExpressionBinding.cs