Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在生成值对象,我想将我的方法从模板切换到CodeDom方法。
我希望我的类型实现一两个自引用通用接口(即IEquatable<MyValueObject>and IComparable<MyValueObject>)。
IEquatable<MyValueObject>
IComparable<MyValueObject>
我已经能够通过执行字符串操作并调用等效的 来获得所需的结果CodeTypeDeclaration.Members.Add("IEquatable<MyValueObject"),但如果可能的话,我宁愿使用对象模型。有没有更好的方法或者字符串将是我最好的选择?
CodeTypeDeclaration.Members.Add("IEquatable<MyValueObject")
您可以执行以下操作:
var type = new CodeTypeDeclaration("MyValueObject"); var iequatable = new CodeTypeReference( "IEquatable", new CodeTypeReference(type.Name)); type.BaseTypes.Add(iequatable);