|
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace XPeriment.Persistence.PersistentObjects
{
public class ReflectionUtility
{
public static bool IsNulable(PropertyInfo pi)
{
object[] attributes = pi.GetCustomAttributes(typeof(NullsAllowed), true);
if (attributes.Length > 0)
return true;
return false;
}
public static string GetDBField(PropertyInfo pi)
{
object[] attributes = pi.GetCustomAttributes(typeof(MapToAttribute), true);
if (attributes.Length > 0)
{
MapToAttribute mapto = (MapToAttribute)attributes[0];
return mapto.DBReference;
}
return pi.Name;
}
public static Dictionary<string, object> GetNameMappings(object obj)
{
Dictionary<string, object> retval = new Dictionary<string, object>();
Type objtype = obj.GetType();
PropertyInfo[] props = objtype.GetProperties();
foreach (PropertyInfo pi in props)
{
if (!MarkedAsPrimaryKey(pi) && !MarkedAsIgnoreMapping(pi))
{
object[] attributes = pi.GetCustomAttributes(typeof(MapToAttribute), true);
if (attributes.Length > 0)
{
MapToAttribute mapto = (MapToAttribute)attributes[0];
retval.Add(mapto.DBReference, pi.GetValue(obj, null));
}
else
{
retval.Add(pi.Name, pi.GetValue(obj, null));
}
}
}
return retval;
}
public static Dictionary<string, object> GetValueMappings(object obj)
{
Dictionary<string, object> retval = new Dictionary<string, object>();
Type objtype = obj.GetType();
PropertyInfo[] props = objtype.GetProperties();
foreach (PropertyInfo pi in props)
{
MapToAttribute[] attributes = (MapToAttribute[])pi.GetCustomAttributes(typeof(MapToAttribute), true);
if (attributes.Length > 0)
{
MapToAttribute mapto = attributes[0];
retval.Add(mapto.DBReference, pi.GetValue(obj, null));
}
else
{
if (!ReflectionUtility.MarkedAsIgnoreMapping(pi))
retval.Add(pi.Name, pi.GetValue(obj, null));
}
}
return retval;
}
public static string GetTargetTable(object obj)
{
Type t = obj.GetType();
MapToAttribute map = (MapToAttribute)t.GetCustomAttributes(typeof(MapToAttribute), true)[0];
return map.DBReference;
}
public static bool MarkedAsPrimaryKey(PropertyInfo pi)
{
object[] attributes = pi.GetCustomAttributes(typeof(PrimaryKeyAttribute),true);
if (attributes.Length > 0)
return true;
return false;
}
public static bool MarkedAsForaignKey(PropertyInfo pi)
{
object[] attributes = pi.GetCustomAttributes(typeof(ForaignKey), true);
if (attributes.Length > 0)
return true;
return false;
}
public static bool MarkedAsIgnoreMapping(PropertyInfo pi)
{
object[] attributes = pi.GetCustomAttributes(typeof(IgnoreMappingAttribute), true);
if (attributes.Length > 0)
return true;
return false;
}
public static string GetPrimaryKeyField(object obj)
{
Type objtype = obj.GetType();
PropertyInfo[] props = objtype.GetProperties();
foreach (PropertyInfo pi in props)
{
PrimaryKeyAttribute[] attributes = (PrimaryKeyAttribute[])pi.GetCustomAttributes(typeof(PrimaryKeyAttribute), true);
if (attributes.Length > 0)
{
PrimaryKeyAttribute pk = attributes[0];
return GetDBField(pi);
}
}
throw new Exception("Object of type '"+objtype.Name+"' has no primary keys defined");
}
public static object GetPrimaryKeyValue(object obj)
{
return "";
}
public static void SetValue(object obj, Dictionary<string, object> mappings)
{
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.