Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ISerializable反序列化性能_C#_.net_Serialization_Iserializable - Fatal编程技术网

C# ISerializable反序列化性能

C# ISerializable反序列化性能,c#,.net,serialization,iserializable,C#,.net,Serialization,Iserializable,在实现ISerializable时,您可以编写这样的代码来执行自定义反序列化 (注意:这是一个简单的示例,不保证自定义反序列化) 需要将要反序列化的类型添加到GetValue方法中,根据intellisense帮助,该方法执行以下操作 如果无法将存储值转换为此类型,系统将引发system.InvalidCast异常 这是否意味着在我的示例语句中执行了两个强制转换 另外,添加此类型参数的意义是什么,因为如果我编写以下代码 _culture = info.GetValue("Culture", ty

在实现ISerializable时,您可以编写这样的代码来执行自定义反序列化

(注意:这是一个简单的示例,不保证自定义反序列化)

需要将要反序列化的类型添加到GetValue方法中,根据intellisense帮助,该方法执行以下操作

如果无法将存储值转换为此类型,系统将引发system.InvalidCast异常

这是否意味着在我的示例语句中执行了两个强制转换

另外,添加此类型参数的意义是什么,因为如果我编写以下代码

_culture = info.GetValue("Culture", typeof(string))
。。。这将不会编译,因为“无法将类型“object”隐式转换为“string”。这意味着我无论如何都必须强制转换对象,因此,如果强制转换无效,我将在任何情况下通过自己的强制转换获得InvalidCastException

这里似乎出现了两个类型转换,同样,在任何情况下,错误都只能在运行时发生(没有编译类型检查,这可以通过泛型实现),除非有人知道发生这种情况的原因


更新:可能是幕后使用“is”操作符检查类型是否符合预期?“是”是否自动尝试强制转换?

如果我们查看GetValue的实现,似乎没有强制转换对象本身,但至少发生了一次强制转换(从类型转换为RuntimeType)

就我所知,还有一些检查认为你的物品是否被铸造不那么重要

public object GetValue(string name, Type type)
{
    Type type3;
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    RuntimeType castType = type as RuntimeType;
    if (castType == null)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
    }
    object element = this.GetElement(name, out type3);
    if (RemotingServices.IsTransparentProxy(element))
    {
        if (RemotingServices.ProxyCheckCast(RemotingServices.GetRealProxy(element), castType))
        {
            return element;
        }
    }
    else if ((object.ReferenceEquals(type3, type) || type.IsAssignableFrom(type3)) || (element == null))
    {
        return element;
    }
    return this.m_converter.Convert(element, type);
}

在处理序列化时,强制转换不太可能成为瓶颈-序列化通常适用于涉及某些慢速介质(例如内存、网络或磁盘)的情况。是的,只是想知道强制转换是否比所需的多。
public object GetValue(string name, Type type)
{
    Type type3;
    if (type == null)
    {
        throw new ArgumentNullException("type");
    }
    RuntimeType castType = type as RuntimeType;
    if (castType == null)
    {
        throw new ArgumentException(Environment.GetResourceString("Argument_MustBeRuntimeType"));
    }
    object element = this.GetElement(name, out type3);
    if (RemotingServices.IsTransparentProxy(element))
    {
        if (RemotingServices.ProxyCheckCast(RemotingServices.GetRealProxy(element), castType))
        {
            return element;
        }
    }
    else if ((object.ReferenceEquals(type3, type) || type.IsAssignableFrom(type3)) || (element == null))
    {
        return element;
    }
    return this.m_converter.Convert(element, type);
}