繁体   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