开发者

Automatically marking c# classes for serialization

开发者 https://www.devze.com 2023-03-03 13:24 出处:网络
I want to build a visual studio plugin that automatically annotates 开发者_StackOverflow中文版classes for serialization. For example for the built in binary serializer I could just add [Serializable]

I want to build a visual studio plugin that automatically annotates 开发者_StackOverflow中文版classes for serialization. For example for the built in binary serializer I could just add [Serializable] to the class declaration, for WCF it could add [DataContract] to the class and [DataMember] to the members and properties (I could get [KnownType] information through reflection and annotate where appropriate). If using protocol buffers it could add [ProtoContract], [ProtoMember] and [ProtoInclude] attributes and so on. I am assuming that the classes we are going to use this on are safe to serialize (so no sockets or nonserializable stuff in there). What I want to know is what is the easier way to take an existent piece of code (or a binary if that's easier) and add those attributes while preserving the rest of the code intact. I am fine with the output being source code or binary.

It comes to mind the idea of a using a C# parser, parse everything find the interesting code elements, annotate them and write back the code. However that seems to be very complex given the relatively small amount of modifications I want to make to the code. Is there an easier way to do so?


Visual Studio already has an API for discovering and emitting code which you might take a look at. It's not exactly a joy to use but could work for this purpose.


While such a plugin would certainly be a useful thing, I would consider rather making an add-in for a tool like ReSharper instead of VS directly. The advantage is somebody already solved the huge pile of problems you haven't even dreamed of yet and so it will be a lot easier to build such a specific functionality.


it looks to me like you need to have a MSBuild task similar to this one http://kindofmagic.codeplex.com/. is that about right?

0

精彩评论

暂无评论...
验证码 换一张
取 消