How can I mark generated methods obsolete?

I use a strongly typed DataSet for which manually adding rows will be error prone. I provide factory methods for creating strings correctly. I would like to direct the users of my class from the generated Add*Row methods in the *Table classes.

Adding Deprecated Attributes to the generated methods would do the trick. Unfortunately, they will be deleted the next time you create the code.

I can’t use partial methods in non-generated code because the DataSet VS2008 constructor doesn't use them.

MyType.Dataset.Designer.cs looks something like this:

 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())); } } 

Can I add the Obsolete attribute from MyType.cs ? Attempting to prototype a C-style does not work because the element is already defined. A stuck in partial does not work because the generated item is not 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. } 

Is there any other way: mark the generated Obsolete method?

How else can I warn users from a generated method?

+4
source share
4 answers

The custom tool used to create the constructor file from the XSD file is not much configured out of the box.

But you can:

  • Remove it from the Properties dialog box in the xsd file for the entered dataset, which sets the Custom Tool field from MSDataSetGenerator to empty. This way you control when your code is generated. But this is a tendency to make mistakes, as you may forget to do this in the future ...
  • You can create your own tool that calls the xsd.exe tool and place the required attribute in the appropriate places, but this means that you install the custom tool on all your dev stations and create the servers. (See this post for pointers)
  • Or, finally, you can force the code generator to create an internal implementation of your DataSet and force the classes to implement a set of public interfaces that you can allow your clients to use. Your Dataset classes will not be used anywhere outside of your assemblies, but your interfaces will be.

The latter is a bit “automatic,” since you will need to reflect the changes in the DataSet schema on your interfaces, but you control everything and much more “factory-friendly”.

+1
source

Another option is to use PostSharp 1.5 and the new aspect of CustomAttributeInjector (see online documentation ).

Basically, create a CompoundAspect and add the CustomAttributeInjectorAspect property to anything. That should work.

-gael

+1
source

Use the new keyword in a non-generated partial class:

 public partial interface ICaseRepository : IRepository<Case> { void Delete(int id); [Obsolete("Use Delete(int id) instead.")] new void Delete(Case entity); } 

This will allow all current methods of the generated method to generate compile-time warnings.

+1
source

He generated the code, right. Nothing prevents you from using the generator output as an input for the next generator, which adds the [Deprecated] attribute to you.

0
source

All Articles