本文整理汇总了C#中System.Configuration.ConfigurationElement.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# ConfigurationElement.GetType方法的具体用法?C# ConfigurationElement.GetType怎么用?C# ConfigurationElement.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Configuration.ConfigurationElement
的用法示例。
在下文中一共展示了ConfigurationElement.GetType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadConfigurationElement
private static IEnumerable<Argument> ReadConfigurationElement(ConfigurationElement element)
{
var arguments = new List<Argument>();
var collection = element as ConfigurationElementCollection;
if (collection != null)
{
foreach (ConfigurationElement child in collection)
{
arguments.AddRange(ReadConfigurationElement(child));
}
}
else
{
foreach (PropertyInfo property in element.GetType().GetProperties())
{
object propertyValue = property.GetValue(element, null);
if (typeof (ConfigurationElement).IsAssignableFrom(property.PropertyType))
{
arguments.AddRange(ReadConfigurationElement((ConfigurationElement) propertyValue));
}
var argument = new Argument(property, propertyValue);
if (argument.Include())
{
arguments.Add(argument);
}
}
}
return arguments;
}
示例2: AutoConfigurationHelper
internal AutoConfigurationHelper(ConfigurationElement element, Action<ConfigurationProperty, object> valueSetter, Func<ConfigurationProperty, object> valueGetter)
{
_ConfigElement = element;
_ValueSetter = valueSetter;
_ValueGetter = valueGetter;
var type = element.GetType();
Dictionary<ConfigurationProperty, PropertyInfo> properties;
if (!_TypeProperties.TryGetValue(type, out properties))
{
properties = new Dictionary<ConfigurationProperty, PropertyInfo>();
foreach (var member in type.GetProperties())
{
var configField = member.GetCustomAttributes(typeof(ConfigurationPropertyAttribute), true).Cast<ConfigurationPropertyAttribute>().FirstOrDefault();
if (configField != null)
{
var property = new ConfigurationProperty(configField.Name, member.PropertyType, configField.DefaultValue, ConfigurationPropertyOptions.None);
properties[property] = member;
}
}
_TypeProperties.TryAdd(type, properties);
}
_Properties = properties;
// Pre-initialize properties of type ConfigurationElement, or things go boom
foreach (var property in _Properties)
{
if (typeof(ConfigurationElement).IsAssignableFrom(property.Value.PropertyType))
property.Value.SetValue(_ConfigElement, _ValueGetter(property.Key), null);
}
}
示例3: Register
public void Register(ConfigurationElement element, Type @interface)
{
var type = element.GetType();
if (ClassAlreadyRegistered(type))
return;
Register(element, new ConfigurationPropertyCollection(@interface, type).ToArray());
}
示例4: Register
protected override void Register(ConfigurationElement element, params ConfigurationProperty[] configurationProperties)
{
var ownerType = element.GetType();
var properties = (SC.ConfigurationPropertyCollection)PropertyBagAccessor[ownerType];
if (properties == null)
{
properties = new SC.ConfigurationPropertyCollection();
PropertyBagAccessor[ownerType] = properties;
}
configurationProperties.ToList().ForEach(x => properties.Add(x));
}
示例5: Register
protected override void Register(ConfigurationElement element, params ConfigurationProperty[] configurationProperties)
{
lock (ElementMaps)
{
var ownerType = element.GetType();
var map = ElementMaps[ownerType];
if (map == null)
{
map = ElementMaps[ownerType] = ElementMapCreator(ownerType);
}
var properties = ElementMapPropertiesAccesor.GetValue(map) as SC.ConfigurationPropertyCollection;
if (properties == null)
{
properties = new SC.ConfigurationPropertyCollection();
}
ElementMapPropertiesAccesor.SetValue(map, properties);
configurationProperties.ToList().ForEach(x => properties.Add(x));
var einfo = ElementInformationConstructor(element);
ElementInformationAccessor.SetValue(element, einfo);
}
}
示例6: GetElementKey
/// <summary>
/// Gets the element key for a mimeType element that uniquely identifies it.
/// </summary>
/// <param name="element">The <see cref="ConfigurationElement" /> to return the key for. </param>
/// <returns>An Object that acts as the key for the specified <see cref="ConfigurationElement" />.</returns>
/// <exception cref="System.ArgumentException">Thrown when the element parameter is not of type
/// <see cref="MimeType" />.</exception>
protected override object GetElementKey(ConfigurationElement element)
{
MimeType mimeType = element as MimeType;
if (mimeType == null)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.Global_GetElementKey_Ex_Msg, typeof(MimeType).ToString(), element.GetType().ToString()));
}
return String.Concat(mimeType.FileExtension, "|", mimeType.BrowserId);
}
示例7: GetElementKey
/// <summary>
/// Retrieves a key of a given element.
/// </summary>
/// <param name="element">Element whose key to retrieve.</param>
/// <returns>A key of a given element.</returns>
protected override object GetElementKey(ConfigurationElement element)
{
object ret = null;
if (element != null && Type.Equals(element.GetType(), typeof(CultureConfiguration)))
ret = ((CultureConfiguration)element).CultureName;
return ret;
}
示例8: GetTagForExtensionElement
public string GetTagForExtensionElement(ConfigurationElement element)
{
Type elementType = element.GetType();
Dictionary<string, Type> dictToSearch = GetDictToSearch(elementType);
foreach(var keyValue in dictToSearch)
{
if (keyValue.Value == elementType)
{
return keyValue.Key;
}
}
throw ElementTypeNotFound(elementType);
}
示例9: SetIsPresent
internal static void SetIsPresent(ConfigurationElement element)
{
// Work around for VSW 578830: ConfigurationElements that override DeserializeElement cannot set ElementInformation.IsPresent
PropertyInfo elementPresent = element.GetType().GetProperty("ElementPresent", BindingFlags.Instance | BindingFlags.NonPublic);
SetIsPresentWithAssert(elementPresent, element, true);
}
示例10: ValidateElement
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive)
{
ConfigurationValidatorBase validator = propValidator;
if ((validator == null) && (elem.ElementProperty != null))
{
validator = elem.ElementProperty.Validator;
if ((validator != null) && !validator.CanValidate(elem.GetType()))
{
throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_does_not_support_elem_type", new object[] { elem.GetType().Name }));
}
}
try
{
if (validator != null)
{
validator.Validate(elem);
}
}
catch (ConfigurationException)
{
throw;
}
catch (Exception exception)
{
throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Validator_element_not_valid", new object[] { elem._elementTagName, exception.Message }));
}
if (recursive)
{
if ((elem is ConfigurationElementCollection) && (elem is ConfigurationElementCollection))
{
IEnumerator elementsEnumerator = ((ConfigurationElementCollection) elem).GetElementsEnumerator();
while (elementsEnumerator.MoveNext())
{
ValidateElement((ConfigurationElement) elementsEnumerator.Current, null, true);
}
}
for (int i = 0; i < elem.Values.Count; i++)
{
ConfigurationElement element = elem.Values[i] as ConfigurationElement;
if (element != null)
{
ValidateElement(element, null, true);
}
}
}
}
示例11: FillConfiguration
private static void FillConfiguration(ConfigurationElement sectionElement, XElement rootElement)
{
var sectionType = sectionElement.GetType();
foreach (var property in sectionType.GetProperties())
{
var configPropertyAttribute = property.GetCustomAttribute<ConfigurationPropertyAttribute>();
if (configPropertyAttribute == null)
{
continue;
}
var propertyElement = rootElement.Element(configPropertyAttribute.Name);
if (typeof(ConfigurationElementCollection).IsAssignableFrom(property.PropertyType))
{
var addMethod = property.PropertyType.GetMethod("Add", new Type[] { typeof(ConfigurationElement) });
if (addMethod == null)
{
return;
}
var configurationCollectionAttribute = property.GetCustomAttribute<ConfigurationCollectionAttribute>();
var elementCollection = (ConfigurationElementCollection)property.GetValue(sectionElement);
var subElementName = string.IsNullOrEmpty(configurationCollectionAttribute.AddItemName) ? "add" : configurationCollectionAttribute.AddItemName;
foreach (var subElement in propertyElement.Elements(subElementName))
{
var elementInstance = (ConfigurationElement)configurationCollectionAttribute.ItemType.GetConstructor(new Type[] { }).Invoke(new object[] { });
FillConfiguration(elementInstance, subElement);
addMethod.Invoke(elementCollection, new object[] { elementInstance });
}
}
else if (typeof(ConfigurationElement).IsAssignableFrom(property.PropertyType))
{
var elementInstance = (ConfigurationElement)property.GetValue(sectionElement);
FillConfiguration(elementInstance, propertyElement);
}
else
{
object value = null;
if (propertyElement != null)
{
value = propertyElement.Value;
}
else
{
var attribute = rootElement.Attribute(configPropertyAttribute.Name);
if (attribute != null)
{
value = attribute.Value;
}
else
{
continue;
}
}
if (value != null)
{
var stringValue = value.ToString();
if (property.PropertyType.IsEnum)
{
property.SetValue(sectionElement, Enum.Parse(property.PropertyType, stringValue));
}
else if (property.PropertyType == typeof(int))
{
property.SetValue(sectionElement, int.Parse(stringValue));
}
else
{
property.SetValue(sectionElement, stringValue);
}
}
}
}
}
示例12: ValidateElement
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator, bool recursive) {
// Validate a config element with the per-type validator when a per-property ( propValidator ) is not supplied
// or with the per-prop validator when the element ( elem ) is a child property of another configuration element
ConfigurationValidatorBase validator = propValidator;
if ((validator == null) && // Not a property - use the per-type validator
(elem.ElementProperty != null)) {
validator = elem.ElementProperty.Validator;
// Since ElementProperty can be overriden by derived classes we need to make sure that
// the validator supports the type of elem every time
if ((validator != null) && !validator.CanValidate(elem.GetType())) {
throw new ConfigurationErrorsException(SR.GetString(SR.Validator_does_not_support_elem_type, elem.GetType().Name));
}
}
try {
if (validator != null) {
validator.Validate(elem);
}
}
catch (ConfigurationException) {
// ConfigurationElement validators are allowed to throw ConfigurationErrorsException.
throw;
}
catch (Exception ex) {
throw new ConfigurationErrorsException(SR.GetString(SR.Validator_element_not_valid, elem._elementTagName, ex.Message));
}
catch {
throw new ConfigurationErrorsException(SR.GetString(SR.Validator_element_not_valid, elem._elementTagName, ExceptionUtil.NoExceptionInformation));
}
if (recursive == true) {
if (elem is ConfigurationElementCollection) {
if (elem is ConfigurationElementCollection) {
IEnumerator it = ((ConfigurationElementCollection)elem).GetElementsEnumerator();
while( it.MoveNext() ) {
ValidateElement((ConfigurationElement)it.Current, null, true);
}
}
}
// Validate all child elements recursively
for (int index = 0; index < elem.Values.Count; index++) {
ConfigurationElement value = elem.Values[index] as ConfigurationElement;
if (value != null) {
// Run the per-type validator on the child element and proceed with validation in subelements
// Note we dont run the per-property validator here since we run those when the property value is set
ValidateElement(value, null, true); // per-type
}
}
}
}
示例13: ElementIsModified
bool ElementIsModified (ConfigurationElement element)
{
return (bool) element.GetType ().GetMethod ("IsModified", BindingFlags.NonPublic | BindingFlags.Instance).Invoke (element, new object [0]);
}
示例14: ValidateElement
internal static void ValidateElement(ConfigurationElement elem, ConfigurationValidatorBase propValidator,
bool recursive)
{
// Validate a config element with the per-type validator when a per-property ( propValidator ) is not supplied
// or with the per-prop validator when the element ( elem ) is a child property of another configuration element
ConfigurationValidatorBase validator = propValidator;
if ((validator == null) && // Not a property - use the per-type validator
(elem.ElementProperty != null))
{
validator = elem.ElementProperty.Validator;
// Since ElementProperty can be overriden by derived classes we need to make sure that
// the validator supports the type of elem every time
if ((validator != null) && !validator.CanValidate(elem.GetType()))
{
throw new ConfigurationErrorsException(string.Format(SR.Validator_does_not_support_elem_type,
elem.GetType().Name));
}
}
try
{
validator?.Validate(elem);
}
catch (ConfigurationException)
{
// ConfigurationElement validators are allowed to throw ConfigurationErrorsException.
throw;
}
catch (Exception ex)
{
throw new ConfigurationErrorsException(string.Format(SR.Validator_element_not_valid, elem.ElementTagName,
ex.Message));
}
if (recursive)
{
// Validate collection items:
// Note: this is a bit of a hack - we will exploit the fact that this method is called with recursive == true only when serializing the top level section
// At deserializtion time the per-element validator for collection items will get executed as part of their deserialization logic
// However we dont perform validation in the serialization logic ( because at that time the object is unmerged and not all data is present )
// so we have to do that validation here.
if (elem is ConfigurationElementCollection)
{
IEnumerator it = ((ConfigurationElementCollection)elem).GetElementsEnumerator();
while (it.MoveNext()) ValidateElement((ConfigurationElement)it.Current, null, true);
}
// Validate all child elements recursively
for (int index = 0; index < elem.Values.Count; index++)
{
ConfigurationElement value = elem.Values[index] as ConfigurationElement;
if (value != null)
{
// Run the per-type validator on the child element and proceed with validation in subelements
// Note we dont run the per-property validator here since we run those when the property value is set
ValidateElement(value, null, true); // per-type
}
}
}
}
示例15: MergeBasedOnConfig
private void MergeBasedOnConfig(XElement mergedElement, XElement partElement, ConfigurationElement configElement)
{
var defaultCollectionProperty = configElement.GetType()
.GetProperties()
.FirstOrDefault(p =>
{
var attr =
(ConfigurationPropertyAttribute)
Attribute.GetCustomAttribute(p, typeof (ConfigurationPropertyAttribute));
return attr!=null && attr.IsDefaultCollection;
});
if (defaultCollectionProperty != null)
{
var configCollectionAttribute = (ConfigurationCollectionAttribute)Attribute.GetCustomAttribute(defaultCollectionProperty.PropertyType,
typeof (ConfigurationCollectionAttribute));
if (configCollectionAttribute != null)
{
var keyProperty = configCollectionAttribute.ItemType.GetProperties()
.Select(p => new
{
Property = p,
Attribute = (ConfigurationPropertyAttribute)
Attribute.GetCustomAttribute(p,
typeof (
ConfigurationPropertyAttribute
))
})
.FirstOrDefault(p => p.Attribute != null && p.Attribute.IsKey);
if (keyProperty != null)
{
MergeAttributes(mergedElement, partElement);
var mergedElements = mergedElement.Nodes().OfType<XElement>().ToList();
foreach (var partChildElement in partElement.Nodes().OfType<XElement>())
{
var key = partChildElement.Attributes()
.FirstOrDefault(
a =>
a.Name.LocalName.Equals(keyProperty.Attribute.Name,
StringComparison.OrdinalIgnoreCase));
if (key == null)
continue;
var mergedChildElement = mergedElements
.FirstOrDefault(e =>
{
var keyAttribute = e.Attribute(key.Name);
return keyAttribute != null && key.Value.Equals(keyAttribute.Value);
});
if (mergedChildElement != null)
mergedElements.Remove(mergedChildElement);
mergedElements.Add(partChildElement);
}
mergedElement.ReplaceNodes(mergedElements);
return;
}
}
}
// TODO: actually, we should dig into each ConfigurationElement, but we'll skip it for now
MergeAsRawXml(mergedElement, partElement);
}