📄 generatecode.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Smark.Data.InterfaceToModelGenerator
{
public class GenerateCode
{
const string INTERFACE_CODE=@"(interface)\s+(\w+)";
const string USING_CODE = @"(using)\s+(\w+)";
const string PROPERTY_CODE = @"(\w+)\s+(\w+)\s*\{\s*get;\s*set;\s*\}";
const string TABLE_ATTRIBUTE = "\\[Table\\(\"(\\w*)\"\\)\\]|\\[Table\\]";
const string COLUMN_ATTRIBUTE = "\\[Column\\(\"(\\w*)\"\\)\\]|\\[Column\\]|\\[ID\\(\"(\\w*)\"\\)\\]|\\[ID\\]";
public string TypeName
{
get;
set;
}
public Element TableMapper
{
get;
set;
}
public Element PropertyMapper
{
get;
set;
}
public GenerateCode(System.IO.StreamReader reader)
{
mReader = reader;
System.IO.MemoryStream stream = new System.IO.MemoryStream();
mWrite = new System.IO.StreamWriter(stream, Encoding.UTF8);
}
private System.IO.StreamWriter mWrite;
private System.IO.StreamReader mReader;
private void WriteClass(Match match,string linecode)
{
mWrite.WriteLine("\t[Serializable]");
TypeName = match.Groups[2].Value.Substring(1, match.Groups[2].Value.Length - 1);
linecode = Regex.Replace(linecode, "interface", "public class");
linecode = Regex.Replace(linecode, match.Groups[2].Value, TypeName);
mWrite.WriteLine(linecode + ":Smark.Data.Mappings.DataObject");
}
public int Index
{
get;
set;
}
private void WriteProperty(Match match, string linecode)
{
string type = match.Groups[1].Value;
string pname = match.Groups[2].Value;
string setcode, getcode,field,table;
/* this.mAPV = value;
this.EntityState.FieldChange("APV");*/
getcode = "get{ return m" + pname + ";}";
setcode = "set{m" + pname + "=value;EntityState.FieldChange(\"" + pname + "\");}";
linecode = Regex.Replace(linecode, type, "public " + type);
if (PropertyMapper != null)
{
linecode = Regex.Replace(linecode, "get;", getcode);
linecode = Regex.Replace(linecode, "set;", setcode);
mWrite.WriteLine(linecode);
mWrite.WriteLine("");
mWrite.WriteLine(string.Format("\t\tprivate {0} m{1};", type, pname));
mWrite.WriteLine("");
table = string.IsNullOrEmpty(TableMapper.Value) ? TypeName : TableMapper.Value;
field = string.IsNullOrEmpty(PropertyMapper.Value) ? pname : PropertyMapper.Value;
mWrite.WriteLine(
string.Format("\t\tpublic static Smark.Data.FieldInfo {0} = new Smark.Data.FieldInfo(\"{1}\",\"{2}\");",
pname.Substring(0,1).ToLower()+pname.Substring(1,pname.Length-1), table, field));
mWrite.WriteLine("");
PropertyMapper = null;
}
}
public System.IO.StreamWriter Builder()
{
try
{
Match match;
string linecode = mReader.ReadLine();
while (linecode != null)
{
Index++;
match = Regex.Match(linecode, TABLE_ATTRIBUTE);
if (match.Length > 0)
{
TableMapper = new Element { Match = match, LineIndex = Index };
}
match = Regex.Match(linecode, COLUMN_ATTRIBUTE);
if (match.Length > 0)
{
PropertyMapper = new Element { Match = match, LineIndex = Index };
}
match = Regex.Match(linecode, USING_CODE);
if (match.Length > 0)
{
mWrite.WriteLine(linecode);
goto NEXT;
}
match = Regex.Match(linecode, INTERFACE_CODE);
if (match.Length > 0)
{
WriteClass(match, linecode);
goto NEXT;
}
match = Regex.Match(linecode, PROPERTY_CODE);
if (match.Length > 0)
{
WriteProperty(match, linecode);
goto NEXT;
}
mWrite.WriteLine(linecode);
goto NEXT;
NEXT:
linecode = mReader.ReadLine();
}
}
catch (Exception e_)
{
mWrite.WriteLine(e_.Message);
}
mWrite.Flush();
return mWrite;
}
}
public class Element
{
public int LineIndex
{
get;
set;
}
public Match Match
{
get;
set;
}
public string Value
{
get
{
if (Match.Groups.Count > 1)
{
for (int i = 1; i < Match.Groups.Count; i++)
{
if (!string.IsNullOrEmpty(Match.Groups[i].Value))
return Match.Groups[i].Value;
}
}
return null;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -