📄 pubfuncs.cs
字号:
Type objType = (Type)_HashObjectType[assemblyname + namespacename + classname];
if (objType == null)
{
object obj = _HashObjectType[assemblyname + namespacename + classname];
obj = Assembly.Load(assemblyname).CreateInstance(namespacename + "." + classname);
_HashObjectType[assemblyname + namespacename + classname] = obj.GetType();
}
return objType;
}
/// <summary>
/// 返回ModelObject的Key值
/// </summary>
/// <param name="ModelObject"></param>
/// <returns></returns>
public static object GetKeyValue(object ModelObject)
{
string strFullName = ModelObject.GetType().FullName;
string strKeyName = Convert.ToString(_HashKeyName[strFullName]);
if (strKeyName == "")
{
MiniORMAttribute.DataObjectAttribute ObjAttr = ModelObject.GetType().GetCustomAttributes(typeof(MiniORMAttribute.DataObjectAttribute), false)[0] as MiniORMAttribute.DataObjectAttribute;
if (ObjAttr != null)
{
strKeyName = ObjAttr.Key;
}
if (strKeyName == "")
{
throw new Exception(ModelObject.ToString() + "没有设置DataObjectAttribute的Key属性。");
}
_HashKeyName[strFullName] = strKeyName;
}
return ModelObject.GetType().InvokeMember(strKeyName, BindingFlags.GetProperty, null, ModelObject, null);
}
/// <summary>
/// 设置ModelObject的Key值
/// </summary>
/// <param name="ModelObject"></param>
/// <param name="keyValue"></param>
public static void SetKeyValue(object ModelObject, object keyValue)
{
string strFullName = ModelObject.GetType().FullName;
string strKeyName = Convert.ToString(_HashKeyName[strFullName]);
if (strKeyName == "")
{
MiniORMAttribute.DataObjectAttribute ObjAttr = ModelObject.GetType().GetCustomAttributes(typeof(MiniORMAttribute.DataObjectAttribute), false)[0] as MiniORMAttribute.DataObjectAttribute;
if (ObjAttr != null)
{
strKeyName = ObjAttr.Key;
}
if (strKeyName == "")
{
throw new Exception(ModelObject.ToString() + "没有设置DataObjectAttribute的Key属性。");
}
_HashKeyName[strFullName] = strKeyName;
}
ModelObject.GetType().InvokeMember(strKeyName, BindingFlags.SetProperty, null, ModelObject, new object[] { keyValue });
}
/// <summary>
/// 设置ModelObject的Foreignkey值,这个对于主从表的Model对象有用
/// </summary>
/// <param name="ModelObject"></param>
/// <param name="ForeignkeyValue"></param>
public static void SetForeignkeyValue(object ModelObject, object ForeignkeyValue)
{
string strFullName = ModelObject.GetType().FullName;
string strForeignkeyName = Convert.ToString(_HashForeignKeyName[strFullName]);
//首先检测缓存中是否已经有ForeignkeyName存在,如果有的话,那么直接取,这样就不再需要再反射出ForeignkeyName了
if (strForeignkeyName == "")
{
MiniORMAttribute.DataObjectAttribute ObjAttr = ModelObject.GetType().GetCustomAttributes(typeof(MiniORMAttribute.DataObjectAttribute), false)[0] as MiniORMAttribute.DataObjectAttribute;
if (ObjAttr != null)
{
strForeignkeyName = ObjAttr.ForeignKey;
}
if (strForeignkeyName == "")
{
throw new Exception(ModelObject.ToString() + "没有设置DataObjectAttribute的ForeignKey属性。");
}
_HashForeignKeyName[strFullName] = strForeignkeyName;
}
ModelObject.GetType().InvokeMember(strForeignkeyName, BindingFlags.SetProperty, null, ModelObject, new object[] { ForeignkeyValue });
}
/// <summary>
/// 返回SqlParameter数组
/// </summary>
/// <param name="userbill"></param>
/// <returns></returns>
public static SqlParameter[] GetParamsWithValue(object ModelObject)
{
List<SqlParameter> Params = new List<SqlParameter>();
PropertyInfo[] props = ModelObject.GetType().GetProperties();
MiniORMAttribute.DataFieldAttribute FieldAttr = null;
foreach (PropertyInfo prop in props)
{
FieldAttr = prop.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false)[0] as MiniORMAttribute.DataFieldAttribute;
if (FieldAttr is MiniORMAttribute.DataFieldAttribute)
{
Params.Add(new SqlParameter("@" + FieldAttr.FieldName, prop.GetValue(ModelObject, null)));
}
}
return Params.ToArray();
}
/// <summary>
/// 返回ModelObject的Attribute中保存的TableName
/// </summary>
/// <param name="ModelObject"></param>
/// <returns></returns>
public static string GetTableName(object ModelObject)
{
return GetTableName(ModelObject.GetType());
}
/// <summary>
/// 根据类型返回对象的数据库表名
/// </summary>
/// <param name="objType"></param>
/// <returns></returns>
public static string GetTableName(Type objType)
{
string strTablename = Convert.ToString(_HashTablename[objType.FullName]);
if (strTablename == "")
{
MiniORMAttribute.DataObjectAttribute ObjAttr = objType.GetCustomAttributes(typeof(MiniORMAttribute.DataObjectAttribute), false)[0] as MiniORMAttribute.DataObjectAttribute;
if (ObjAttr != null)
{
strTablename = ObjAttr.TableName;
}
else
{
throw new Exception(objType.ToString() + ".Attributes不属于MiniORMAttribute.DataFieldAttribute。");
}
if (strTablename == "")
{
throw new Exception(objType.ToString() + ".DataFieldAttribute没有设置TableName属性。");
}
_HashTablename[objType.FullName] = strTablename;
}
return strTablename;
}
/// <summary>
/// 返回此类型的ForeignKey名
/// </summary>
/// <param name="objType"></param>
/// <returns></returns>
public static string GetForeignKey(Type objType)
{
string strForeignKey = Convert.ToString(_HashForeignKeyName[objType.FullName]);
if (strForeignKey == "")
{
MiniORMAttribute.DataObjectAttribute ObjAttr = objType.GetCustomAttributes(typeof(MiniORMAttribute.DataObjectAttribute), false)[0] as MiniORMAttribute.DataObjectAttribute;
if (ObjAttr != null)
{
strForeignKey = ObjAttr.ForeignKey;
}
else
{
throw new Exception(objType.ToString() + ".Attributes不属于MiniORMAttribute.DataFieldAttribute。");
}
if (strForeignKey == "")
{
throw new Exception(objType.ToString() + ".DataFieldAttribute没有设置ForeignKey属性。");
}
_HashForeignKeyName[objType.FullName] = strForeignKey;
}
return strForeignKey;
}
/// <summary>
/// 返回此类型的ForeignKey名
/// </summary>
/// <param name="objType"></param>
/// <returns></returns>
public static string GetKey(Type objType)
{
string strKey = Convert.ToString(_HashKeyName[objType.FullName]);
if (strKey == "")
{
MiniORMAttribute.DataObjectAttribute ObjAttr = objType.GetCustomAttributes(typeof(MiniORMAttribute.DataObjectAttribute), false)[0] as MiniORMAttribute.DataObjectAttribute;
if (ObjAttr != null)
{
strKey = ObjAttr.Key;
}
else
{
throw new Exception(objType.ToString() + ".Attributes不属于MiniORMAttribute.DataFieldAttribute。");
}
if (strKey == "")
{
throw new Exception(objType.ToString() + ".DataFieldAttribute没有设置Key属性。");
}
_HashKeyName[objType.FullName] = strKey;
}
return strKey;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -