[英]passing the type parameter dynamically to a generic type
public class Access
{
public Entity eeee { get; set; }
public Permission<eeee.GetType()> pppp { get; set; }
}
public class Entity
{
}
public class Permission<T>
{
public bool CanView {get;set;}
public bool CanEdit {get;set;}
public bool CanDelete {get;set;}
}
public class Photo:Entity
{
}
public class PhotoPermission:Permission<Photo>
{
public bool CanTag {get;set;}
}
public class Video:Entity
{
}
public class VideoPermission:Permission<Video>
{
public bool CanFastForward {get;set;}
}
因此,如果eeee
的類型為Photo
,則pppp
的“類型”應為Permission<Photo>
是否有類似eeee.GetType()
東西
看起來您可以非常輕松地將Access
類更改為
public class Access<T>
{
public T eeee { get; set; }
public Permission<T> pppp { get; set; }
}
泛型背后的想法是,泛型類型在運行時是已知的。 因此,您的代碼將無法編譯。 我建議您使用反射來完成您的任務。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.