[英]Get all types with some type included in properties
我從加載的程序集中讀取類型:
var someType = loadedAssemblies
.Where(a => a != null && a.FullName.StartsWith("MY."))
.SelectMany(a => a.GetTypes())
.Distinct()
.ToArray()[0];
類型名稱為“ AddressesRevisionContainerId”。
現在我有這兩個課程:
public class RevisionContainer {
public RevisionContainer(RevisionContainerId revisionContainerId)
{
this.RevisionContainerId = revisionContainerId;
}
public virtual RevisionContainerId RevisionContainerId { get; private set;}
}
public class AddressesRevisionContainerId : RevisionContainerId
{}
如果您有someType,如何找到包含該類型的所有類? 例如,如何從類型“ SomeType”中發現它包含在RevisionContainer類(可能還有其他類)中?
只需遍歷程序集中的類型,並檢查每種類型的屬性類型是否等於某種類型,或者某種類型是其子類:
var typesThatHasSomeTypeAsProperty =
assemblies.SelectMany(assembly =>
assembly.GetTypes().Where(type =>
type.GetProperties().Any(property =>
property.PropertyType == someType || someType.IsSubclassOf(property.PropertyType)))).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.