首页学校入驻客服中心网站导航

手机版

400-035-8011咨询热线

新闻详情

邯郸万博网络教育-c# 代码生成

来源:邯郸万博网络教育学校 时间:2013-05-09

 namespace TwitterClient {
    using System.Collections.Generic;
    
    
    public class Tweet {
        
        private System.DateTime _date;
        
        private string _text;
        
        private string _source;
        
        public virtual System.DateTime Date {
            get {
                return _date;
            }
            set {
                _date = value;
            }
        }
        
        public virtual string Text {
            get {
                return _text;
            }
            set {
                _text = value;
            }
        }
        
        public virtual string Source {
            get {
                return _source;
            }
            set {
                _source = value;
            }
        }
    }
    
    public class Tweets : List<Tweet> {
    }
}

 

For completeness, here's the entirety of the code required to generate the above example:

 

using System.CodeDom;
using Microsoft.CSharp;
using System.IO;
using System;

namespace CodeGeneration
{
  class Program
  {
    static void Main(string[] args)
    {
      CodeCompileUnit compileUnit = new CodeCompileUnit();

      // Add a namespace.
      CodeNamespace twitterNamespace = new CodeNamespace("TwitterClient");
      compileUnit.Namespaces.Add(twitterNamespace);

      // Add a Tweet class.
      CodeTypeDeclaration twitterClass = new CodeTypeDeclaration("Tweet");
      twitterClass.IsClass = true;
      twitterClass.Attributes = MemberAttributes.Public;
      twitterNamespace.Types.Add(twitterClass);

      // Create a field to hold the date.
      CodeMemberField dateField = new CodeMemberField(typeof(DateTime)"_date");
      dateField.Attributes = MemberAttributes.Private;
      twitterClass.Members.Add(dateField);

      // Create a field to hold the text.
      CodeMemberField textField = new CodeMemberField(typeof(string)"_text");
      dateField.Attributes = MemberAttributes.Private;
      twitterClass.Members.Add(textField);

      // Create a field to hold the source.
      CodeMemberField sourceField = new CodeMemberField(typeof(string)"_source");
      dateField.Attributes = MemberAttributes.Private;
      twitterClass.Members.Add(sourceField);

      // Add a property for date.
      twitterClass.Members.Add(CreateProperty("_date""Date"typeof(DateTime)));

      // Add a property for text.
      twitterClass.Members.Add(CreateProperty("_text""Text"typeof(string)));

      // Add a property for source.
      twitterClass.Members.Add(CreateProperty("_source""Source"typeof(string)));

      // Add a class to hold a collection of tweets.
      twitterNamespace.Imports.Add(
        new CodeNamespaceImport("System.Collections.Generic"));
      CodeTypeDeclaration twitterCollection = 
        new CodeTypeDeclaration("Tweets");
      twitterCollection.BaseTypes.Add(new CodeTypeReference("List"
        new CodeTypeReference("Tweet")));

      twitterNamespace.Types.Add(twitterCollection);
 
      // Write the code to a file.
      using (var fileStream = new StreamWriter(File.Create(@"C:\outputfile.cs")))
      {
        var provider = new CSharpCodeProvider();
        provider.GenerateCodeFromCompileUnit(compileUnit, fileStream, null);
      }
    }

    /// <summary>
    /// Creates a public property with getters and setters that wrap the 
    /// specified field.
    /// </summary>
    /// <param name="field">The field to get and set.</param>
    /// <param name="name">The name of the property.</param>
    /// <param name="type">The type of the property.</param>
    /// <returns></returns>
    static CodeMemberProperty CreateProperty(string field, string name, Type type)
    {
      CodeMemberProperty property = new CodeMemberProperty()
      {
        Name = name,
        Type = new CodeTypeReference(type),
        Attributes = MemberAttributes.Public
      };

      property.SetStatements.Add(
        new CodeAssignStatement(
          new CodeFieldReferenceExpression(null, field),
              new CodePropertySetValueReferenceExpression()));

      property.GetStatements.Add(
        new CodeMethodReturnStatement(
          new CodeFieldReferenceExpression(null, field)));

      return property;
    }
  }
}
邯郸万博网络教育学校,开设有专业的邯郸软件开发培训,邯郸网站开发培训,邯郸幼师培训等。

求学登记
相关学校更多>>

全国分站|网站地图|关于我们|联系我们

版权所有: 郑州天华信息技术有限公司