如何将类中的对象属性设置为基于该对象类型的另一个类的新实例?

发布于 2024-10-20 03:52:58 字数 1688 浏览 2 评论 0原文

我有以下类(无法编译):

public class Field {
    public string fldName { get; set; }
    public object fldValue { get; set; }
    public Field() { }
    public Field(string name, object value) {
        fldName = name;
        Type type = Type.GetType(value);
        fldValue = Activator.CreateInstance(type);
        if (value == null) fldValue = ""; // forces object to be a string
    }
}

我试图通过传入一个字符串或我创建的另一个类来使其工作。如果我设置:

fldValue = value;

它仅通过引用复制对象,而不是实例化新类。我不确定这里是否需要通用的,但我宁愿不使用。我还不确定如果此代码确实有效,是否需要将参数对象深度复制到新的实例对象。

除了字符串之外,我想要复制到 fldValue 中的类的属性如下所示:

 public class DateReminder {
            public DateTime reminderDate {get; set;}
            public bool remindDate { get; set; }
            public int warningTime {get; set;}
            public bool remindWarn { get; set; }
    }

对于任何感兴趣的人,可以通过执行以下操作来解决:

 public class Field {

        public string fldName { get; set; }
        [XmlElement( typeof(string))]
        [XmlElement(typeof(DateReminder))]
        public object fldValue { get; set; }

        public Field() { }

        public Field(Field inField) {
            this.fldName = inField.fldName;
            if (inField.fldValue.GetType() == typeof(DateReminder)) {
                fldValue = (DateReminder)inField.fldValue;
            }
            else fldValue = inField.fldValue;
        }

        public Field(string name, object value) {
            if (value == null) {
                value = "";
            }
            fldName = name;
            fldValue = value;
        }
    }

并且 DateReminder 有自己的复制构造函数。

I have the following class (doesn't compile):

public class Field {
    public string fldName { get; set; }
    public object fldValue { get; set; }
    public Field() { }
    public Field(string name, object value) {
        fldName = name;
        Type type = Type.GetType(value);
        fldValue = Activator.CreateInstance(type);
        if (value == null) fldValue = ""; // forces object to be a string
    }
}

I'm trying to get this to work with passing in either a string or another class I created. If I set:

fldValue = value;

It only copies the object by reference instead of instantiating a new class. I'm not sure whether I need a generic here or not but would prefer to do without. I'm also unsure of whether or not I need to deep copy the argument object to the new instance object if this code did work.

The properties of the class I want to copy into fldValue besides a string looks like this:

 public class DateReminder {
            public DateTime reminderDate {get; set;}
            public bool remindDate { get; set; }
            public int warningTime {get; set;}
            public bool remindWarn { get; set; }
    }

For anyone interested, solved by doing the following:

 public class Field {

        public string fldName { get; set; }
        [XmlElement( typeof(string))]
        [XmlElement(typeof(DateReminder))]
        public object fldValue { get; set; }

        public Field() { }

        public Field(Field inField) {
            this.fldName = inField.fldName;
            if (inField.fldValue.GetType() == typeof(DateReminder)) {
                fldValue = (DateReminder)inField.fldValue;
            }
            else fldValue = inField.fldValue;
        }

        public Field(string name, object value) {
            if (value == null) {
                value = "";
            }
            fldName = name;
            fldValue = value;
        }
    }

and DateReminder has its own copy constructor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2024-10-27 03:52:58

重要的是要理解 value 变量的值是一个引用,而不是一个对象。该值通过语句直接复制到 fldValue

fldValue = value;

,所以是的,fldValue 将引用 value 所引用的任何对象。

我建议在调用代码中而不是在构造函数中执行复制...否则您将不知道是否需要复制,或者实际上如何创建一个副本。

最终你想要实现什么目标,什么对你不起作用?

It's important to understand that the value of the value variable is a reference, not an object. That value is copied directly into fldValue by the statement

fldValue = value;

So yes, fldValue will refer to whatever object value referred to.

I would suggest performing the copy in the calling code instead of in the constructor... otherwise you won't know whether you need to copy or not, or indeed how to create a copy.

What are you trying to achieve, ultimately, and what's not working for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文