0

I've got a class public class foubar : fou

fou has its properties and so does foubar I've created an instance of foubar and set values to all the properties including the base classes'. Now I need an instance of fou that has the property values that are in foubar. True, I could create a new instance of fou and write code that would populate this new instance from a foubar instance but that means I have to change to code every time the base class properties change.

I'd like something that would work like this, where FB is an instance of foubar

fou test = (fou) FB;

This "works" in C#, but test is really type foubar. I need only fou, nothing else,

I want a way to dynamically return an instance of a base class populated from the data/instance of the derived class.

Ideas?

bruno conde
  • 47,767
  • 15
  • 98
  • 117
mike
  • 21
  • 3
  • Why do you need it to *only* be the base class? – Rowland Shaw Oct 16 '09 at 13:58
  • Why do you need the real type to be fou ? Seems very "unobject" to me... – Cédric Rup Oct 16 '09 at 14:04
  • I've got a method that only takes type fou, won't take foubar. The base class fou and the method that takes fou are out of my control. – mike Oct 16 '09 at 14:05
  • Well, a method with a fou parameter will accept to recieve a foubar value... it's basic object – Cédric Rup Oct 16 '09 at 14:07
  • 2
    That method should take descendants of `fou`, including your `foubar` - it sounds like you're trying to solve the wrong problem... – Rowland Shaw Oct 16 '09 at 14:08
  • ...it might help if you expand your question to include the rationale (including errors when you do pass down your inherited object) – Rowland Shaw Oct 16 '09 at 14:13
  • fou is an entity framework class, it really represents a table foubar is my wrapper around it derived from fou when I try to do context.AddTofou(FB); it doesn't accept a foubar object, even with a cast; it wants fou. – mike Oct 16 '09 at 14:15
  • entity framwork error thrown: Message="Object mapping could not be found for Type with identity 'foubar'." Source="System.Data.Entity" – mike Oct 16 '09 at 14:17
  • 1
    Don't know much about EF ;o( How about using composition over inheritance ? – Cédric Rup Oct 16 '09 at 14:18
  • 1
    You should really put more context in your question, specially the fact that you're using EF – Cédric Rup Oct 16 '09 at 14:19
  • You are certainly trying to solve the wrong problem. If foubar inherits from fou *in your entity model* then you certainly can call context.AddTofou(FB), because inherited types and their parents have the same entity set. Since you say you can't, I suspect your mapping is not right. – Craig Stuntz Oct 16 '09 at 15:58

3 Answers3

2

I used reflection to dynamically make a copy of the base class' fields. Got this from: C# Using Reflection to copy base class properties Now I have a true fou type I can pass to EF.

private fou GetBaseData(foubar FB)
        {
            fou test_fou = new fou();
            Type type = FB.GetType();
            type = type.BaseType;
            UpdateForType(type, FB, test_fou);
            return test_fou;
        }

        private static void UpdateForType(Type type, foubar source, fou destination)
        {
            FieldInfo[] myObjectFields = type.GetFields(
                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            foreach (FieldInfo fi in myObjectFields)
            {
                fi.SetValue(destination, fi.GetValue(source));
            }
        }
Community
  • 1
  • 1
mike
  • 21
  • 3
0

I don't see why do you need such a thing? But you can do something like this:

class Foo 
{
    // ...

    public Foo ConcreteFoo()
    {
        if (this.GetType().Equals(typeof(Foo)))
            return this;
        else
        {
            Foo f = new Foo();
            // clone properties
            return f;
        }
    }
}

So you can do:

FouBar foobar = new FooBar();
Foo f = foobar.ConcreteFoo();

I'm really not comfortable with this ... Is there any special reason why you want a concrete Foo?

(edit)

Following the comments I can see that your problem is related to this question.

Community
  • 1
  • 1
bruno conde
  • 47,767
  • 15
  • 98
  • 117
0

You could use reflection and generics to map like-named properties to eachother, but I don't think you can change the base type of an object without cloning or instantiating a new one.

Antony Koch
  • 2,043
  • 1
  • 16
  • 23