Код: Выделить всё
var collectionInterfaceType = GetTypeHierarchy(type)
.SelectMany(t => t.Interfaces)
.Select(iface => iface.InterfaceType)
.OfType()
.FirstOrDefault(interfaceType =>
interfaceType.ElementType.FullName is "System.Collections.Generic.ICollection`1" or "System.Collections.Generic.IReadOnlyCollection`1");
if (collectionInterfaceType != null)
{
var itemType = collectionInterfaceType.GenericArguments[0];
// Do something with itemType
}
static IEnumerable GetTypeHierarchy(TypeDefinition type)
{
return GetAncestorTypesAndSelf().Reverse().Skip(1 /*Exclude System.Object*/);
IEnumerable GetAncestorTypesAndSelf()
{
var currentType = type;
while (currentType != null)
{
yield return currentType;
if (currentType.BaseType == null)
break;
currentType = currentType.BaseType.Resolve();
}
}
}
Код: Выделить всё
public class CustomCollection : List { }
Подробнее здесь: https://stackoverflow.com/questions/798 ... with-cecil
Мобильная версия