Reflect, generate, compile, execute, disassemble, decompile, reflect

安稳与你 提交于 2020-01-17 07:01:12

问题


Before heading back to business, I decided to take a few days off for recreational coding. A few 30-hour days without fresh air? Sounds fantastic, for a change.

I've struggled with managed code generation over time and always wondered if there is a circular relationship between C# code, reflection and codedom. Never got around to exploring it sadly. So the goal is to create useless and annoyingly complex classes with nested generics, constraints, anonymous methods, delegates, nested complex reference types, etc. and use reflection+codedom to regenerate their source code from a class called CodeGenerator<T> where T would be the useless classes.

The inspiration came about from revenge frankly. Having a company to run takes you away from programming, and doing hobby projects in your nonexistent free time becomes frustrating due to lack of practice and being out of touch. I feel like an idiot right now but I'll show you! Stupid CLR! That said, please don't mistake this for a joke. I take these little vacations very seriously.

This came up most recently while trying to achieve this. I wanted to generate code without using string manipulation and was stumped at how to create a new XyzObject() constructor call at runtime. I do NOT want to use default(T) because it sets reference types to null. There HAS to be a way. Stupid CLR!

Why SO? Well, it should be a fun discussion to start with. Much to be learnt here.

So the plan:

  • Create a few useless and annoyingly complex classes.
  • Respond to amusing and/or silly suggestions from SO members (hopefully).
  • Attempt to traverse useless class structures using reflection.
  • Get down voted for getting stuck and asking stupid questions.
  • Successfully traverse useless class structures using reflection.
  • Attempt to regenerate code for useless classes using codedom.
  • Get down voted for getting stuck and asking stupid questions.
  • Successfully regenerate code for useless classes using codedom.
  • Gain knowledge along the way.
  • Use said knowledge on unsuspecting developers back home to enforce conventions through code generation.

来源:https://stackoverflow.com/questions/8814508/reflect-generate-compile-execute-disassemble-decompile-reflect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!