How to use .NET to generate C # source code

xiaoxiao2021-03-06  62

Last week, I said how to use .NET implementation directly calculate an expression, in fact, this method can extend to execute a method in a class or use in other more useful places, as in the article, the article is only described One method of implementation, as well as a simple implementation of the method.

Today we have to look at how to automatically generate source code. You can use your own code for your own consumption (Previous article, please refer to: http://www.9cbs.net/develop: http://www.9cbs.net/develop /READ_Article.asp?id=34659).

Is it very cool? Some friends may say this is a bit hard, it is not. We use Microsoft's CodeDom namespaces in our project, so you can easily implement it. For more information about CODEDOM, you can refer to MSDN, which is very clear! I am not Roso here. Connection reference: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemcodedom.asp? Frame = true

This article describes the automatic implementation of the source code of a simple data access layer. All code is written in the way using Hard-Code. If you are interested, you can use other methods, such as the database (resource file), Generate source code and some or mapping operations can also be implemented.

The data access layer in this article has a field and a method. Attribute is a property that gets the connection object, the field is a private member of the connection object, the method is a get method to get a query result set. Source code generation is later like this: use system; use system.data; using system.data.sqlclient;

namespace Power.Cuike519 {public class PowerDataAccess {private SqlConnection m_connection; public PowerDataAccess () {} public virtual SqlConnection Connection {get {return this.m_connection;} set {this.m_connection = value;}} public virtual DataSet GetAllAuthors (string s_State) {try {if (this.m_connection! = null) {System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter (string.Format ( "select * from authors where state like '{0}'", s_State), this.m_connection ); System.data.dataset ds = new dataset (); da.fill (ds); return ds;} else {return null;}} catch (system.applicationException ex) {throw ex;} Finally {this.m_connection.close ();}}}} Let's take a look at what method to generate this code, in order to generate this code, we need a project, I have established a Windows application to do this. You can also use the console or the ASP.NET program to post you generated source code to let more people see. We wrote a method CreatePowerDataAccesslayer in the project. This method created the source code above. Let's take a look at the snapshot of the source code: First we need a stream of the source code, we use the following code to create a stream: stream codeReam = File.open ("MyDataAccess.cs", FileMode.create; Streamwriter CodeWriter = New Streamwriter;

Then we want to write CSHARP code then we use CSHARPCODEPROVIDER to create a code generator. CSharpCodeProvider provider = new CSharpCodeProvider (); ICodeGenerator codeGenerator = provider.CreateGenerator (codeWriter); CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions (); In order to allow our source code can run correctly we use the following method to our source code to add the namespace CodeSnippetCompileUnit literal = New CodesnippetCompileunit ("Using System; / Nusing System.data; / Nusing System.data.sqlclient; / N"); CodeGenerator.GeneratecodeFromCompileUnit (Litral, CodeWriter, CodegeneratorOptions);

Then we have our source code and set a namespace in the namespace below to create a class CodeNamespace codeNamespace = new CodeNamespace ( "Power.Cuike519"); // namespace name CodeTypeDeclaration codeTypeDeclaration = new CodeTypeDeclaration (); codeTypeDeclaration. Name = "PowerDataAccess"; // Class name CODETYPEDECLATION.ISCLASS = true; CODETYPEDECLATION.TYPEATTRIBUTES = TypeAttributes.public; CODENAMESPACE.TYPES.ADD (CODETYPEDECLATION);

Then we create a constructor for the class, create only one default constructor CodeConstructor codeConstrustor = new CodeConstructor () here; codeConstrustor.Attributes = MemberAttributes.Public; codeTypeDeclaration.Members.Add (codeConstrustor);

Then we create a class field, which is private, it is SqlConnection type name m_connectionCodeMemberField codeMember = new CodeMemberField (); codeMember.Name = "m_connection"; codeMember.Attributes = MemberAttributes.Private; codeMember.Type = New CodePereference ("SqlConnection"); CODETYPEDECLATION.MEMBERS.ADD (CODEMEMBER);

With a field, we add a property that can operate and read for this word. We specify the set and get method for it, and it is public, which is the SqlConnection type, as shown below: CodeMeMberProperty codeProperty = new codememberProperty (); codeMemberProperty.Name = "Connection"; codeMemberProperty.Attributes = MemberAttributes.Public; codeMemberProperty.Type = new CodeTypeReference ( "SqlConnection"); codeMemberProperty.GetStatements.Add (new CodeMethodReturnStatement (new CodeFieldReferenceExpression (new CodeThisReferenceExpression (), "m_connection")) ); codeMemberProperty.SetStatements.Add (new CodeAssignStatement (new CodeFieldReferenceExpression (new CodeThisReferenceExpression (), "m_connection"), new CodePropertySetValueReferenceExpression ())); codeTypeDeclaration.Members.Add (codeMemberProperty); in order to make our class looks more with us Use the following method to create a function and content in a function. Here you can write a function in it. This is the reason for this. It can provide a chance to see that Microsoft provides many other relevant source code in the namespace. The class of the statement used.

It is the name of the DataSet type method for the returning method of public, called GetAllauthors. Although this name is called, we still provide a parameter to provide a more flexible use of space, but also methods of using parameters in the function. CodeMemberMethod codeMemberMethod = new CodeMemberMethod (); codeMemberMethod.Name = "GetAllAuthors"; codeMemberMethod.Attributes = MemberAttributes.Public; codeMemberMethod.ReturnType = new CodeTypeReference ( "DataSet"); codeMemberMethod.Parameters.Add (new CodeParameterDeclarationExpression (typeof (string), "s_state")));

In order to make our source code more strong, we use the try-catch-finnaly statement, just like this: CODETRYCATCHFINALLYSTATEMENT TRY1 = New CodeTrycatchFinalLinAlStatement ();

In order to judge our m_connection is not already initialized judgment we use the following statement to determine: CodeConditionStatement conditionalStatement = new CodeConditionStatement (); conditionalStatement.Condition = new CodeVariableReferenceExpression ( "this.m_connection = null!");

We hope that our condition is true when the source code is executed the following code: CodeVariableDeclarationStatement variableDeclarationDA = new CodeVariableDeclarationStatement (typeof (System.Data.SqlClient.SqlDataAdapter), "da", new CodeVariableReferenceExpression ( "new SqlDataAdapter (string.Format (/ "select * from authors where state like '{0}' /", s_State), this.m_connection) ")); conditionalStatement.TrueStatements.Add (variableDeclarationDA); CodeVariableDeclarationStatement variableDeclarationDS = new CodeVariableDeclarationStatement (typeof (DataSet)," ds " , new CodeVariableReferenceExpression ( "new DataSet ()")); conditionalStatement.TrueStatements.Add (variableDeclarationDS); CodeExpression invokeExpression = new CodeMethodInvokeExpression (new CodeTypeReferenceExpression ( "da"), "Fill", new CodeVariableReferenceExpression ( "ds")); CodeExpressionStatement expressionStatement = new CodeExpressionStatement (invokeExpression); conditionalStatement.TrueStatements.Add (expressionStatement); conditionalStatement.TrueStatements.Add (new CodeVariableReferenceExpressi ON ("Return DS"));

The following statement is performed when fake: ConditionalStatement.falseStatements.Add (New CodeVariableReferenceExpression ("Return Null));

Then we add the IF statement to the middle of the TRY statement, just like this: try1.tryStatements.add (ConditionalStatement);

At the same time, we use the following method to add the TRY sentence to our method: codemembermethod.statements.add (try1);

Then it's time we encountered an exception thrown hope information, as follows: CodeCatchClause catch1 = new CodeCatchClause ( "ex", new CodeTypeReference ( "System.ApplicationException")); catch1.Statements.Add (new CodeVariableReferenceExpression ( "throw ex ")); try1.catchclauses.add (catch1);

Then you need to recover the resource code, we will turn off here (actually check the status before you turn off, although SqlDataAdapter does not need to be explicitly closed, but if you add judgment, there is nothing wrong with it) Try1.FinalLinallyStatements.Add (new CodeVariableReferenceExpression ( "this.m_connection.Close ()")); codeTypeDeclaration.Members.Add (codeMemberMethod); the following code can be generated codeGenerator.GenerateCodeFromNamespace (codeNamespace, codeWriter, codeGeneratorOptions);

Don't forget to close the resource codewriter.close (); CodeStream.close ();

Most of the article is code, I hope you have not paired, the final resulting code is what is shown above. There is an insecure call or send an email to wu_jian830@hotmail.com. Thank you!

转载请注明原文地址:https://www.9cbs.com/read-119328.html

New Post(0)