类中有个地方,老出错,帮我看看
nbjed 2009-03-20 08:14:04 using System;
using System.Collections;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace Jedsoft.Common
{
///<Summary>
///Thread safe dictionary template
///</Summary>
[SerializableAttribute()]
// [DefaultMemberAttribute("Item")]
public class SafeDictionary<KeyType, ValueType> : Hashtable, ISerializable
{
#region Fields
///<Summary>
///If true, the dictionary allows null values
///</Summary>
protected bool mAllowNulls;
///<Summary>
///Default value
///</Summary>
protected ValueType mDefaultValue;
#endregion
#region Constructors
///<Summary>
///SafeDictionary constructor
///</Summary>
public SafeDictionary()
{
}
///<Summary>
///Deserialization constructor
///</Summary>
///<Param>
///Streaming context
///</Param>
///<Param>
///Serialization info
///</Param>
public SafeDictionary(SerializationInfo info, StreamingContext context)
: base(info, context)
{
}
#endregion
#region Properties
///<Summary>
///If true, the dictionary allows null values as valid
///</Summary>
public bool AllowNulls
{
get
{
return this.mAllowNulls;
}
set
{
this.mAllowNulls = value;
}
}
public ValueType this[KeyType key]
{
get
{
return (ValueType)base[key];
}
set
{
if ((value == null) && this.mAllowNulls)
{
value = ((ValueType)DBNull.Value);//这里出错:无法将类型“System.DBNull”转换为“ValueType”
}
base[key] = value;
}
}
#endregion
#region Methods
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
}
public bool TryGetValue(KeyType key, out ValueType value)
{
object object1 = base[key];
if ((object1 == DBNull.Value) && this.mAllowNulls)
{
value = default(ValueType);
return true;
}
else if (object1 != null)
{
value = ((ValueType)object1);
return true;
}
else
{
value = this.mDefaultValue;
return false;
}
}
#endregion
}
}
高手帮我解决一下