当前位置: 首页>>代码示例>>C#>>正文


C# ConfigurationElement.HasValue方法代码示例

本文整理汇总了C#中System.Configuration.ConfigurationElement.HasValue方法的典型用法代码示例。如果您正苦于以下问题:C# ConfigurationElement.HasValue方法的具体用法?C# ConfigurationElement.HasValue怎么用?C# ConfigurationElement.HasValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Configuration.ConfigurationElement的用法示例。


在下文中一共展示了ConfigurationElement.HasValue方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HasValue

		/*
		 * FIXME: LAMESPEC
		 * 
		 * SerializeElement() and SerializeToXmlElement() need to emit different output
		 * based on the ConfigurationSaveMode that's being used.  Unfortunately, neither
		 * of these methods take it as an argument and there seems to be no documented way
		 * how to get it.
		 * 
		 * The parent element is needed because the element could be set to a different
		 * than the default value in a parent configuration file, then set locally to that
		 * same value.  This makes the element appear locally modified (so it's included
		 * with ConfigurationSaveMode.Modified), but it should not be emitted with
		 * ConfigurationSaveMode.Minimal.
		 * 
		 * In theory, we could save it into some private field in Unmerge(), but the
		 * problem is that Unmerge() is kinda expensive and we also need a way of
		 * determining whether or not the configuration has changed in Configuration.Save(),
		 * prior to opening the output file for writing.
		 * 
		 * There are two places from where HasValues() is called:
		 * a) From Configuration.Save() / SaveAs() to check whether the configuration needs
		 *    to be saved.  This check is done prior to opening the file for writing.
		 * b) From SerializeToXmlElement() to check whether to emit the element, using the
		 *    parent and mode values from the cached 'SaveContext'.
		 * 
		 */

		/*
		 * Check whether property 'prop' should be included in the serialized XML
		 * based on the current ConfigurationSaveMode.
		 */
		internal bool HasValue (ConfigurationElement parent, PropertyInformation prop,
		                        ConfigurationSaveMode mode)
		{
			if (prop.ValueOrigin == PropertyValueOrigin.Default)
				return false;
			
			if (mode == ConfigurationSaveMode.Modified &&
			    prop.ValueOrigin == PropertyValueOrigin.SetHere && prop.IsModified) {
				// Value has been modified locally, so we always emit it
				// with ConfigurationSaveMode.Modified.
				return true;
			}

			/*
			 * Ok, now we have to check whether we're different from the inherited
			 * value - which could either be a value that's set in a parent
			 * configuration file or the default value.
			 */
			
			var hasParentValue = parent != null && parent.HasValue (prop.Name);
			var parentOrDefault = hasParentValue ? parent [prop.Name] : prop.DefaultValue;

			if (!prop.IsElement)
				return !object.Equals (prop.Value, parentOrDefault);

			/*
			 * Ok, it's an element that has been set in a parent configuration file.			 * 
			 * Recursively call HasValues() to check whether it's been locally modified.
			 */
			var element = (ConfigurationElement) prop.Value;
			var parentElement = (ConfigurationElement) parentOrDefault;
			
			return element.HasValues (parentElement, mode);
		}
开发者ID:nlhepler,项目名称:mono,代码行数:65,代码来源:ConfigurationElement.cs

示例2: PrepareSave

		/*
		 * Cache the current 'parent' and 'mode' values for later use in SerializeToXmlElement()
		 * and SerializeElement().
		 * 
		 * Make sure to call base when overriding this in a derived class.
		 */
		internal virtual void PrepareSave (ConfigurationElement parent, ConfigurationSaveMode mode)
		{
			saveContext = new SaveContext (this, parent, mode);

			foreach (PropertyInformation prop in ElementInformation.Properties)
			{
				if (!prop.IsElement)
					continue;

				var elem = (ConfigurationElement)prop.Value;
				if (parent == null || !parent.HasValue (prop.Name))
					elem.PrepareSave (null, mode);
				else {
					var parentValue = (ConfigurationElement)parent [prop.Name];
					elem.PrepareSave (parentValue, mode);
				}
			}
		}
开发者ID:nlhepler,项目名称:mono,代码行数:24,代码来源:ConfigurationElement.cs

示例3: Unmerge

		protected internal virtual void Unmerge (
				ConfigurationElement source, ConfigurationElement parent,
				ConfigurationSaveMode updateMode)
		{
			if (parent != null && source.GetType() != parent.GetType())
				throw new ConfigurationErrorsException ("Can't unmerge two elements of different type");

			bool isMinimalOrModified = updateMode == ConfigurationSaveMode.Minimal ||
				updateMode == ConfigurationSaveMode.Modified;

			foreach (PropertyInformation prop in source.ElementInformation.Properties)
			{
				if (prop.ValueOrigin == PropertyValueOrigin.Default)
					continue;
				
				PropertyInformation unmergedProp = ElementInformation.Properties [prop.Name];
				
				object sourceValue = prop.Value;
				if (parent == null || !parent.HasValue (prop.Name)) {
					unmergedProp.Value = sourceValue;
					continue;
				}

				if (sourceValue == null)
					continue;

				object parentValue = parent [prop.Name];
				if (!prop.IsElement) {
					if (!object.Equals (sourceValue, parentValue) || 
					    (updateMode == ConfigurationSaveMode.Full) ||
					    (updateMode == ConfigurationSaveMode.Modified && prop.ValueOrigin == PropertyValueOrigin.SetHere))
						unmergedProp.Value = sourceValue;
					continue;
				}

				var sourceElement = (ConfigurationElement) sourceValue;
				if (isMinimalOrModified && !sourceElement.IsModified ())
					continue;
				if (parentValue == null) {
					unmergedProp.Value = sourceValue;
					continue;
				}

				var parentElement = (ConfigurationElement) parentValue;
				ConfigurationElement copy = (ConfigurationElement) unmergedProp.Value;
				copy.Unmerge (sourceElement, parentElement, updateMode);
			}
		}
开发者ID:nlhepler,项目名称:mono,代码行数:48,代码来源:ConfigurationElement.cs


注:本文中的System.Configuration.ConfigurationElement.HasValue方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。