反射通过逻辑类生成数据库脚本
2015-09-04 23:26:07 访问(1347) 赞(0) 踩(0)
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolStripButton_生成_Click(object sender, EventArgs e)
{
try
{
CreateCode();
}
catch (Exception err)
{
MsgForm.MsgShowDialog(err.Message);
}
}
/// <summary>
/// 是否是对应的逻辑类
/// </summary>
/// <param name="t"></param>
/// <param name="typeName"></param>
/// <returns></returns>
protected bool IsBaseType(Type t, string typeName)
{
while (true)
{
if (t == null)
return false;
if (t.FullName == typeName)
return true;
t = t.BaseType;
}
}
/// <summary>
///
/// </summary>
protected void CreateCode()
{
string dllName = @"E:\Projects\SlowX\SlowXClientZX\Code\SRC\Lib\SlowX.ZXLib\bin\Debug\SlowX.ZXLib.dll";
Assembly ass = Assembly.LoadFile(dllName);
Type[] tA = ass.GetTypes();
string baseTypeName = "SlowX.Core.TableAttribute.BaseTableAttribute";
object oInstance = null;
string dbHelperName = "sqlserver";
StringBuilder sb = new StringBuilder();
string str = null;
foreach (Type t in tA)
{
if (!IsBaseType(t, baseTypeName))
continue;
oInstance = FindInstance8Reflection(t);
if (oInstance == null)
continue;
str = InvokeGetSQL(oInstance, dbHelperName);
if (str != null && str.Length > 0)
sb.AppendLine(str);
}
richTextBox1.Text = sb.ToString();
}
protected string InvokeGetSQL(object oInstance, string dbHelperName)
{
try
{
Type t = oInstance.GetType();
MethodInfo mInfo = t.GetMethod("SqlScriptGet8Str");
if (mInfo == null)
return "";
object[] p = new object[] { dbHelperName };
object oValue = mInfo.Invoke(oInstance, p);
if (oValue == null)
return "";
return oValue.ToString();
}
catch
{
return "";
}
}
/// <summary>
///
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
protected object FindInstance8Reflection
(
Type t
)
{
FieldInfo info = t.GetField("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance);
if (info == null)
return null;
return info.GetValue(null);
}
标签:
反射通过逻辑类生成数据库脚本 


上一条:
下一条:
相关评论
发表评论