|
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;
using Signum.Entities;
using Signum.Utilities;
using System.Linq;
using System.Security.Permissions;
using Signum.Utilities.Reflection;
namespace Signum.Entities.Reflection
{
public class ModifyInspector
{
//dicconario con los field info con posibles modificables de los modificables normales
static Dictionary<Type, Func<object, object>[]> getterCache = new Dictionary<Type, Func<object, object>[]>();
static Func<object, object>[] ModifiableFieldGetters(Type type)
{
lock (getterCache)
return getterCache.GetOrCreate(type, () =>
{
FieldInfo[] aux = Reflector.InstanceFieldsInOrder(type);
return aux.Where(fi => Reflector.IsModifiableOrInterface(fi.FieldType) && !fi.HasAttribute<IgnoreAttribute>())
.Select(fi => ReflectionTools.CreateGetterUntyped(type, fi)).ToArray();
});
}
/// <summary>
/// Devuelve todos los Modificables que haya dentro de obj
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static IEnumerable<Modifiable> FullExplore(Modifiable obj)
{
if (obj == null)//|| obj is Lite)
yield break;
if (Reflector.IsMList(obj.GetType()))
{
Type t = obj.GetType().ElementType();
if (Reflector.IsModifiableOrInterface(t))
{
IEnumerable col = obj as IEnumerable;
foreach (Modifiable item in col)
yield return item;
}
}
else
{
foreach (Func<object, object> getter in ModifiableFieldGetters(obj.GetType()))
{
object field = getter(obj);
if (field == null)
continue;
yield return (Modifiable)field;
}
}
}
public static IEnumerable<Modifiable> IdentifiableExplore(Modifiable obj)
{
if (obj == null)//|| obj is Lite)
yield break;
if (Reflector.IsMList(obj.GetType()))
{
Type t = obj.GetType().ElementType();
if (t.IsModifiable() && !t.IsIdentifiableEntity())
{
IEnumerable col = obj as IEnumerable;
foreach (Modifiable item in col)
yield return item;
}
}
else
{
foreach (Func<object, object> getter in ModifiableFieldGetters(obj.GetType()))
{
object field = getter(obj);
if (field == null || field is IdentifiableEntity)
continue;
yield return (Modifiable)field;
}
}
}
}
}
|
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.
I'm Computer Scientist, one of the founders of Signum Software, and the lead developer behind Signum Framework.
www.signumframework.com
I love programming in C#, Linq, Compilers, Algorithms, Functional Programming, Computer Graphics, Maths...