开发者

Annotating C# code in arbitrary levels without obfuscating the code too much?

开发者 https://www.devze.com 2023-03-28 07:24 出处:网络
I\'m looking for the best way to do the following in C#. Say I have开发者_如何学Go a certain set of instructions:

I'm looking for the best way to do the following in C#.

Say I have开发者_如何学Go a certain set of instructions:

int a = 1;
int b = 2;
if (a==b) {return;}
...

I need to get some information per instruction or sets of instructions. So I annotate the code, roughly as follows:

Console.WriteLine("a");
int a = 1;
Console.WriteLine("a");
int b = 2;
Console.WriteLine("if");
if (a==b) {
 Console.WriteLine("ret");
  return;}
...

Now suppose I need to have much more complete information about each instruction, actually several classes with some methods, and I want to even pass some information to those classes. Annotating the code like that certainly obfuscates the code to a point where it's practically unreadable. Imagine lots of prints in-between instructions, for instance.

My question, finally, is: is there a way (would [Attributes] help here?) to annotate code with arbitrary levels of complexity of the annotations, without cluttering it? I can imagine something like attributes, which would look like

[Class.variable("a")]
int a = 1;
[Class.variable("a")]
int b = 2;
[Class.instruction("if")]
if (a==b) {
  [Class.instruction("ret")]
  return;}
...

while each class does a lot of stuff? If the above example with atts work, could someone give me an example of how it would work "for real" and not in pseudocode?

Thanks very much.


If you want to read the code by some other program you are writing (to generate the graph), I think your best be is using comments, possibly a “special” kind of them like //:, or something.

I would usually advise against parsing source code by yourself, but I think it's the only way here. Although Mono might be of help here: it's compiler is open source and you can use it for your purposes.


If you are debugging your code, then it could be useful to set brakepoints and to configure the "When Hit.." item of the brakepoints context menu. This one lets you print out a text to the Output Window without breaking code the execution.


you can't use attributes to decorate statements, not really sure that there's a clean way to log statements as you envision. what are you trying to accomplish with this level of verbose logging?

0

精彩评论

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

关注公众号