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
- SchemaEntity.cs
- HtmlControl.cs
- StylusPointProperty.cs
- XmlDocumentType.cs
- LockCookie.cs
- SafeJobHandle.cs
- MediaTimeline.cs
- SessionSwitchEventArgs.cs
- ParallelEnumerable.cs
- MailSettingsSection.cs
- BamlRecordReader.cs
- StackOverflowException.cs
- ShaperBuffers.cs
- NotFiniteNumberException.cs
- EntityChangedParams.cs
- Style.cs
- PresentationAppDomainManager.cs
- ServicePointManager.cs
- RegexGroupCollection.cs
- RuntimeResourceSet.cs
- AssemblyCollection.cs
- GridViewRowCollection.cs
- DecoderFallbackWithFailureFlag.cs
- XmlFormatReaderGenerator.cs
- DataMemberConverter.cs
- ServiceOperationInfoTypeConverter.cs
- BaseWebProxyFinder.cs
- SystemFonts.cs
- LazyTextWriterCreator.cs
- SystemPens.cs
- ServiceDescriptionReflector.cs
- WSSecurityPolicy.cs
- RowsCopiedEventArgs.cs
- DateTimeOffsetStorage.cs
- DiscoveryRequestHandler.cs
- AppDomainUnloadedException.cs
- Size3D.cs
- SingleKeyFrameCollection.cs
- Pair.cs
- CodeParameterDeclarationExpression.cs
- DataViewSettingCollection.cs
- printdlgexmarshaler.cs
- MessageQueueKey.cs
- PackagingUtilities.cs
- SignatureResourcePool.cs
- ObjectStateEntry.cs
- DynamicValidatorEventArgs.cs
- WebSysDefaultValueAttribute.cs
- HighContrastHelper.cs
- WebControl.cs
- HeaderCollection.cs
- NetCodeGroup.cs
- ZipPackagePart.cs
- SafeViewOfFileHandle.cs
- HtmlInputText.cs
- ActivationServices.cs
- Triangle.cs
- XPathMultyIterator.cs
- ShapingWorkspace.cs
- InfoCardProofToken.cs
- SystemMulticastIPAddressInformation.cs
- RuleAction.cs
- PermissionToken.cs
- IisTraceWebEventProvider.cs
- BrowserCapabilitiesCodeGenerator.cs
- ParserStreamGeometryContext.cs
- ValidationError.cs
- QilInvokeEarlyBound.cs
- SoapMessage.cs
- XmlSchemaSimpleContent.cs
- ToolStripMenuItem.cs
- ProtectedConfigurationSection.cs
- MetadataWorkspace.cs
- COM2Properties.cs
- PropertyValueChangedEvent.cs
- XamlTreeBuilderBamlRecordWriter.cs
- UpdatePanelTriggerCollection.cs
- DrawingBrush.cs
- SamlNameIdentifierClaimResource.cs
- XPathDocument.cs
- SqlWriter.cs
- IncrementalReadDecoders.cs
- ListenerAdapterBase.cs
- BinaryObjectInfo.cs
- RouteParametersHelper.cs
- ScaleTransform.cs
- ViewGenerator.cs
- FunctionNode.cs
- login.cs
- QilName.cs
- DecimalStorage.cs
- ImageMapEventArgs.cs
- RankException.cs
- X509SubjectKeyIdentifierClause.cs
- XPathDescendantIterator.cs
- XmlSchemaSimpleType.cs
- Binding.cs
- ThrowHelper.cs
- CaseInsensitiveHashCodeProvider.cs
- AspCompat.cs