Code:
/ DotNET / DotNET / 8.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
- Binding.cs
- AttachedAnnotationChangedEventArgs.cs
- RuleDefinitions.cs
- SqlTopReducer.cs
- AstTree.cs
- ProxyWebPartConnectionCollection.cs
- LayoutTableCell.cs
- arclist.cs
- SqlRemoveConstantOrderBy.cs
- WebPart.cs
- ColorContextHelper.cs
- DeviceFilterDictionary.cs
- ByValueEqualityComparer.cs
- GraphicsPath.cs
- PropertyFilterAttribute.cs
- _AutoWebProxyScriptEngine.cs
- DataSourceHelper.cs
- ConvertEvent.cs
- StylusPointPropertyInfoDefaults.cs
- Exception.cs
- SqlException.cs
- TrustManagerPromptUI.cs
- FileLogRecordStream.cs
- ConfigXmlElement.cs
- DisableDpiAwarenessAttribute.cs
- ClientApiGenerator.cs
- webclient.cs
- DesignTable.cs
- ButtonAutomationPeer.cs
- XmlNamespaceDeclarationsAttribute.cs
- DelegateSerializationHolder.cs
- ToolBar.cs
- BrushValueSerializer.cs
- DesignTimeTemplateParser.cs
- ClientRolePrincipal.cs
- Stroke2.cs
- StoreItemCollection.Loader.cs
- StylusSystemGestureEventArgs.cs
- XMLUtil.cs
- ThreadExceptionDialog.cs
- SupportingTokenBindingElement.cs
- MiniConstructorInfo.cs
- CaseInsensitiveOrdinalStringComparer.cs
- TextEndOfParagraph.cs
- BitmapEffectInput.cs
- GrammarBuilderWildcard.cs
- AssemblyInfo.cs
- ExpandableObjectConverter.cs
- AdapterDictionary.cs
- DynamicControlParameter.cs
- DataSourceExpressionCollection.cs
- XmlRawWriterWrapper.cs
- EmptyEnumerator.cs
- TypeDelegator.cs
- BaseDataList.cs
- UserControlCodeDomTreeGenerator.cs
- SqlConnection.cs
- LinkLabel.cs
- ColumnCollection.cs
- EditorPartCollection.cs
- AssemblyUtil.cs
- Timer.cs
- DataBoundControlHelper.cs
- Pen.cs
- AesCryptoServiceProvider.cs
- InstalledVoice.cs
- RuntimeConfig.cs
- SqlUDTStorage.cs
- WebBrowserNavigatedEventHandler.cs
- XmlSchemaImporter.cs
- Token.cs
- SelectedGridItemChangedEvent.cs
- NameObjectCollectionBase.cs
- CompoundFileReference.cs
- XPathDescendantIterator.cs
- MSAANativeProvider.cs
- DefaultIfEmptyQueryOperator.cs
- Attribute.cs
- templategroup.cs
- TemplatePagerField.cs
- HyperLinkColumn.cs
- SqlFlattener.cs
- SchemaImporter.cs
- FolderBrowserDialog.cs
- HtmlSelect.cs
- ConnectionStringsSection.cs
- StoreContentChangedEventArgs.cs
- mediaeventshelper.cs
- SerialStream.cs
- XmlLoader.cs
- Tokenizer.cs
- DataGridViewDataErrorEventArgs.cs
- DataBoundControlDesigner.cs
- OperatingSystem.cs
- UrlAuthFailureHandler.cs
- KeyInfo.cs
- ActivitySurrogateSelector.cs
- CodeMethodReturnStatement.cs
- UpdateCommand.cs
- SymbolMethod.cs