如何使用c#语言在运行时生成c#代码 [英] How to generate c# code at run time using c# language

查看:99
本文介绍了如何使用c#语言在运行时生成c#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   void  AddConstructor()
{
Con = .VarValue;
// 声明构造函数
CodeConstructor构造函数= new CodeConstructor();
constructor.Attributes = MemberAttributes.Public;
constructor.Parameters.Add( new CodeParameterDeclarationExpression(Type,Con));

CodeFieldReferenceExpression widthReference = new CodeFieldReferenceExpression( new CodeThisReferenceExpression(),Con);

constructor.Statements.Add( new CodeAssignStatement(widthReference, new CodeArgumentReferenceExpression( CON)));

targetClass.Members.Add(constructor);
}





此代码的输出为



< pre lang =cs> public MyCode( String val)
{
this .val = val;
}





i只需要非参数化的构造函数

就像这样



  public  MyCode()
{
val = Value;
}





i尝试但没有得到....请给我任何简单的线索来生成c#源代码文件在运行时使用c#...

解决方案

请参阅以下链接获取类似文章:







- Amit


public void AddConstructor()
        {
            Con = this.VarValue;
            // Declare the constructor
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes = MemberAttributes.Public;
            constructor.Parameters.Add(new CodeParameterDeclarationExpression(Type, Con));
            
            CodeFieldReferenceExpression widthReference = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), Con);

            constructor.Statements.Add(new CodeAssignStatement(widthReference, new CodeArgumentReferenceExpression(Con)));
            
            targetClass.Members.Add(constructor);
        }



Output of this code is

public MyCode(String val)
        {
            this.val = val;
        }



i want only non parameterized Constructor
like this

public MyCode()
        {
            val = "Value";
        }



i tried but didn't get.... plese give me any simple clue to generate c# source code file At runtime using c#...

解决方案

Refer the links below for similar articles:



--Amit


这篇关于如何使用c#语言在运行时生成c#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆