Can I pass a parent object and rebuild it on the other side of a function?

家住魔仙堡 提交于 2019-12-14 03:24:26

问题


using these classes

public class Parent
{
   public int parentInt;
}

public class Child: Parent
{
   public string childString;

   public Child(string passedString, int passedInt)
   {
       childString = passedString
       parentInt = passedInt
   }     
}

I want to write a function that does stuff like this:

public static void DoSomething(Parent passedParent, Type passedChildType)
{
    ScreenRecorder.Start()
    //print all of parents properties
    //print all of the specific child's properties if possible

}

I want this function to be able to handle any type of child I create in the future, How can I make it so that I can dynamically rebuild the child's properties inside the function?

Here is an example of the method being called:

public static int dostuff(string s)
{
    int functionReturnValue = 0;

    switch (s)
    {
        case "":
            functionReturnValue = 0;
            break;
        case "7/8":
            functionReturnValue = 14;
            break;
        case "15/16":
            functionReturnValue = 15;
            break;
        default:
            Child ex = new Child("Blue", 1);
            Library.Handler.Process(ex, typeof(Child));
            break;
    }
    return functionReturnValue;
}

What can I pass in or do inside the Dosomething method to rebuild the child object attributes?


回答1:


To answer the question that is actually being asked. Yes you can "rebuild" an object on the other side.

See this code for an example:

public class Parent
{
    int thing = 1;
}


public class Child1 : Parent
{
    public string name;

    public Child1(string p)
    {
        this.name = p;
    }
}

public class Child2 : Parent
{
    public string name;

    public Child2(string p)
    {
        this.name = p;
    }
}

In this function we setup a Child1 object and pass it to another function.

    private void SetupStuff
    {
        Child1 x = new Child1("Bob");

        DoStuff(x);
    }

Here we can receive and "rebuild it"

    private void DoStuff(Parent obj)
    {
        Child1 rebuiltObject = ((Child1)obj);
        MessageBox.Show(rebuiltObject.name);
    }

The MessageBox will show "Bob"

Please note however that this will cause a runtime error:

    private void DoStuff(Parent obj)
    {
        Child2 rebuiltObject = ((Child2)obj);
        MessageBox.Show(rebuiltObject.name);
    }

It is smart enough to prevent you from rebuilding your object as something it never was.




回答2:


Method 1: Message Dictionaries

The best way to do this, I'd say, would be to replace both the Parent and Child classes with a simple dictionary, so you can include any number of properties as needed. This can be easily iterated over and include whatever information you need in your messages.


Method 2: Interfaces

Rather than making your Process method take a type as a parameter, I'd suggest making it generic - constrained to IParent. The call becomes:

Library.Handler.Process(ex);

Declared as:

public void Process<TMessageType>(T message) where TMessageType : IParent
{
  ScreenRecorder.Start();
  message.Print();
}

Your IParent interface would, as above, include a method called Print, which in your base parent class would print the ParentInt property you defined above, formatted as needed. Child would then look the same as it does now, with an additional override to Print.

This allows you to keep your current structure for the most part.


Method 3: Reflection

The third, worst way would be to use your current structure as is and reflect on the properties. Your DoSomething method will become a mess, and perhaps rather slow (maybe unimportant). This would require the least redesign, but at the cost of being rather terrible code.


Any of these approaches will do, but I really have to stress that the order they're presented in has a lot to do with their quality.



来源:https://stackoverflow.com/questions/22021203/can-i-pass-a-parent-object-and-rebuild-it-on-the-other-side-of-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!