ÔÚÉÏһƪÎÄÕÂÖУ¬ÀÏÖܺñ×ÅÁ³Æ¤¸ø´ó»ï½éÉÜÁË´úÂëÎĵµµÄ»ù±¾½á¹¹£¬ÒÔ¼°Ò»Ð©´úÂë¶ÔÏóÓëCodeDomÀàÐ͵ĶÔÓ¦¹Øϵ¡£
ÔÚÆÀÂÛÖÐÀÏÖÜ¿´µ½ÓÐÅóÓÑÌáµ½ÁË Emit£¬ÄÇÀÏÖܾÍ˳±ãÌáһϡ£ÑϸñÉÏ˵£¬Emit²¢²»ÊÇÕë¶Ô´úÂëÎĵµÉú³ÉºÍ±àÒë¶øÉè¼ÆµÄ£¬EmitÒ»·½Ãæ¿ÉÒÔʵʱ·¢³ö IL Ö¸ÁÁíÒ»·½ÃæÒ²Ö§³Ö¶¯Ì¬³ÌÐò¼¯£¬¼´¿ÉÒÔÔÚÔËÐÐʱ´´½¨³ÌÐò¼¯£¬²¢¿ÉÒÔ¶¨ÒåÀàÐÍ£¬È»ºó¿ÉÒÔÖ´ÐС£¶øCodeDomËùÕë¶ÔµÄÊÇ´úÂëÎĵµµÄÉú³ÉºÍ±àÒ룬ËùÒÔ˵£¬ÊÇÓÐËù²»Í¬µÄ¡£
Ŷ£¬ÊÇÁË£¬»¹ÓÐÒ»¸öÍæÒâ¶ùͦÓÐȤ£¬Ò²Ìáһϰɗ—¶¯Ì¬ Linq ±í´ïʽÊ÷¡£ËüÒ²¸ú¶¯Ì¬±àÒëÓеãÏñ£¬¾ÍÊǶ¯Ì¬´´½¨ LINQ±í´ïʽÊ÷£¬LINQ¶®°É£¬±ð¸æËßÄã²»ÖªµÀ£¬ÕâÊÇÍæ.netµÄ±Ø±¸·¨±¦£¬±í´ïʽÊ÷´´½¨ºó»áʵʱ±àÒëΪһ¸öίÍÐʵÀý£¬Ê¹ÓÃʱֱ½Óµ÷ÓÃÉú³ÉµÄίÍÐʵÀý¼´¿É¡£
ºÃ£¬ÏÂÃ濪ʼ±¾ÎĵÄÄÚÈÝ¡£ÏÈ˵˵±í´ïʽ£¬ÒòΪÓï¾äÊÇÓɱí´ïʽ×é³ÉµÄ£¬°´ÕÕÕý³£ÈËÀàµÄ˼¿¼·½Ê½£¬Ó¦µ±ÓÉС¼°´óÀ´Ñ§Ï°¡£É¶ÊDZí´ïʽÄØ£¬Æäʵ¿ÉÒÔ˵£¬±í´ïʽÊÇ´úÂëÎĵµµÄ»ù´¡ÔªËØ£¬±ÈÈçÒ»¸öintÖµ 2500£¬¾ÍÊÇÒ»¸ö±í´ïʽ£»×Ö·û´®³£Á¿ÓÃË«ÒýºÅ°üÆðÀ´£¬Èç"abc"£¬Ò²ÊÇÒ»¸ö±í´ïʽ£»µ±Ç°ÀàʵÀýµÄÒýÓà thisÒ²ÊDZí´ïʽ£»»ùÀàʵÀýµÄÒýÓà base£¬Ò²ÊDZí´ïʽ£»±äÁ¿Ãû a Ò²ÊDZí´ïʽ£»Êý×éË÷Òý£¬Èç [0] Ò²ÊDZí´ïʽ£»·½·¨ÖеÄÊä³ö²ÎÊý out Ò²ÊDZí´ïʽ……
CodeExpression ÊÇËùÓбí´ïʽ¶ÔÏóµÄ¹«¹²»ùÀ࣬´ÓËüµÄÅÉÉúÀàÀ´¿´£¬ÔÛÃDz»·Á¶Ô±í´ïʽµÄÀàÐÍÏÈÀ´¸ö·Çרҵ×ܽᣬÕâÑùÓÐÖúÓÚ´ó¼ÒÕÆÎÕ˼·¡£Õâ¸öÀàµÄÅÉÉúÀàÏ൱¶à£¬²»ÒªÔΣ¬Ë¼Â·ÀíÇåÁË£¬¾Í²»ÅÂËüÊýÁ¿¶à¡£
ÀÏÖÜ´óÖ°ÑÕâЩ±í´ïʽÀà»®·ÖÒÔϼ¸Àࣨ½ö¹©²Î¿¼£©£º
1¡¢´´½¨ÊµÀý¡£ÈçCodeArrayCreateExpression¡¢CodeDelegateCreateExpressionµÈ£¬´ó¼Ò¿ÉÒÔ¸ù¾ÝËüÃǵÄÃû×ÖÀ´²Â²ÂÆä×÷Óã¬ÏÖÔÚÄã²»±ØŪÃ÷°×µ½µ×ÔõôÓ㬺óÃæÀÏÖÜ»á½ÌÄãÔõôÓõġ£
2¡¢ÒýÓᣱÈÈ統ǰʵÀýÒýÓã¨this£©CodeThisReferenceExpression£¬ÔÙ±ÈÈçÒýÓÃij¸öʵÀýµÄ·½·¨µÄÓï¾ä CodeMethodReferenceExpression£¬ Ïñ x.Run(……)¡£ÔÙ±ÈÈçÊôÐÔÀïÃæset·ÃÎÊÆ÷ÖУ¬´ó¼Ò¶¼ÖªµÀÓÐÒ»¸ö value ¹Ø¼ü×Ö£¬ÒªÒýÓÃËü¿ÉÒÔÓà CodePropertySetValueReferenceExpression Àà¡£
3¡¢ÔËËã·û£¨²Ù×÷·û£©¡£ÕâºÃ¶®ÁË°É£¬+-*/=()==!<>ÕâЩ¶¼ÊDzÙ×÷·û¡£¶ÔÓÚ¶þ½øÖÆÔËË㣬¿ÉÒÔÓÃCodeBinaryOperatorExpression£¬Ëüͨ¹ý CodeBinaryOperatorType ö¾ÙÀ´¹æ·¶ÒªÓõ½µÄÔËËã·û£¬Èç¼Ó·¨¡¢¼õ·¨µÈ¡£Èç¹ûÒª½øÐÐÀàÐÍת»»£¬¿ÉÒÔʹÓÃÀàÐÍת»»ÔËËã·û CodeCastExpression£¬ËüÒ²ÊÇÒ»ÖÖ±í´ïʽ¡£Èç¹ûÏëÓà typeof ÔËËã·û£¬¿ÉÒÔʹÓà CodeTypeOfExpression Àà¡£
4¡¢ÆäËû¡£±È½ÏÁãËé¡£Ïñ²ÎÊýÒýÓã¬Êý×éË÷ÒýµÈ¡£
ºÜ¶à±í´ïʽÀàÔÚʹÓÃʱ£¬Ö±½Ó¸³ÏàÓ¦µÄÖµ¾ÍÐУ¬ÓÃÏ°¹ßÁËÖ®ºóÒ²²»»áºÜÄÑ¡£ÔÚ¾ÙÀý֮ǰ£¬ÀÏÖÜÏȽ²Ò»ÏÂÈçºÎÉú³É´úÂ룬ÏàÐÅ´ó»ïÔÚ×öÍêÀý×Óºó£¬×îÆÈÇеľÍÊÇÏë¿´¿´Éú³ÉµÄ´úÂëÊÇɶÑù×ӵġ£
Éú³É´úÂëÒªÓà CodeDomProvider ÀࣨλÓÚSystem.CodeDom.CompilerÃüÃû¿Õ¼äÏ£©£¬Õâ¸öÀ಻Óà new µÄ£¬ËüÓÐÒ»¸ö¾²Ì¬µÄ CreateProvider ·½·¨£¬µ÷ÓúóÖ±½Ó·µ»Ø CodeDomProvider ʵÀý£¬µ÷Ó÷½·¨Ê±£¬¿ÉÒÔÓÃÒ»¸ö×Ö·ûÀ´Ö¸¶¨±à³ÌÓïÑÔ¡£±ÈÈ磬C#¡¢cs¡¢CSharp¶¼¿ÉÒÔ±íʾC#ÓïÑÔ£»Óà VB¡¢VisualBasic¶¼±íʾVBÓïÑÔ£»ÓÃjs¡¢JScript±íʾjscriptÓïÑÔ£»ÓÃcpp±íʾC++ÓïÑÔ£¨Íйܣ©¡£Ãû×ÖÊDz»Çø·Ö´óСдµÄ£¬ËùÒÔ£¬CSºÍcs¶¼Ò»Ñù¡£
È»ºó£¬Äã»á¿´µ½Õâ¸öÀàÓÐÒ»¶ÑGenerateCodeFromXXXXµÄ·½·¨£¬ÕâЩXXXX¿ÉÒÔÊDZí´ïʽ¡¢Óï¾ä¡¢±àÒëµ¥Ôª¡¢ÀàÐͶ¨ÒåµÈ¡£Éú³É´úÂëʱ£¬Äã±ØÐëÌṩһ¸ö TextWriter£¬´úÂëÉú³Éºó»áд½øÕâ¸öwriterÀïÃæ¡£
ÏÂÃæ¸ø´ó¼ÒÑÝʾһÏ£¬ÔÛÃǾͰѴúÂëÊä³öµ½¿ØÖÆ̨°É£¬ÕâÑùÂíÉϾÍÄÜ¿´µ½Ð§¹û¡£
¼ÙÉèÎÒ¶¨ÒåÒ»¸öÀàÐÍ£¬Ãû½Ð Dog£¬ËüÊǽṹ¡£À´£¬¿´´úÂ룺
CodeTypeDeclaration dcl = ); dcl.IsStruct = true; dcl.Attributes = MemberAttributes.Public;
IsStruct Ö¸Ã÷ËüÊǸö½á¹¹£¬CodeTypeDeclarationÀ๹Ô캯ÊýÖд«µÄÊÇÀàÐ͵ÄÃû×Ö£¬½ÐDog¡£AttributesÊôÐÔÉèÖÃÕâ¸öÀàÐÍΪ¹«¹²ÀàÐÍ£¨public£©¡£
ºÃ£¬ÏÖÔÚÎÒÃǾͶ¨ÒåºÃÒ»¸öÀàÐÍÁË£¬ÏÂÃæÔÛÃÇÀ´Éú³ÉC#¡¢VBºÍC++ÈýÖÖÓïÑԵĴúÂë¡£
// Éú³ÉC#´úÂë CodeDomProvider provider = CodeDomProvider.CreateProvider(); Console.WriteLine(); provider.GenerateCodeFromType(dcl, Console.Out, null); // Éú³É VB ´úÂë provider = CodeDomProvider.CreateProvider(); Console.WriteLine(); provider.GenerateCodeFromType(dcl, Console.Out, null); // Éú³É C++ ´úÂë provider = CodeDomProvider.CreateProvider(); Console.WriteLine(); provider.GenerateCodeFromType(dcl, Console.Out, null);
ÔËÐк󣬾ͻῴµ½ÒÔÏÂÄÚÈÝ¡£
·½·¨ºÜ¼òµ¥£¬ÏÈÓÃCreateProvider¾²Ì¬·½·¨»ñÈ¡Ìض¨ÓïÑÔµÄÌṩ³ÌÐò£¬È»ºó GenerateCodeFromXXXXX£¬ÄãÒª´Óʲô´úÂë¶ÔÏóÉú³É£¬¾Íµ÷ÓÃÄĸö°æ±¾£¬±ÈÈçÒª´Ó±àÒëµ¥ÔªÉú³É´úÂ룬¾ÍÓ¦µ±µ÷ÓÃGenerateCodeFromCompileUnit·½·¨£¬Òª´Ó¶¨ÒåµÄÃüÃû¿Õ¼äÉú³É´úÂë¾Íµ÷ÓÃGenerateCodeFromNamespace·½·¨¡£
ºÃ£¬ÔÛÃÇ¿ªÊ¼Á·Ï°£¬ÏÈÀ´¸ö²Ù×÷·ûµÄ£¬ÏÂÃæÀý×ÓÓÃÀ´Éú³É a + b£¬aºÍbÊDZäÁ¿Ãû£¬ÎÒÃÇÏȲ»¹Ü±äÁ¿ÊÇÄÄÀ´µÄ£¬·´ÕýÄ¿µÄÊÇʹÓòÙ×÷·û¡£
CodeVariableReferenceExpression left = ); CodeVariableReferenceExpression right = ); CodeBinaryOperatorExpression opt = new CodeBinaryOperatorExpression(); opt.Operator = CodeBinaryOperatorType.Add; opt.Left = left; opt.Right = right;
Ò»¸ö²Ù×÷·û±í´ïʽ£¬Í¨³£»áÈý¸ö×é³ÉÔªËØ——ÔËËã·û£¬×ó±ß²Ù×÷Êý£¬Óұ߲Ù×÷Êý¡£ÔÚÕâ¸öÀý×ÓÀïÃ棬×ó±ßºÍÓұ߷ֱðʹÓñäÁ¿ÒýÓ㬼´Óõ½ CodeVariableReferenceExpression À࣬Õâ¸öÀàÓ÷¨Ò²¼òµ¥£¬Ö»ÒªÌṩһ¸ö±äÁ¿Ãû³Æ¾ÍÐÐÁË¡£
×îºóÉú³É´úÂëΪ£º
(a + b)
ÔÙ¿´Ò»¸öÀý×Ó¡£
¡¡