How can i represent a pseudo-type by specifying a base class and an interface?

I have a class that i want to store an object in. This object must inherit DependencyObject and implement IAnimatable. Id like to do something like this:

public class AnimationAspect<T, A> where A : AnimationTimeline, new() { public [DependencyObject, IAnimatable] AnimationTarget { get; } public DependencyProperty AnimatedProperty { get; } public AnimationAspect([DependencyObject, IAnimatable] animationTarget, DependencyProperty animationProperty) {...} } 

so a property of the type "[DependencyObject, IAnimatable]" can only be assigned a value where the actual type inherits DependencyObject and implements IAnimatable (for example, Controls, Shapes, UIElements, etc. but also things like Transform). If i access a property encapsulated in [DependencyObject, IAnimatable], ill be able to access only members defined in DependencyObject and IAnimatable until i cast it to its actual type.

I can do this by adding another generic type and adding a constraint:

public class AnimationAspect<T, D, A> where D : DependencyObject, IAnimatable where A : AnimationTimeline, new() { public D AnimationTarget { get; } public DependencyProperty AnimatedProperty { get; } public AnimationAspect(D animationTarget, DependencyProperty animationProperty) {...} } 

But when a declare a value of type AnimationAspect, i dont want to have to specify the actual type of 'D'. It shouldnt matter the actual type as long as it inherits DependencyObject and implements IAnimatable.

I can also do this with code contracts/specifying AnimationTarget as DependencyObject and ensuring it implements IAnimatable, but requires casting to IAnimatable and just doesnt seem clean.

Is there a way to do this in C#?

by CCRed95 via /r/csharp

Leave a Reply