|
<< Click to Display Table of Contents >> InfoAtributo.cs |
![]() ![]()
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Modelo.classes
{
public class InfoAtributo
{
/// <summary>
/// Médodo auxiliar que recebe um objeto (instância) e procura a partir de seu
/// tipo um CustomAttribute do tipo TabelaAtribute e retorna, permitindo ler o nome da tabela
/// </summary>
/// <returns>retorna o atributo "TabelaAtribute" do objeto passado pro parametro</returns>
public static TabelaAttribute Tabela(object data)
{
return Tabela(data.GetType());
}
public static TabelaAttribute Tabela(Type type)
{
return ((type as MemberInfo).GetCustomAttributes(true)[0] as TabelaAttribute);
}
public static List<PropertyInfo> PropertySimple(Type type)
{
object data = GetObject(type);
return new List<PropertyInfo>(from PropertyInfo p in Property(type)
where (Campo(p) != null)
select p);
}
public static List<PropertyInfo> PropertySimple(object data)
{
return PropertySimple(data.GetType());
}
public static PropertyInfo GetProperty(object data, string propertyName)
{
List<PropertyInfo> list = PropertySimple(data);
foreach (PropertyInfo property in data.GetType().GetProperties())
if (property.Name == propertyName)
return property;
return null;
}
public static PropertyInfo GetProperty(Type type, string propertyName)
{
List<PropertyInfo> list = PropertySimple(type);
foreach (PropertyInfo property in type.GetProperties())
if (property.Name == propertyName)
return property;
return null;
}
public static object GetObject(Type type)
{
try
{
return type.GetConstructor(new Type[] { }).Invoke(new object[] { });
}
catch
{
return null;
}
}
public static List<PropertyInfo> Property(Type type)
{
List<PropertyInfo> list = new List<PropertyInfo>();
foreach (PropertyInfo property in type.GetProperties())
list.Add(property);
return list;
}
public static List<PropertyInfo> PropertyList(object data)
{
return PropertyList(data.GetType());
}
public static CampoAttribute Campo(PropertyInfo property)
{
foreach (object attribute in property.GetCustomAttributes(true))
if (attribute is CampoAttribute)
return (attribute as CampoAttribute);
return null;
}
}
}