using System;
|
using System.Collections.Concurrent;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Linq;
|
using System.Reflection;
|
using System.Xml.Serialization;
|
|
namespace OpenTap.Addin.Reader
|
{
|
public class PropertyInfoCache
|
{
|
private static readonly ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> cache = new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
|
|
public static void Refresh(params Type[] addIns)
|
{
|
GetProps(typeof(TestVariable));
|
GetProps(typeof(TestPlan));
|
|
var types = TypeData.GetDerivedTypes(TypeData.FromType(typeof(ITestStep)))
|
.Where(x => x.CanCreateInstance)
|
.Where(x => x.GetAttribute<BrowsableAttribute>()?.Browsable ?? true)
|
.OrderBy(x => x.GetAttribute<DisplayAttribute>()?.Order);
|
foreach (var type in types)
|
{
|
var target = type.CreateInstance().GetType();
|
GetProps(target);
|
}
|
foreach (var addin in addIns)
|
{
|
TypeData.GetDerivedTypes(TypeData.FromType(addin));
|
GetProps(addin);
|
}
|
}
|
|
public static Dictionary<string, PropertyInfo> GetProps(Type type)
|
{
|
if (cache.ContainsKey(type))
|
{
|
return cache[type];
|
}
|
PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
|
.Where(p => p.SetMethod != null && p.GetCustomAttribute<XmlIgnoreAttribute>() == null).ToArray();
|
var result = props.ToLookup(p => p.Name, p => p).ToDictionary(g => g.Key, g => g.Last());
|
cache[type] = result;
|
return result;
|
}
|
}
|
}
|