Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3664968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:42:20+00:00 2026-05-19T01:42:20+00:00

I asked this question awhile ago, but didn’t get a usuable answer. Basically, I

  • 0

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.

Here’s 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 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?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:42:20+00:00Added an answer on May 19, 2026 at 1:42 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I originally asked this question on RefactorMyCode , but got no responses there... Basically
I originally asked this question , but in finding an answer, discovered that my
UPDATE 10/19/2010 I know I asked this question a while ago, but the workarounds
Okay so I know I asked a similar question a while ago, but this
I've asked one question about this a month ago, it's here: "post" method to
I asked this question two months ago and got nary an answer. In fact
I know this question have been asked endless times but I can't find a
I asked this question a while ago and got an answer that I thought
I actually asked this question before, but I cannot get my account details back,
I asked this question a while back but now I'm looking to implement an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.