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
- URLAttribute.cs
- UnsafeNativeMethods.cs
- XPathNodeInfoAtom.cs
- XD.cs
- RectIndependentAnimationStorage.cs
- Cursors.cs
- Helpers.cs
- XmlSchemaObject.cs
- InputProcessorProfilesLoader.cs
- XmlNamedNodeMap.cs
- UrlAuthFailedErrorFormatter.cs
- SqlFlattener.cs
- GenericUriParser.cs
- StylusDevice.cs
- EntryIndex.cs
- FtpWebRequest.cs
- MaterialCollection.cs
- ReferenceEqualityComparer.cs
- AccessViolationException.cs
- DbParameterHelper.cs
- ConditionCollection.cs
- NamespaceMapping.cs
- propertyentry.cs
- CurrentChangingEventManager.cs
- DebugView.cs
- CommandEventArgs.cs
- FileDataSource.cs
- ValidationEventArgs.cs
- SynchronizationContext.cs
- dataobject.cs
- MetadataPropertyCollection.cs
- XmlSchemaAny.cs
- CoTaskMemSafeHandle.cs
- HandleRef.cs
- AstNode.cs
- SiteOfOriginPart.cs
- PasswordTextContainer.cs
- DefaultValueConverter.cs
- TeredoHelper.cs
- iisPickupDirectory.cs
- EditingCoordinator.cs
- WhileDesigner.cs
- RoleGroupCollection.cs
- RSAPKCS1SignatureDeformatter.cs
- DbTransaction.cs
- DbExpressionBuilder.cs
- DependencyPropertyKey.cs
- MessagePartProtectionMode.cs
- TraceSection.cs
- ProxyHwnd.cs
- MembershipSection.cs
- DataListCommandEventArgs.cs
- SendSecurityHeader.cs
- XmlSchemaAll.cs
- FixedSchema.cs
- DispatcherTimer.cs
- CommandManager.cs
- UrlMappingCollection.cs
- CqlGenerator.cs
- TextServicesCompartmentEventSink.cs
- AuthenticationException.cs
- CategoryNameCollection.cs
- ContainerAction.cs
- unitconverter.cs
- QuaternionAnimationUsingKeyFrames.cs
- SymbolType.cs
- MsmqIntegrationOutputChannel.cs
- Cursor.cs
- InputScope.cs
- DetailsViewPagerRow.cs
- TypeBrowser.xaml.cs
- DragDeltaEventArgs.cs
- LineServicesRun.cs
- NullableDecimalAverageAggregationOperator.cs
- TypeReference.cs
- CheckPair.cs
- ResourceAttributes.cs
- LoginUtil.cs
- LifetimeServices.cs
- FrameworkTemplate.cs
- TraceHandlerErrorFormatter.cs
- XmlSchemaSimpleType.cs
- RectangleConverter.cs
- ServiceHttpModule.cs
- AnnotationObservableCollection.cs
- ControlPaint.cs
- DesignerDataRelationship.cs
- ObjectDataSourceSelectingEventArgs.cs
- ReferencedType.cs
- xmlsaver.cs
- Calendar.cs
- FileRecordSequence.cs
- CqlParser.cs
- GZipObjectSerializer.cs
- WebPartVerb.cs
- IInstanceTable.cs
- TemplatePropertyEntry.cs
- DataGridViewControlCollection.cs
- _ReceiveMessageOverlappedAsyncResult.cs
- ServiceChannelProxy.cs