Question

Specifically we're making our application compatible with the Out Of Process Session State server where all types saved in session must be serializable.

Is there a way to see at compile time that any type put into HttpSessionState is marked with the Serializable attribute. Something along the lines of this 'non-valid' code

public static void Put<T>( string key, T value ) where T : IsMarkedWitheSerializableAttribute
{
   HttpContext.Current.Session[key] = value;
}
Was it helpful?

Solution

You could write a custom FxCop rule to generate warnings for this scenario.

Jason Block has a sample rule on his site.

OTHER TIPS

No, there's no kind of generic constraint like that. Attributes are generally meant to indicate something about the implementation of a type. You'll have to either introduce your own marker interface (urgh) or insert an execution-time check and hope that your testing is sufficient.

Eric Lippert's post about properties and attributes is good to read here. It's not quite the same question, as it's talking more about objects vs types, but it's still relevant in terms of attributes expressing mechanics.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top