taher chhabrawala
taher chhabrawala

Reputation: 4130

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;}
}

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

Is there something like eeee.GetType()

Upvotes: 0

Views: 349

Answers (2)

Andrea
Andrea

Reputation: 894

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.

Upvotes: 0

Bala R
Bala R

Reputation: 109017

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; }
}

Upvotes: 1

Related Questions