使用构造函数创建属性
Using constructor to create properties
我是 C# 的新手,但必须在工作项目中使用它,所以如果这是重复的、使用错误的词汇或我问的是一个简单的问题(很难研究一个问题),我深表歉意当你不明白问题应该是什么时)。
我有一个带有多个构造函数的 class。我希望 class 的属性基于我调用的构造函数。这是我现在的代码:
public class MyClass
{
public object Property1;
public object Property2;
public object Property3;
public MyClass(object newProperty1, object newProperty2, object newProperty3)
{
Property1 = newProperty1;
Property2 = newProperty2;
Property3 = newProperty3;
}
public MyClass(object newProperty1, object newProperty2)
{
Property1 = newProperty1;
Property2 = newProperty2;
}
}
当我调用第二个构造函数时,我得到一个空的 Property3 对象。我想要发生的是,当调用第二个构造函数时,MyClass 中根本没有包含 Property3 对象 属性。
这可能吗?在此先感谢您的帮助。
不,这不可能。
字段是 class 的编译时属性,而不是运行时属性。在运行时,您无法更改 class 存储的变量数量和类型(尽管您当然可以更改它们的值)。
你应该使用继承来完成你想要完成的事情。
public class MyClass
{
public object Property1;
public object Property2;
public MyClass(object newProperty1, object newProperty2)
{
Property1 = newProperty1;
Property2 = newProperty2;
}
}
public class MyClass2 : MyClass
{
public object Property3;
public MyClass2(object newProperty1, object newProperty2, object newProperty3)
:base(newProperty1, newProperty2)
{
Property3 = newProperty3;
}
}
您的声明等同于:
public class MyClass
{
public object Property1 = null ;
public object Property2 = null ;
public object Property3 = null ;
如您所见,Property3 默认为空。那么就不用处理 Property3.
备注:您可以像这样重构您的第二个构造函数:
public MyClass(object newProperty1, object newProperty2) : this(newProperty1,newProperty2,null) {}
可以按照你的要求去做,但是你失去了使用智能感知的能力(至少从 VS2013 开始):
public class MyClass : DynamicObject
{
private Dictionary<string, object> _dynamicMembers = new Dictionary<string, object>();
public MyClass(object newProperty1, object newProperty2, object newProperty3)
: this(newProperty1, newProperty2)
{
((dynamic)this).NewProperty3 = newProperty3;
}
public MyClass(object newProperty1, object newProperty2)
{
((dynamic)this).NewProperty1 = newProperty1;
((dynamic)this).NewProperty2 = newProperty2;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _dynamicMembers.Keys.ToArray();
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _dynamicMembers.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_dynamicMembers[binder.Name] = value;
return true;
}
}
然后像这样使用:
dynamic threePropertyClass = new MyClass(10, "something", 1.6f);
dynamic twoPropertyClass = new MyClass(10, "something");
然而这里有很多 boxing/unboxing 发生,我会尽可能避免它。 MyClass
对象确实没有您要引用的属性,它从 Dictionary<string, object>
加载它们,但它确实像您想要的那样工作,只包含您想要的属性。您还可以通过执行以下操作来添加新属性:
threePropertyClass.NewProperty = 15.2;
并且您可以在 TrySetMember
中添加额外的逻辑,以防止用户在您不希望这样做的情况下这样做。
我是 C# 的新手,但必须在工作项目中使用它,所以如果这是重复的、使用错误的词汇或我问的是一个简单的问题(很难研究一个问题),我深表歉意当你不明白问题应该是什么时)。
我有一个带有多个构造函数的 class。我希望 class 的属性基于我调用的构造函数。这是我现在的代码:
public class MyClass
{
public object Property1;
public object Property2;
public object Property3;
public MyClass(object newProperty1, object newProperty2, object newProperty3)
{
Property1 = newProperty1;
Property2 = newProperty2;
Property3 = newProperty3;
}
public MyClass(object newProperty1, object newProperty2)
{
Property1 = newProperty1;
Property2 = newProperty2;
}
}
当我调用第二个构造函数时,我得到一个空的 Property3 对象。我想要发生的是,当调用第二个构造函数时,MyClass 中根本没有包含 Property3 对象 属性。
这可能吗?在此先感谢您的帮助。
不,这不可能。
字段是 class 的编译时属性,而不是运行时属性。在运行时,您无法更改 class 存储的变量数量和类型(尽管您当然可以更改它们的值)。
你应该使用继承来完成你想要完成的事情。
public class MyClass
{
public object Property1;
public object Property2;
public MyClass(object newProperty1, object newProperty2)
{
Property1 = newProperty1;
Property2 = newProperty2;
}
}
public class MyClass2 : MyClass
{
public object Property3;
public MyClass2(object newProperty1, object newProperty2, object newProperty3)
:base(newProperty1, newProperty2)
{
Property3 = newProperty3;
}
}
您的声明等同于:
public class MyClass
{
public object Property1 = null ;
public object Property2 = null ;
public object Property3 = null ;
如您所见,Property3 默认为空。那么就不用处理 Property3.
备注:您可以像这样重构您的第二个构造函数:
public MyClass(object newProperty1, object newProperty2) : this(newProperty1,newProperty2,null) {}
可以按照你的要求去做,但是你失去了使用智能感知的能力(至少从 VS2013 开始):
public class MyClass : DynamicObject
{
private Dictionary<string, object> _dynamicMembers = new Dictionary<string, object>();
public MyClass(object newProperty1, object newProperty2, object newProperty3)
: this(newProperty1, newProperty2)
{
((dynamic)this).NewProperty3 = newProperty3;
}
public MyClass(object newProperty1, object newProperty2)
{
((dynamic)this).NewProperty1 = newProperty1;
((dynamic)this).NewProperty2 = newProperty2;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _dynamicMembers.Keys.ToArray();
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _dynamicMembers.TryGetValue(binder.Name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_dynamicMembers[binder.Name] = value;
return true;
}
}
然后像这样使用:
dynamic threePropertyClass = new MyClass(10, "something", 1.6f);
dynamic twoPropertyClass = new MyClass(10, "something");
然而这里有很多 boxing/unboxing 发生,我会尽可能避免它。 MyClass
对象确实没有您要引用的属性,它从 Dictionary<string, object>
加载它们,但它确实像您想要的那样工作,只包含您想要的属性。您还可以通过执行以下操作来添加新属性:
threePropertyClass.NewProperty = 15.2;
并且您可以在 TrySetMember
中添加额外的逻辑,以防止用户在您不希望这样做的情况下这样做。