首页 > C# System.RuntimeType.TryChangeType 如何调用类转化方法

C# System.RuntimeType.TryChangeType 如何调用类转化方法

如下代码先运行一遍:

namespace openMV
{
    [Serializable]
    class A
    {
        public A()
        {
 
        }
        public int a = 1;
        public void func()
        {
            Console.WriteLine('A');
        }
        public B b = new B();
        //public C b = new C();
    }

    [Serializable]
    class B
    {
        public B()
        {
 
        }
        public int[] b = new int[5];
        //public int c = 4;
        public static implicit operator C(B current)
        {
            C c = new C();
            return c;
        }
        /*public static explicit operator C(B current)
        {
            C c = new C();
            return c;
        }*/
    }

    [Serializable]
    class C
    {
        public C()
        {
 
        }
        public int[,] c = new int[4, 4];
        public void func()
        {
            Console.WriteLine("CC");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            //FileStream fs = new FileStream("C:\\testing\\serialize.txt", FileMode.Open);
            FileStream fs = new FileStream("C:\\testing\\serialize.txt", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            A a = new A();
            //var o = (A)bf.Deserialize(fs);
            bf.Serialize(fs, a);
            Console.WriteLine("here");
        }
    }
}

序列化保存到文件之后,然后再运行下面的代码

namespace openMV
{
    [Serializable]
    class A
    {
        public A()
        {
 
        }
        public int a = 1;
        public void func()
        {
            Console.WriteLine('A');
        }
        //public B b = new B();
        public C b = new C();
    }

    [Serializable]
    class B
    {
        public B()
        {
 
        }
        public int[] b = new int[5];
        //public int c = 4;
        public static implicit operator C(B current)
        {
            C c = new C();
            return c;
        }
        /*public static explicit operator C(B current)
        {
            C c = new C();
            return c;
        }*/
    }

    [Serializable]
    class C
    {
        public C()
        {
 
        }
        public int[,] c = new int[4, 4];
        public void func()
        {
            Console.WriteLine("CC");
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("C:\\testing\\serialize.txt", FileMode.Open);
            //FileStream fs = new FileStream("C:\\testing\\serialize.txt", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            //A a = new A();
            var o = (A)bf.Deserialize(fs);
            //bf.Serialize(fs, a);
            Console.WriteLine("here");
        }
    }
}

这样运行之后会出现类型转化错误,在报错堆栈的最上一层是System.RuntimeType.TryChangeType,可是我明明已经在class B中定义了转化代码,为什么还会出现这样的问题? 各位大牛求解答如何才能实现在反序列化的时候可以自动转化类型?


首先你这个问题呢,属于不按照规则出牌,提点建议吧
1、如果你要在反序列化的时候换个类型,可以不用二进制序列化,比如xml、json;
2、你重载了operator,但实际上B和C还是两种类型,是可以转换,但前提是序列化的类库里实现了这个功能。


using (var ms = new MemoryStream())
{
    BinaryFormatter bf = new BinaryFormatter();

    A a = new A();

    bf.Serialize(ms, a);

    ms.Position = 0;

    var o = (A)bf.Deserialize(ms);

    System.Diagnostics.Debugger.Break();
}
【热门文章】
【热门文章】