常规属性生成

本代码是在所写JFrameWork当中辅助的一个类,:-)
很简单.请大家共赏

源代码:

using System;
using System.Collections;
using JFrameWork.Common;

namespace JFrameWork.CodeBuilder
{
 /// <summary>
 /// Description of Propety.
 /// </summary>
 public class Property
 {
  public Property()
  {
  }
  

  public static string GetPropertyBlock(string prespace, ArrayList name, ArrayList type)
  {
   return GetPropertyBlock(prespace,ArrayHelper.ToStringArray(name),ArrayHelper.ToStringArray(type));
  }
  
  public static string GetPropertyBlock(string prespace, string[] name, string[] type)
  {
   System.Text.StringBuilder block = new System.Text.StringBuilder();
   
   for(int i = 0; i < name.Length; i++)
   {
    // Gen:
    // private <TYPE> <_Property>;
          // public <TYPE> <Property>
          // {
          //  get
          // {
          //     return (<_Property>);
          //  }
          //  set
          // {
          //    <_Property> = value;
          //  }
          // }
          block.Append(prespace + " private " + type[i] + " _" + name[i] + ";\r\n");
          block.Append(prespace + " \r\n");
          block.Append(prespace + " public  " + type[i] + "  " + name[i] + "\r\n");
          block.Append(prespace + " {\r\n");
          block.Append(prespace + " \tget\r\n");
          block.Append(prespace + " \t{\r\n");
          block.Append(prespace + " \t\treturn  _" + name[i] + ";\r\n");
          block.Append(prespace + " \t}\r\n");
          block.Append(prespace + " \tset\r\n");
          block.Append(prespace + " \t{\r\n");
          block.Append(prespace + " \t\t_" + name[i] + " = value;\r\n");
          block.Append(prespace + " \t}\r\n");         
          block.Append(prespace + " }\r\n");
          block.Append(prespace + " \r\n");
          ;
   }
   return block.ToString();
  }
 }
}

引用方法:
  /// <summary>
  /// 转换为字符串数组ArrayHelper。ToStringArray
  /// </summary>
  /// <param name="coll"></param>
  /// <returns></returns>
  public static string[ ] ToStringArray( ICollection coll )
  {
   string[ ] result = new string[coll.Count];
   int i = 0;
   foreach( object obj in coll )
   {
    result[ i++ ] = obj.ToString();
   }
   return result;
  }

测试以及引用示例:
using System;
using System.Collections;
using JFrameWork.CodeBuilder;
using JFrameWork.PubUtility.File;

namespace JFrameWork.CodeBuilder.Test
{
 class MainClass
 {
  public static void Main(string[] args)
  {
//   string prespace = "\t";
//   string[] name = {"ID","Name","Value"};
//   string[] type = {"string","string","string"};
   
   ArrayList alname = new ArrayList();
   ArrayList altype = new ArrayList();   
   
   Console.WriteLine("Set Prespace:");
   string prespace = Console.ReadLine();
   string quit;
   do
   {
    Console.WriteLine("Set Name:");
    alname.Add(Console.ReadLine());
    Console.WriteLine("Set Prespace:");
    altype.Add(Console.ReadLine());
    Console.WriteLine("Go on?");
    quit = Console.ReadLine();
   }while (quit != "n" && quit != "N");
   string block  = Property.GetPropertyBlock(prespace, alname, altype);
   Console.WriteLine(block);
   FileManager.ToFile(System.Environment.CurrentDirectory + "/Temp.txt",block);
   //Console.ReadLine();
  }
 }
}

日后将对JFrameWork逐步部分进行开放。
JFrameWork进行中.... ....

posted @ 2006-02-18 10:13  我想去长安  阅读(602)  评论(0编辑  收藏  举报