Code:
/ FX-1434 / FX-1434 / 1.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
- ImageBrush.cs
- XmlSchemaAny.cs
- DuplicateWaitObjectException.cs
- TcpTransportSecurity.cs
- DataSourceNameHandler.cs
- SamlDoNotCacheCondition.cs
- AppSecurityManager.cs
- AbandonedMutexException.cs
- ParseHttpDate.cs
- DynamicControl.cs
- BitmapImage.cs
- PrivilegedConfigurationManager.cs
- streamingZipPartStream.cs
- ListViewTableRow.cs
- CalendarSelectionChangedEventArgs.cs
- MessageQueueEnumerator.cs
- securitycriticaldata.cs
- ChannelServices.cs
- XmlLanguage.cs
- NoneExcludedImageIndexConverter.cs
- BackgroundWorker.cs
- WorkflowFileItem.cs
- XmlSchemaSimpleContentRestriction.cs
- SimpleApplicationHost.cs
- DataServiceContext.cs
- EnumConverter.cs
- XpsResourceDictionary.cs
- BmpBitmapEncoder.cs
- MtomMessageEncodingElement.cs
- SqlGenericUtil.cs
- UriSectionData.cs
- shaper.cs
- XmlSchemaComplexContent.cs
- MappedMetaModel.cs
- DomainLiteralReader.cs
- XmlDataSourceView.cs
- SynchronizingStream.cs
- AesManaged.cs
- TerminateSequence.cs
- DispatcherObject.cs
- FormViewModeEventArgs.cs
- MultiSelectRootGridEntry.cs
- IsolatedStorage.cs
- QueryOptionExpression.cs
- Label.cs
- DbMetaDataCollectionNames.cs
- GAC.cs
- CollectionTypeElement.cs
- EnlistmentTraceIdentifier.cs
- VisualStyleTypesAndProperties.cs
- ContentType.cs
- WebPartZoneBase.cs
- XsltLibrary.cs
- HandlerFactoryCache.cs
- CallbackHandler.cs
- ValueQuery.cs
- SourceFilter.cs
- CodeTypeMember.cs
- PopOutPanel.cs
- XmlDataContract.cs
- Section.cs
- PointConverter.cs
- IndexedString.cs
- BaseDataList.cs
- IssuedSecurityTokenProvider.cs
- DesignerEditorPartChrome.cs
- WmfPlaceableFileHeader.cs
- UnionCqlBlock.cs
- ViewCellSlot.cs
- Win32MouseDevice.cs
- HttpResponseHeader.cs
- DrawingContextDrawingContextWalker.cs
- XmlExceptionHelper.cs
- HtmlWindow.cs
- LowerCaseStringConverter.cs
- ClientUrlResolverWrapper.cs
- BooleanAnimationBase.cs
- CodeTypeReference.cs
- HtmlUtf8RawTextWriter.cs
- EncryptedData.cs
- figurelength.cs
- EnumDataContract.cs
- InteropEnvironment.cs
- SQLConvert.cs
- DESCryptoServiceProvider.cs
- ButtonAutomationPeer.cs
- TrackingMemoryStream.cs
- DataContractJsonSerializer.cs
- DataControlFieldCollection.cs
- BigInt.cs
- SqlColumnizer.cs
- ListMarkerSourceInfo.cs
- PropertyConverter.cs
- InfoCardPolicy.cs
- EmptyStringExpandableObjectConverter.cs
- CaretElement.cs
- TextBoxBase.cs
- PolicyManager.cs
- CodeTypeReferenceExpression.cs
- BoolExpr.cs