protected virtual Type[] GetKnownTypesViaAttributes(Type type)
{
Argument.IsNotNull("type", type);
string typeName = type.AssemblyQualifiedName;
if (string.IsNullOrWhiteSpace(typeName))
{
return new Type[] { };
}
return _knownTypesByAttributesCache.GetFromCacheOrFetch(typeName, () =>
{
var additionalTypes = new List<Type>();
var knownTypeAttributes = type.GetCustomAttributesEx(typeof(KnownTypeAttribute), true);
foreach (var attr in knownTypeAttributes)
{
var ktattr = attr as KnownTypeAttribute;
if (ktattr != null)
{
if (ktattr.MethodName != null)
{
var mi = type.GetMethodEx(ktattr.MethodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
// this can be null because we are also getting here through the recursive behaviour
// of GetCustomAttributesEx. We are getting at this point once per class derived from a
// base class having a KnownType() with a method. This can be ignored
if (mi != null)
{
var types = mi.Invoke(null, null) as IEnumerable<Type>;
if (types != null)
{
additionalTypes.AddRange(types);
}
}
}
else
{
additionalTypes.Add(ktattr.Type);
}
}
}
return additionalTypes.ToArray();
});
}