System.Configuration中的BUG - 使用自定义配置节时出现的异常
2009-12-24 15:20
260 查看
发现当自定义应用程序配置节中出现树型结构时将引发 “System.StackOverflowException” 类型异常.
程序集: System.Configuration, Version=2.0.0.0 例: 使用NodeElement(从ConfigurationElement派生)实现自定义配置节,
当获取其属性数据时出现"未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中。".
代码如下:
使用 .NET Reflector 查看 System.Configuration.ConfigurationElement 源代码后发现,
当创建 ConfigurationProperty时缺少对属性类型与其当前类型的对比判断, 导致无限递归.
代码如下:
当获取其属性数据时出现"未处理的“System.StackOverflowException”类型的异常出现在 mscorlib.dll 中。".
代码如下:
public class NodeElement : ConfigurationElement { [ConfigurationProperty("text", IsRequired = true)] public string Text { get { return (string)this["text"]; } } [ConfigurationProperty("child")] public NodeElement ChildElement { get { return (NodeElement)this["child"]; } } } static class Program { [STAThread] static void Main() { NodeElement node = new NodeElement(); object o = node.ChildElement; // 异常将出现在这里 } }
使用 .NET Reflector 查看 System.Configuration.ConfigurationElement 源代码后发现,
当创建 ConfigurationProperty时缺少对属性类型与其当前类型的对比判断, 导致无限递归.
代码如下:
private static ConfigurationProperty CreateConfigurationPropertyFromAttributes(PropertyInfo propertyInformation) { ConfigurationProperty property = null; if (Attribute.GetCustomAttribute(propertyInformation, typeof(ConfigurationPropertyAttribute)) is ConfigurationPropertyAttribute) { property = new ConfigurationProperty(propertyInformation); } //问题出现在下面这句判断 //此处缺少对 property.Type 与当前其所属类型的判断 //导致 PropertiesFromType 方法无限递归. if ((property != null) && typeof(ConfigurationElement).IsAssignableFrom(property.Type)) { ConfigurationPropertyCollection result = null; PropertiesFromType(property.Type, out result); } return property; }
相关文章推荐
- Guid做主键使用数据集时出现System.OutOfMemoryException: 引发类型为“System.OutOfMemoryException”的异常
- Android M原生BUG,手机使用过程中SystemServer空指针异常导致手机重启
- 使用过多的递归出现错误,“System.StackOverflowException”类型的未经处理的异常在 mscorlib.dll 中发生
- Hibernate 使用时出现 Could not parse configuration: /hibernate.cfg.xml 的异常问题
- C#2010 在使用 System.Data.SQLite.dll 时出现异常
- 关于i--与System.out.println()联合使用时可能出现的异常情况
- 某APK中使用了动态注册BroadcastReceiver,Launcher中动态加载此APK出现java.lang.SecurityException异常的解决方法
- 解决使用CHtmlView后出现的COleException异常
- There is no setter for property named 'categoryType ' in 'class com.cyg.data 解决mybatis 使用配置文件出现的bug
- StringReader使用过程中出现空指针异常
- 使用Excel时出现System.Data.OleDb.OleDbException: 找不到可安装的 ISAM错误。
- 使用WCF服务的客户端出现maxReceivedMessageSize异常解决方案
- 使用Hibernate集成shiro的时候,同时使用Ehcahce出现异常
- Android环境下使用net.sf.JSONObject问题出现BUG
- 使用BaseAdapter刷新列表出现下标越界异常
- 在 System.FormatException 中第一次偶然出现的“mscorlib.dll”类型的异常,如何调试呢。
- Gdiplus使用中出现的一个Bug记录
- 安装SQL2008的时候 出现System.Configuration.ConfigurationErrorsException: 创建 userSettings/Microsoft.SqlServe
- LINQ entityset 不包含GetEnumerator的定义,在MVC中使用entity framework(EF)出现“必须添加对程序集“System.Data.Entity”解决方法
- 使用 Anotation 注释出现Exception org.hibernate.MappingException: An AnnotationConfiguration。。。