我正在使用 强类型 DataSet
手动添加行将容易出错。我提供工厂方法 正确创建行。我想引导我类的消费者远离生成的 Add*Row
*Table
上的方法类。
添加 Obsolete attributes到生成的方法会成功的。遗憾的是,它们将在下次生成代码时被删除。
我无法使用 partial methods 在非生成代码中,因为 VS2008 DataSet 设计器不使用它们。MyType.Dataset.Designer.cs
看起来有点像这样:
public partial class ThingyDataTable : global::System.Data.DataTable, global::System.Collections.IEnumerable {
// I'd love an [Obsolete("Please use the factory method.")] here.
// I can't use a partial method, as this method isn't partial.
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public ShelfRow NewShelfRow()
return ((ShelfRow)(this.NewRow()));
}
}
有什么方法可以添加
Obsolete
来自 MyType.cs
的属性?尝试 C 风格的原型(prototype)是行不通的,因为成员已经定义。干扰 partial
不起作用,因为生成的成员不是 partial
.// BROKEN EXAMPLE:
public partial class ThingyDataTable {
// I'd love an [Obsolete("Please use the factory method.")] here.
// I can't use a partial method, as this method isn't partial.
[Obsolete("Please use the factory method.")]
public ShelfRow NewShelfRow(); // ERROR: member already defined.
}
有没有其他方法可以标记生成的方法
Obsolete
? 我怎么可能告诫消费者远离生成的方法?
最佳答案
用于从 XSD 文件生成设计器文件的自定义工具并没有太多开箱即用的可配置性。
但是您可以:
后者不那么“自动”,因为您需要在界面中反射(reflect) DataSet 模式的更改,但您可以控制一切并且更加“工厂友好”。
关于.net - 如何将生成的方法标记为过时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/584102/