如何将类中的对象属性设置为基于该对象类型的另一个类的新实例?
我有以下类(无法编译):
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重要的是要理解
value
变量的值是一个引用,而不是一个对象。该值通过语句直接复制到fldValue
中,所以是的,
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 intofldValue
by the statementSo yes,
fldValue
will refer to whatever objectvalue
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?