473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

CA1000: Why shouldn't I declare static members on generic types?

I've written an abstract base type which uses generics to provide XML serialization and deserialization methods for use by its derived types, but I'm seemingly unable to write it in a way which completely satisfies FxCop code analysis. Here is the simplest form of the base class which reproduces the problem:

Expand|Select|Wrap|Line Numbers
  1. public abstract class XmlSerializableItem<T>
  2. {
  3.     public string ToXml()
  4.     {
  5.         XmlSerializer xs = new XmlSerializer( this.GetType() );
  6.         TextWriter xw 
  7.             = new StringWriter( CultureInfo.InvariantCulture );
  8.         xs.Serialize( xw, this );
  9.         return Convert.ToString( xw, CultureInfo.InvariantCulture );
  10.     }
  11.  
  12.     [SuppressMessage("Microsoft.Design", 
  13.                     "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
  14.     public static T FromXml( string xml )
  15.     {
  16.         XmlSerializer xs = new XmlSerializer( typeof( T ) );
  17.         TextReader tr = new StringReader( xml );
  18.         return (T) xs.Deserialize( tr );
  19.     }
  20. }
  21.  
The derived type would look something like

Expand|Select|Wrap|Line Numbers
  1. public class MyDerived : XmlSerializableItem<MyDerived>
  2. {
  3.     /* add some properties etc */
  4. }
  5.  
and it would be used like this

Expand|Select|Wrap|Line Numbers
  1. MyDerived d1 = new MyDerived(); // assume a suitable constructor has been implemented
  2. string xml = d1.ToXml();
  3. MyDerived d2 = MyDerived.FromXml( xml );
  4.  
and after executing this, we'd have two instances of MyDerived, both with the same property values.

The reason the base type needs to be generic is so that I can use the type parameter T as the return type of the FromXml method.

The problem is that the FromXml method throws a FxCop warning CA1000:DoNotDeclareStaticMembersOnGenericTypes. Microsoft seem to be quite emphatic that warning CA1000 should not be suppressed although I'm not sure I agree in this case. Their reason is

// Static method in a generic type.
GenericType<int>.StaticMethod();

// Generic instance method that does not support inference.
someObject.GenericMethod<int>();

Generally, both of the prior declarations should be avoided so that the type argument does not have to be specified when the member is called. This results in a syntax for calling members in generics that is no different from the syntax for non-generics.
Except in this case the type parameter is supplied in the derived type's class declaration and does not need to be provided when the derived type is used - the XmlSerializableItem class is effectively hidden from consumers of the derived type.

If I follow Microsoft's suggestion and change FromXml from a static method to an instance method then the derived type becomes, if anything, more difficult to use:

Expand|Select|Wrap|Line Numbers
  1. MyDerived d1 = new MyDerived(); // assume a suitable constructor has been implemented
  2. string xml = MyDerived.ToXml();
  3. MyDerived d2 = new MyDerived();
  4. MyDerived d2 = d2.FromXml( xml );
  5.  
I need an extra line of code, instantiating an instance of MyDerived, whose value is never used because that instance is only used to make the FromXml method available so that I can instantiate the instance that I actually want, from the XML string. This strikes me as a bit silly.

Maybe I'm missing something, and there's another way of providing this deserialization functionality in a base type without violating any FxCop rules. Or maybe Microsoft have got it wrong and this rule shouldn't apply to static members on abstract base types. Any thoughts on this subject much appreciated...

Thanks

Simon.
Aug 19 '09 #1
0 2928

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
15
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
1
by: Ole Nielsby | last post by:
I'm puzzled at how static fields intersect with generics, as the following snippet illustrates. namespace MonkeyParty { abstract class Fruit { } class Apple : Fruit { } class Pear : Fruit {...
4
by: Ole Nielsby | last post by:
Here comes a generic class with a static property. Let's say they are exotic singleton pets. abstract class Pet {...} abstract class SharedPet<T>: Pet where T: SharedPet<T>, new() {...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
49
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current...
3
by: Filip Zawada | last post by:
Hi, I've encountered a rather interesting language problem. Suppose I've got following types: //class providing usefull static methods public class GoodBase<T> { public static void Met(){};...
5
by: Bill McCormick | last post by:
Hi C# programmers, I'd like to be able to call a static function or property and have it return a generic list. The compiler indicates that "static types cannot be used as generic arguments", so...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.