Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...