开发者

passing the type parameter dynamically to a generic type

开发者 https://www.devze.com 2023-03-14 23:24 出处:网络
public class Access { public Entity eeee { get; set; } public Permission<eeee.GetType()> pppp { get; set; }
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;}
}

So, If eeee is of type Photo, then the "type" of pppp should be Permission<Photo>

Is there something like eeee.GetType()


Looks like you can very easily change your Access class to

public class Access<T>
{
    public T eeee { get; set; }
    public Permission<T> pppp { get; set; }
}


The idea behind generics is that the generic type is known at runtime; therefore, your code would not compile. I suggest you to use Reflection to accomplish your task.

0

精彩评论

暂无评论...
验证码 换一张
取 消