113 lines
3.6 KiB
C#
113 lines
3.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
namespace ln.types.reflection
|
|
{
|
|
public static class Reflector
|
|
{
|
|
|
|
public static void Get(object o, string path, out Type type, out object value)
|
|
{
|
|
Queue<string> pElements = new Queue<string>(path.Split('/'));
|
|
type = o.GetType();
|
|
|
|
while (pElements.Count > 0)
|
|
{
|
|
GetValue(o, pElements.Dequeue(), out type, out o);
|
|
}
|
|
value = o;
|
|
}
|
|
public static void Set(object o, string path, object value)
|
|
{
|
|
Queue<string> pElements = new Queue<string>(path.Split('/'));
|
|
Type valueType = null;
|
|
|
|
while (pElements.Count > 1)
|
|
{
|
|
GetValue(o, pElements.Dequeue(), out valueType, out o);
|
|
}
|
|
|
|
SetValue(o, pElements.Dequeue(), value);
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void GetValue(object o, string attributeName,out Type valueType, out object value)
|
|
{
|
|
TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(o.GetType());
|
|
AttributeDescriptor attributeDescriptor = typeDescriptor.GetAttributeDescriptor(attributeName);
|
|
valueType = attributeDescriptor.AttributeType;
|
|
value = attributeDescriptor.GetValue(o);
|
|
}
|
|
public static void SetValue(object o, string attributeName, object value)
|
|
{
|
|
TypeDescriptor typeDescriptor = TypeDescriptor.GetDescriptor(o.GetType());
|
|
typeDescriptor.GetAttributeDescriptor(attributeName).SetValue(o, value);
|
|
}
|
|
|
|
|
|
/*
|
|
public static ReflectedValue[] Walk(object o,string path)
|
|
{
|
|
List<ReflectedValue> reflectedValues = new List<ReflectedValue>();
|
|
|
|
foreach (string pathElement in path.Split('/'))
|
|
{
|
|
ReflectedValue reflectedValue = new ReflectedValue(o, pathElement);
|
|
o = reflectedValue.Value;
|
|
reflectedValues.Add(reflectedValue);
|
|
}
|
|
|
|
return reflectedValues.ToArray();
|
|
}
|
|
|
|
|
|
public static object GetValue(object o, string path) => GetValue(o, new Queue<string>(path.Split('/')));
|
|
public static object GetValue(object o, Queue<string> path)
|
|
{
|
|
while (path.Count > 0)
|
|
o = GetNext(o, path.Dequeue());
|
|
return o;
|
|
}
|
|
public static object SetValue(object o, string path, object value) => SetValue(o, new Queue<string>(path.Split('/')), value);
|
|
public static object SetValue(object o, Queue<string> path, object value)
|
|
{
|
|
while (path.Count > 1)
|
|
o = GetNext(o, path.Dequeue());
|
|
|
|
return o;
|
|
}
|
|
|
|
public static object GetNext(object o, string next)
|
|
{
|
|
if (o is Array)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
else
|
|
{
|
|
return GetAttributeValue(o, next);
|
|
}
|
|
}
|
|
public static void SetNext(object o, string next, object value)
|
|
{
|
|
if (o is Array)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
else
|
|
{
|
|
SetAttributeValue(o, next, value);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
}
|