使用序列时铸造的错误 [英] Casting Error when using serialization

查看:137
本文介绍了使用序列时铸造的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题前一段时间,但没有得到一个答案usuable。基本上,我不能让我的复制对象由于无效的转换异常的工作方法。但是,我可以投的东西复制方法的罚款之外。

I asked this question awhile ago, but didn't get a usuable answer. Basically, I can't get my method for duplicating objects to work due to invalid cast exceptions. But I can cast things fine outside of the duplication method.

这里的复制方法

public static class ObjectDuplicator
{
    public static T Clone<T>(T source)
    {
        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("the Type must be serializable.", "source");
        }

        if (Object.ReferenceEquals(source, null)) //dont try to serialize a null object
        {
            return default(T);
        }

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }
}



问题是这样的:当我打电话使用下面的代码

The problem is this: when I call this method using the code below

public void AddJob(Job job)
{
    if (!Jobs.Contains(job))
    {
        Job newcopy = Utilities.ObjectDuplicator.Clone<Job>(job);

        Jobs.Add(newcopy);
    }
}



它抛出这个异常:

it throws this exception:

System.InvalidCastException是
未处理消息=无法转换类型
'KH.CharacterClasses.Freelancer'的
对象
型'KH.CharacterClasses.Job

System.InvalidCastException was unhandled Message=Unable to cast object of type 'KH.CharacterClasses.Freelancer' to type 'KH.CharacterClasses.Job'

现在,工作我加入的类型是从作业继承的类(自由职业者)和这两个类的代码如下

Now, the type of job I'm adding is an inherited class from Job, (Freelancer) and the code for those two classes is below

[Serializable]
public class Job : Ability
{
    protected JobCommand basecommand1;
    protected JobCommand basecommand2;
    protected JobCommand basecommand3;
    protected JobCommand basecommand4;
    protected JobCommand command1;
    protected JobCommand command2;
    protected JobCommand command3;
    protected JobCommand command4;
    bool mastered;
    protected FFJob job;
    protected string name;
    int level;

    public FFJob SetJob
    {
        get
        {
            return job;
        }
    }

    public bool Mastered
    {
        get
        {
            return mastered;
        }
    }

    public JobCommand Command1
    {
        get
        {
            return command1;
        }
        set
        {
            command1 = value;
        }
    }

    public JobCommand DefaultCommand1
    {
        get
        {
            return basecommand1;
        }
    }

    public JobCommand Command2
    {
        get
        {
            return command2;
        }
        set
        {
            command2 = value;
        }
    }

    public JobCommand DefaultCommand2
    {
        get
        {
            return basecommand2;
        }
    }

    public JobCommand Command3
    {
        get
        {
            return command3;
        }
        set
        {
            command3 = value;
        }
    }

    public JobCommand DefaultCommand3
    {
        get
        {
            return basecommand3;
        }
    }

    public JobCommand Command4
    {
        get
        {
            return command4;
        }
        set
        {
            command4 = value;
        }
    }

    public JobCommand DefaultCommand4
    {
        get
        {
            return basecommand4;
        }
    }

    public Job(string name, string description, int jobID)
        : base(name, description, jobID, -1, -1, null, null, -1, -1)
    {
    }

    public static bool operator ==(Job job1, Job job2)
    {
        if (System.Object.ReferenceEquals(job1, job2))
            return true;
        if (((object)job1 == null) || ((object)job2 == null))
            return false;
        return (job1.Name == job2.Name && job1.UID == job2.UID);
    }

    public static bool operator !=(Job job1, Job job2)
    {
        return !(job1 == job2);
    }


    // public abstract void CharacterModifier(BaseCharacter character);

    // public abstract void CharacterDemodifier(BaseCharacter character);
}

[Serializable]
public class Freelancer : Job
{
    public Freelancer()
        : base("Freelancer", "A character not specializing in any class. Can combine the power of all mastered Jobs.", Globals.JobID.ID)
    {
        basecommand1 = JobCommand.Attack;
        basecommand2 = JobCommand.Free;
        basecommand3 = JobCommand.Free;
        basecommand4 = JobCommand.Items;
        command1 = basecommand1;
        command2 = basecommand2;
        command3 = basecommand3;
        command4 = basecommand4;
        job = FFJob.Freelancer;
    }
}



我真的不知道是什么问题。正如我所说,铸造工程这种方法的精细外,我知道这个代码工作之前。任何想法?

I really don't know what the issue is. As I said, casting works fine outside of this method, and I know this code has worked before. Any ideas?

感谢

推荐答案

我想通了。在某些时候,我编译它作为一个.dll在另一个项目中引用。我忘了删除bin目录中的.dll文件,所以程序被加载我的课从DLL,而不是从代码的新版本。我意识到,在我试图做同一类型objct的直重复,看见有人referecing从.dll和从.exe的东西。删除该.dll固定它。愚蠢的我。

I figured it out. At some point, I compiled it as a .dll to reference in another project. I forgot to delete the .dll from the bin directory, so the program was loading my classes from the dll, not from the new version of the code. I realized that after I tried to do a straight duplication of the same type of objct and saw it was referecing something from the .dll and from the .exe. Deleting the .dll fixed it. Silly me.

这篇关于使用序列时铸造的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆