是否有可能有一个编译时检查一个类型是标有Serializable属性 [英] Is it possible to have a compile time check that a type is marked with the Serializable attribute
本文介绍了是否有可能有一个编译时检查一个类型是标有Serializable属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具体来说,我们正在做的进程外会话状态服务器的保存在会话中的所有类型都必须是可序列化我们的应用程序兼容。
有没有办法看到的,任何类型的投入HttpSessionState标有Serializable属性的编译时间。沿着这种非有效code东西线
公共静态无效把< T>(字符串键,T值),其中T:IsMarkedWitheSerializableAttribute
{
HttpContext.Current.Session [关键] =价值;
}
解决方案
您可以编写一个自定义的FxCop规则生成这种情况下警告。
贾森阻止对他的网站一个示例规则。
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;
}
解决方案
You could write a custom FxCop rule to generate warnings for this scenario.
Jason Block has a sample rule on his site.
这篇关于是否有可能有一个编译时检查一个类型是标有Serializable属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文