1

HI,

I have code below but getting error "object does not match target type" on the prop.SetValue statement. But the types are both Int32.

    private UniqueProjectType CreateUniqueProjectType(TBR.Domain.Project project)
    {
        UniqueProjectType type = new UniqueProjectType();

        foreach (PropertyInfo prop in type.GetType().GetProperties())
        {
            if (prop.Name == "ID")
            {}
            else if (prop.Name == "PayFrequency")
                type.PayFrequency = _tbrService.GetEmployee((int)project.EmployeeID).PayFrequency;
            else
                prop.SetValue(type, prop.GetValue(project, null), null);

        }

        return type;
    }
3
  • It would be helpful to see the declaration of SetValue() and GetValue() methods together with it's types otherwise we can only assume what is going on. Commented Mar 17, 2011 at 11:51
  • @Raphael B.: they are built-in .NET types Commented Mar 17, 2011 at 11:56
  • Maybe Raphael B. meant it would be helpful to see what field this is crashing on - need to know the data type, etc. Commented Mar 17, 2011 at 11:59

2 Answers 2

2

I think here's the catch:

prop.GetValue(project, null);

prop is specific to UniqueProjectType while project is TBR.Domain.Project type. I think you should get all properties of TBR.Domain.Project and find one that has corresponding name.

Sign up to request clarification or add additional context in comments.

Comments

2

I think you should call GetValue on the PropertyInfo corresponding to the Project type. PropertyInfo instances are tied to a specific type.

Basically, for each property info of the UniqueProjectType type, you have to look for a PropertyInfo on the Project type with the same name. Then you call GetValue and SetValue for the two objects using their corresponding PropertyInfo.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.