如何从另一个对象初始化一个对象
本文关键字:一个对象 初始化 | 更新日期: 2023-09-27 18:25:46
我已经编程了 15 年,但我是 C# 的新手,对强类型语言缺乏经验,所以如果这是一个超级菜鸟问题,我深表歉意。
我试图避免大量重复的代码,并且很难找到一种方法来做我想做的事情。这是我目前拥有的示例:
class BaseModel {
string sharedProperty1;
string sharedProperty2;
string sharedProperty3;
string sharedProperty4;
string sharedProperty5;
string sharedProperty6;
}
class ExtendedModelA : BaseModel {
string exclusivePropertyA;
}
class ExtendedModelB : BaseModel {
string exclusivePropertyB;
}
// --------
dynamic finalModel;
if( conditionA )
{
finalModel = new ExtendedModelA{
sharedProperty1 = 1,
sharedProperty2 = 2,
sharedProperty3 = 3,
sharedProperty4 = 4,
sharedProperty5 = 5,
sharedProperty6 = 6,
exclusivePropertyA = "foo"
};
}
else
{
finalModel = new ExtendedModelB{
sharedProperty1 = 1,
sharedProperty2 = 2,
sharedProperty3 = 3,
sharedProperty4 = 4,
sharedProperty5 = 5,
sharedProperty6 = 6,
exclusivePropertyB = "bar"
};
}
如您所见,有很多冗余,因为初始化值的唯一区别是每个值的最后一exclusiveProperty
(在我的特定情况下,我在if/else
的每一半中都有大约 30 个属性被复制,只有 2 或 3 个唯一(。我想做的是使用所有sharedProperty
值初始化一次"模板"模型,并且在if/else
中只需要初始化exclusiveProperty
值。
这里有一些伪代码来说明我想做什么的概念,但我还没有能够让它工作。
var template = new BaseModel
{
sharedProperty1 = 1,
sharedProperty2 = 2,
sharedProperty3 = 3,
sharedProperty4 = 4,
sharedProperty5 = 5,
sharedProperty6 = 6
};
dynamic finalModel;
if( conditionA )
{
finalModel = new ExtendedModelA{template};
finalModel.exclusivePropertyA = "foo";
}
else
{
finalModel = new ExtendedModelB{template};
finalModel.exclusivePropertyB = "foo";
}
我熟悉可用于接受"模板"和"扩展"作为参数的工厂模式,但这与简单地保留我的重复项一样多的代码,所以我正在寻找一个更...直接?如何做到这一点。
我认为在不重复代码的情况下执行此操作的最简单方法是在 if else 块中创建具有唯一属性的对象,然后在最后分配所有共享对象。 这将防止您不得不重复代码。
BaseModel finalModel = null;
if (conditionA)
{
finalModel = new ExtendedModelA()
{
exclusivePropertyA = "some value";
};
}
else
{
finalModel = new ExtendedModelB()
{
exclusivePropertyB = "some other value";
};
}
finalModel.sharedProperty1 = "asdf";
// assign the rest of the values
您还可以为基本模型创建一个构造函数,该构造函数将基本模型作为参数,并将所有值分配给新对象。 这基本上是使用原始模板作为模板来创建新的。 然后,您可以使用模板创建扩展类并将其传递给基构造函数。
class BaseModel
{
public string sharedProperty1 { get; set; }
public string sharedProperty2 { get; set; }
public string sharedProperty3 { get; set; }
public string sharedProperty4 { get; set; }
public string sharedProperty5 { get; set; }
public string sharedProperty6 { get; set; }
public BaseModel(BaseModel t)
{
//assign template properties
}
public BaseModel()
{
}
}
class ExtendedModelA : BaseModel
{
public string exclusivePropertyA { get; set; }
public ExtendedModelA(BaseModel t)
: base(t)
{
}
}
class ExtendedModelB : BaseModel
{
public string exclusivePropertyB { get; set; }
public ExtendedModelB(BaseModel t)
: base(t)
{
}
}
然后使用它将是
BaseModel template = new BaseModel()
{
//assign values
};
ExtendedModelA = new ExtendedModelA(template)
{
exlusivePropertyA = "asdf";
};
你的第二次尝试几乎是正确的。您只需要在 ExtendedModelA
& ExtendedModelB
中添加一个 BaseModel
ctor:
class ExtendedModelA : BaseModel
{
public ExtendedModelA(BaseModel b)
: base(b)
{
}
}
然后这样称呼它:
BaseModel template = new BaseModel
{
// init code
};
finalModel = new ExtendedModelA(template); // note the parenthesis's not curly braces