簡體   English   中英

將類型參數動態傳遞給泛型類型

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM