1

我有一个UserControl使用Guid存储在ViewState其中的偶尔Guid.Empty. 我也在GuidConverter我的系统中注册了这个:

public class GuidConverter : System.ComponentModel.GuidConverter {
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
        return typeof(IEnumerable<byte>).IsAssignableFrom(sourceType) || base.CanConvertFrom(context, sourceType);
    }
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
        return destinationType == typeof(byte[]) || base.CanConvertTo(context, destinationType);
    }
    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) {
        if (value is IEnumerable<byte>) {
            return new Guid(((IEnumerable<byte>)value).ToArray());
        }
        if (value is string) {
            if(string.IsNullOrEmpty((string)value)) {
                return Guid.Empty;
            }
            return new Guid((string)value);
        }
        return base.ConvertFrom(context, culture, value);
    }
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) {
        if (destinationType == typeof(byte[])) {
            return ((Guid)value).ToByteArray();
        }
        if(destinationType == typeof(string)) {
            var g = (Guid)value;
            if (g == Guid.Empty) {
                return null; //here is the cause of the problem
            }

            return g.ToString("N");
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

当那ViewState GuidGuid.Empty我得到这个例外:

System.ArgumentException: Error serializing value '00000000-0000-0000-0000-000000000000' of type 'System.Guid.' ---> System.ArgumentNullException: Value cannot be null.
Parameter name: value
   at System.IO.BinaryWriter.Write(String value)
   at System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value)
   --- End of inner exception stack trace ---
   at System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value)
   at System.Web.UI.ObjectStateFormatter.Serialize(Stream outputStream, Object stateGraph)
   at System.Web.UI.ObjectStateFormatter.Serialize(Object stateGraph)
   at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(Object state)
   at System.Web.UI.Util.SerializeWithAssert(IStateFormatter formatter, Object stateGraph)
   at System.Web.UI.HiddenFieldPageStatePersister.Save()
   at System.Web.UI.Page.SavePageStateToPersistenceMedium(Object state)
   at System.Web.UI.Page.SaveAllState()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   --- End of inner exception stack trace ---

null我可以通过在上面的注释行中替换为来消除错误string.Empty,但是使用该转换器的代码可能会以与该更改不兼容的方式区分这两种情况(例如,我可以将字符串形式存储在数据库和另一个应用程序可能会读取该值并根据字段是否为空执行不同的操作)。

为什么会这样?


换句话说:如果我最终提供了一个实现TypeConverter.ConvertToInvariantString,它是否意味着我不能返回 null 的任何地方?我是否在 System.Web.dll 中发现错误或此方法的文档中缺少某些内容?

4

0 回答 0