[英]Generic method to return a child type in a parent method
我已经在C#中尝试了扩展方法几周,并且遇到了一些有趣的事情。 我已经尝试过为DTO构建泛型,如下所示:
public class ParentDto{
public string attrStr{get;set;}
}
public static class ParentDtoExtensions{
public static T AttrStr<T>(this T parentDto, string attrStr)
where T:ParentDto{
parentDto.attrStr = attrStr;
return parentDto;
}
}
然后在子类中:
public class ChildDto:ParentDto{
public string childAttrStr{get;set;}
}
public static class ChildDtoExtensions{
public static T ChildAttrStr<T>(this T childDto, string childAttrStr)
where T:ChildDto{
childDto.childAttrStr = childAttrStr;
return childDto;
}
}
然后让我像这样链接我的方法:
return ((new ChildDto()).AttrStr("someString").ChildAttrStr("someOtherString"));
这真的吸引了我。 能够使用monad-ish setter以及其他方法返回调用类型对于链接代码块非常方便。
但是,我希望能够将setter方法集成到我认为它们真正所属的父类中,同时保持上面显示的现有代码流,但是我不知道实现返回孩子的方法的方法。实现类的类。 就像是:
public class ParentDto{
public string attrStr{get;set;}
public T AttrStr<T>(string attrStr)
where T:ParentDto{
parentDto.attrStr = attrStr;
return parentDto;
}
}
但这对编译器(?)不起作用,因为它不知道调用类型。 有谁知道如何做到这一点?
请记住,我不是在寻求有关现有实现的代码味道的建议,因为我敢肯定有更多的C#
方式可以实现此目的。
您可以执行以下操作,但是IMO的扩展方法要好得多:
public class ParentDto<T> where T : ParentDto<T> {
public string attrStr{get;set;}
public T AttrStr(string attrStr) {
this.attrStr = attrStr;
return (T)this;
}
}
public sealed class ChildDto : ParentDto<ChildDto> {
public string childAttrStr{get;set;}
public ChildDto ChildAttrStr(string childAttrStr) {
this.childAttrStr = childAttrStr;
return this;
}
}
有关此模式的更多信息以及为什么应尽可能避免使用它,请参阅Eric Lippert的博客文章Curiouser and curiouser 。
就是说,我同意这是一种代码味道。 您应该只使用属性设置器,而不要使用流利的语法。 但是,由于您不在那里寻求建议,因此我将保留它。
如果您要做的只是在新对象上设置属性,则可以使用对象初始化程序来代替:
return new ChildDto()
{
attrStr = "someString",
childAttrString = "someOtherString"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.