我正在使用消息格式实现REST
服务。我有一个方法应该采用不同类型的参数(但从基本类派生)。而且我想知道在这种情况下将扩展类与转换运算符一起使用是否是一种好方法? JSON
WCF
示例(省略DataContract
等DataMember
):
class ClassOne
{
public string Name { get; set; }
}
class ClassTwo : ClassOne
{
public int A { get; set; }
}
class ClassThree : ClassOne
{
public int B { get; set; }
}
class ClassExtended
{
public string Name { get; set; }
public int A { get; set; }
public int B { get; set; }
public static implicit operator ClassTwo(ClassExtended extended)
{
return new ClassTwo()
{
Name = extended.Name,
A = extended.A
};
}
public static implicit operator ClassThree(ClassExtended extended)
{
return new ClassThree()
{
Name = extended.Name,
B = extended.B
};
}
}
void Test(ClassExtended extended)
{
// using as ClassThree here
}