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
- FormViewCommandEventArgs.cs
- ItemList.cs
- XmlLoader.cs
- RawStylusInputCustomDataList.cs
- TextProperties.cs
- XmlLinkedNode.cs
- MdbDataFileEditor.cs
- UInt32Storage.cs
- LinqDataSourceInsertEventArgs.cs
- MetabaseReader.cs
- StreamInfo.cs
- ConcurrentBag.cs
- TraceSection.cs
- TextTreeTextNode.cs
- ScriptingSectionGroup.cs
- MaskPropertyEditor.cs
- EnumBuilder.cs
- Viewport3DAutomationPeer.cs
- ClientRuntime.cs
- DataGridViewCellLinkedList.cs
- _ConnectOverlappedAsyncResult.cs
- CategoryAttribute.cs
- ModelItemKeyValuePair.cs
- BufferedWebEventProvider.cs
- FilteredXmlReader.cs
- PropVariant.cs
- ListSortDescription.cs
- DesignerProperties.cs
- DesignerCategoryAttribute.cs
- XmlDictionaryReaderQuotas.cs
- SourceFilter.cs
- ClientSponsor.cs
- TriggerActionCollection.cs
- ButtonFlatAdapter.cs
- XmlKeywords.cs
- DataSourceCache.cs
- AspProxy.cs
- GlyphRunDrawing.cs
- CodeTypeReferenceCollection.cs
- DbSourceParameterCollection.cs
- ZipIOExtraFieldElement.cs
- ResourceReferenceExpressionConverter.cs
- BeginEvent.cs
- AuthenticationModeHelper.cs
- SmiEventSink_Default.cs
- ListItemsPage.cs
- QuotedPrintableStream.cs
- TemplateBindingExtension.cs
- Vector3DConverter.cs
- SearchForVirtualItemEventArgs.cs
- DataGridViewRowDividerDoubleClickEventArgs.cs
- TriggerBase.cs
- DebugControllerThread.cs
- DataControlFieldHeaderCell.cs
- DCSafeHandle.cs
- EntityUtil.cs
- CompoundFileReference.cs
- CodeNamespace.cs
- ProviderException.cs
- AttachedPropertyBrowsableForChildrenAttribute.cs
- SHA1Cng.cs
- NotImplementedException.cs
- VisualStateManager.cs
- EntityModelBuildProvider.cs
- SecureEnvironment.cs
- cryptoapiTransform.cs
- PriorityBinding.cs
- EmitterCache.cs
- _UriTypeConverter.cs
- WindowsNonControl.cs
- PageCodeDomTreeGenerator.cs
- ContainerFilterService.cs
- PropertyGrid.cs
- RemotingServices.cs
- RegisteredDisposeScript.cs
- StrokeCollection2.cs
- ClientBuildManager.cs
- NamedPipeTransportElement.cs
- DataServiceHost.cs
- SqlInternalConnection.cs
- OrderByQueryOptionExpression.cs
- MetadataPropertyAttribute.cs
- CancellationTokenSource.cs
- HostProtectionPermission.cs
- HostingPreferredMapPath.cs
- OleDbConnectionFactory.cs
- RequestUriProcessor.cs
- PictureBox.cs
- LinkButton.cs
- DurableTimerExtension.cs
- XmlComment.cs
- DependencyObjectProvider.cs
- FontCacheUtil.cs
- ContainerFilterService.cs
- UITypeEditor.cs
- oledbmetadatacolumnnames.cs
- MessageQueueEnumerator.cs
- NodeFunctions.cs
- PropertyPath.cs
- MsmqReceiveParameters.cs