如何在c#中复制对象?

问题描述

我必须在 asp.net 网页中使用 c# 做一个重复的按钮,这让我很伤心,因为我从 Microsoft 的网站 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-write-a-copy-constructor 上找到了一些文档,但不知何故仍然不知道如何使它在我的项目。我试图在我的模型项目中编写一个复制构造函数,其中我有我的类和属性

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        //Instance constructor.
        public Person()
        {
        }
        //copy constructor.
        public Person(Person prevIoUsPerson)
        {
            Name = prevIoUsPerson.Name;
            Age = prevIoUsPerson.Age;
        }
}

然后我有我的内容页面,我想在其中显示一个表单:

@using ClassLibrary1
@{

    if (IsPost)
    {
        Person person = new Person();


        if (Validation.IsValid())
        {
            switch (Request.Form["action"])
            {
                case "Submit":
                    person.Name = Request.Form["Name"];
                    person.Age = Request.Form["Age"].AsInt();
                    break;
                case "Duplicate":
                    Person person1 = new Person(person);
                    person1.Name = Request.Form["Name"];
                    person1.Age = Request.Form["Age"].AsInt();
                    break;
            }
        }
    }
}
<form method="post">
    <fieldset>
        <legend>Add Customer</legend>
        <div>
            <label for="Name">Name:</label>
            <input type="text" name="Name"
                   value="@Request.Form["Name"]" />
        </div>
        <div>
            <label for="Age"> Age:</label>
            <input type="text" name="Age"
                   value="@Request.Form["Age"]" />
        </div>
        <button name="action" value="Save" type="submit">Save</button>
        <button name="action" value="Duplicate" type="submit">Duplicate</button>
    </fieldset>
</form>

我想让用户完成 form,当他们按下保存按钮时,form 将被提交。 我的问题是当我想要复制表单并制作该 form 的深层副本时。 我错过了一些东西,我不知道是什么。任何的想法? 顺便一提。祝你有美好的一天。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)