調用unknowed的構造,我有這樣的情況: 我有類represeting性質:在編譯時類
public abstract class RootProperty {
public int Id {get; set;}
}
public class AProperty {}
public class BProperty {}
,並讓有這些的looot:
public class ZProperty {}
而且一每個屬性的值,
public abstract class RootValue {
public RootProperty Property {get;set;}
}
public class AValue : RootValue {
public AValue(AProperty) { ... }
public string Value {get; set;}
}
public class ZValue : RootValue {
public ZValue(ZProperty) { ... }
public sometipe Value {get;set;} }
實際上,每個類都代表一種類型財產比事物必須具有的。所以,如果我想說的是,一個屬性由一個字符串至極定義必須在一個給定的域值,我說:
public class DomainProperty {
public ICollection<string> Domain {get; set;}
}
public class DomainValue {
public DomainValue(DomainProperty) {...}
public string Value
{
set {
if (!this.Property.Domain.Any(d=>d==value)) {
throw new Exceptin("Value out of range!");
}
}
}
}
這樣我就可以狗可以大或小,而我的大。
DomainProperty DogsSize = new DomainProperty { Domain= ["big","small"] }
DomainValue MyDogSize = new DomainValue(color) { Value = "big" }
利用這一點,我可以用最大值和最小值定義的數值範圍屬性等等歐凱,這裏的第一個問題是:¿你能想到的coustomizable各類物業的這個問題的一些其他的解決辦法? ?
現在,讓我在這裏發表的questio: 前面的邏輯在服務器端,它從客戶端reciving抽象DTOS的列表:
public abstract class RootDto { public int PropertyId {get; set; } }
public class ADto : RootDto { public string Value {get; set;} }
pubic class BDto: RootDto { public bool Value {get;set;} }
public class ZDto : RootDto {public someothertype Value {get; set;}}
隨着RootDto屬性ID我可以找到原始的RootProperty。我想創建一個新的?Value實例,並將該值設置爲?Dto.Value。 現在我有這個在很多鑄件:
List<RootDto> listDto;
foreach(dto in listDtio) {
if (someDto is ADto) {
ADto castedDto = (ADto) dto;
AProperty dtoProperty = (AProperty) Repository.findRootProperty(dto.PropertyId);
AValue valueForDto = new AValue(dtoProperty);
valueForDto.Value = castedDto.Value;
}
if ...(B.....Z)
}
這樣的作品,但如果你有宋衍濤行代碼的戰利品說,15更多鈔票財產類。 我已經調查過反射和動態類型,但沒有發現任何東西,你能幫我嗎? 感謝您的時間,我希望我給了一個很好的解釋
優秀,我看到這兩個大的區別是:1屬性創造價值。 2-由於所有的Value類都具有相同的Value屬性,因此將其設置爲根的抽象屬性。感謝您的時間,我學到了新的東西:D –