I have a the following C# classes and interfaces:
class NativeTool
class NativeWidget: NativeTool
class NativeGadget: NativeTool
// above classes defined by the API I am using. Below classes and interfaces defined by me.
interface ITool
interface IWidget: ITool
interface IGadget: ITool
class MyTool: NativeTool, ITool
class MyWidget: NativeWidget, IWidget
class MyGadget: NativeGadget, IGadget
Now, I would like MyTool to keep a list of children. The children will all conform to ITool and inherit from NativeTool. The classes MyTool, MyWidget, and MyGadget all fit these criteria.
My question is, is there any way to tell MyTool that its children will always inherit from both NativeTool and ITool? I can do one or the other easily enough. But both?
You may do something like:
public class MyTool<T,U> where T: ITool where U: NativeTool
{
}
and create this like:
var tool = new MyTool<MyWidget, MyWidget>();
and also derivates, like
public class MyWidget : MyTool<....>
{
}
This appears to do it. An annoying number of wrappers, but it gets the job done, without duplicating storage.
public interface ITool { }
public interface IWidget : ITool { }
public class NativeTool { }
public class NativeWidget : NativeTool { }
public class MyTool : NativeTool, ITool, INativeTool {
public MyTool() {
this.Children = new List<INativeTool>();
}
public ITool InterfacePayload { get { return this; } }
public NativeTool NativePayload { get { return this; } }
public List<INativeTool> Children { get; set; }
public NativeTool NativeChild(int index) {
return this.Children[index].NativePayload;
}
public ITool InterfaceChild(int index) {
return this.Children[index].InterfacePayload;
}
public void AddChild(MyTool child) {
this.Children.Add(child);
}
public void AddChild(MyWidget child) {
this.Children.Add(child);
}
}
public class MyWidget : NativeWidget, IWidget, INativeTool {
public ITool InterfacePayload { get { return this; } }
public NativeTool NativePayload { get { return this; } }
}
public interface INativeTool {
// the two payloads are expected to be the same object. However, the interface cannot enforce this.
NativeTool NativePayload { get; }
ITool InterfacePayload { get; }
}
public class ToolChild<TPayload>: INativeTool where TPayload : NativeTool, ITool, INativeTool {
public TPayload Payload { get; set; }
public NativeTool NativePayload {
get {return this.Payload;}
}
public ITool InterfacePayload {
get { return this.Payload; }
}
}
Related
Assuming these classes:
public abstract class Creature
{ public abstract int MaxLevel { get; } }
public abstract class NormalCreature : Creature
{
public abstract Type Mutation { get; }
public override int MaxLevel { get { return 50; } }
}
public abstract class MutantCreature : Creature
{
public abstract Type Base { get; }
public override int MaxLevel { get { return 70; } }
}
public sealed class Human : NormalCreature
{
public override Type Mutation { get { return typeof(Superhuman); } }
...
}
public sealed class Superhuman : MutantCreature
{
public override Type Base { get { return typeof(Human); } }
...
}
public sealed class Dwarf : NormalCreature
{
public override Type Mutation { get { return typeof(DwarfOnADonkey); } }
public void SwimLikeADolphin() { ... }
...
}
public sealed class DwarfOnADonkey : MutantCreature
{
public override Type Base { get { return typeof(Dwarf); } }
// can no longer SwinLikeADolphin(), this is why DwarfOnADonkey does not heritate Dwarf
public void JumpOverTwentyFeet() { ... }
...
}
...
How could I achieved this, getting a connection from each NormalCreature to MutantCreature, and vice-versa, without duplicating code? (if Human mutates into Superhuman, then I could know that Superhuman is the mutation of Human, without specifying it)
I also thought about another class, pairing them with tuple, or array[2], but I do not really like this solution.
I also thought about Attributes, but I don't really know if it could do the job nor how to use them properly.
With inheritance always think 'is a'. A superhuman is a human so Superhuman should derive from Human.
But a human is of a race, not a human is a race. This means Human cannot derive from Race. You could use the term 'creature' as a base class for human and dwarf.
I wouldn't use the class hierarchy to save the history of an object.
Well, I finally chose this solution, but I don't know about its acceptability:
public enum Race
{
Human = 0x01,
Dwarf = 0x02,
...
Mutant = 0x80,
Superhuman = Mutant | Human,
DwarfOnADonkey = Mutant | Dwarf,
...
}
public abstract Creature
{
public abstract Race Race { get; }
...
}
public abstract NormalCreature : Creature { ... }
public abstract MutantCreature : Creature { ... }
public sealed Human : NormalCreature
{
public override Race Race { get Race.Human; }
...
}
public sealed Superhuman : MutantCreature
{
public override Race Race { get Race.Superhuman; }
...
}
Consider the following interface and class declarations.
public interface IMessage
{
string Body { get; set; }
}
public abstract class MessageBase : IMessage
{
public string Body { get; set; }
}
public class MessageA : MessageBase { }
public class MessageB : MessageBase { }
public class MessageC : MessageBase { }
public interface IMessageProcessor<T> where T : IMessage
{
Action<T> ProcessorAction { get; set; }
}
public abstract class MessageProcessorBase<T> : IMessageProcessor<T> where T : MessageBase
{
public Action<T> ProcessorAction { get; set; }
}
public class MessageAProcessor : MessageProcessorBase<MessageA> { }
public class MessageBProcessor : MessageProcessorBase<MessageB> { }
public class MessageCProcessor : MessageProcessorBase<MessageC> { }
Now I want to declare a list of processor instances like so, but cannot figure out what generic type will allow me to add any derived processor type to the list.
var processors = new List<???>();
processors.Add(new MessageAProcessor());
processors.Add(new MessageBProcessor());
processors.Add(new MessageCProcessor());
I have tried:
// compiles but throws InvalidCastException
var processors = new List<IMessageProcessor<IMessage>>();
processors.Add((IMessageProcessor<IMessage>)new MessageAProcessor());
and
// compiles but throws InvalidCastException
var processors = new List<IMessageProcessor<MessageBase>>();
processors.Add((IMessageProcessor<MessageBase>)new MessageAProcessor());
and
// won't compile
var processors = new List<MessageProcessorBase<MessageBase>>();
processors.Add((MessageProcessorBase<MessageBase>)new MessageAProcessor());
What should the type of this list be? I know I must be missing something obvious here. Any help here would be greatly appreciated. Thanks!
You need to introduce IMessageProcessor and inherit all your Processors from it. In list you would have to use this interface.
public interface IMessage {
string Body { get; set; }
}
public abstract class MessageBase : IMessage {
public string Body { get; set; }
}
public class MessageA : MessageBase {
}
public class MessageB : MessageBase {
}
public class MessageC : MessageBase {
}
public interface IMessageProcessor<T> where T : IMessage {
Action<T> ProcessorAction { get; set; }
}
public abstract class MessageProcessorBase<T> : IMessageProcessor<T> where T : MessageBase {
public Action<T> ProcessorAction { get; set; }
public void ProcessMessage(IMessage message) {
var msg = message as T;
ProcessorAction(msg);
}
}
public interface IMessageProcessor {
void ProcessMessage(IMessage message);
}
public class MessageAProcessor : MessageProcessorBase<MessageA>,IMessageProcessor {
}
public class MessageBProcessor : MessageProcessorBase<MessageB>,IMessageProcessor {
}
public class MessageCProcessor : MessageProcessorBase<MessageC>,IMessageProcessor {
}
And Processors:
var processors = new List<IMessageProcessor>();
processors.Add(new MessageAProcessor());
processors.Add(new MessageBProcessor());
processors.Add(new MessageCProcessor());
Since there is no inheritance relationship between ISomething<Type1> and ISomething<Type2> you can't have list of objects that contain mix of those types.
Only real option is to force some base type - either use List<object> or better have base non-generic interface for IMessageProcessor<T> like IMessageProcessor<T> : IMessageProcessor.
Unfortunately it also means you can't have access to properties/arguments using generic type without casts.
I'm not sure what you are trying to accomplish, but your code will indeed not work.
I did the following:
public class MessageAProcessor : MessageProcessorBase<MessageBase> { }
public class MessageBProcessor : MessageProcessorBase<MessageBase> { }
public class MessageCProcessor : MessageProcessorBase<MessageBase> { }
And then:
var processors = new List<MessageProcessorBase<MessageBase>> ();
processors.Add(new MessageAProcessor());
processors.Add(new MessageBProcessor());
processors.Add(new MessageCProcessor());
will work.
The problem is that
public class MessageAProcessor : MessageProcessorBase<MessageA> { }
is too specific, and you can't cast this back to MessageProcessorBase<MessageBase> in the List<>.
I am stuck on interfaces and inheritance. If I implement two classes who both have an interface each, how would I be able to add the properties of Class A and B together? For instance I wanted to associate firstitem with the seconditem.
public interface IAlpha
{
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/AddBravoToAlpha/{firstitem}/{seconditem}")]
void AddBravoToAlpha(int firstitem, int seconditem);
}
public interface IBravo
{
// what goes in here?
}
public Class Alpha
{
public Alpha()
{
AlphaAdd = new List<Bravo>();
}
int Firstitem { get; set }
public List<Bravo> AlphaAdd { get; set; }
}
public Class Bravo
{
public Bravo()
{
BravoAdd = new List<Alpha>(); //not sure if Bravo can access Alpha (derived class)
}
int Seconditem { get; set }
Guid Indexer { get; set }
public List<Alpha> BravoAdd { get; set; }
}
public Class BravoDoesAlpha : IBravo, IAlpha //????
{
List<Alpha> alpha = new List<Alpha>();
List<Bravo> bravo = new List<Bravo>();
public void AddBravoToAlpha(int firstitem, int seconditem)
{
var result = alpha.Where(n => String.Equals(n.Firstitem, firstitem)).FirstOrDefault();
var result1 = bravo.Where(n => String.Equals(n.Seconditem, seconditem)).FirstOrDefault();
if (result != null)
{
result.BravoAdd.Add(new Alpha() { Firstitem = firstitem });
}
if (result1 != null)
{
result1.AlphaAdd.Add(new Bravo() { Seconditem = seconditem });
}
}
}
Okay, so the question you are being asked is basically one about how to do a certain kind of refactoring known as "extracting" an interface.
This is one of the more easy refactorings to do and to understand if you understand interfaces vs. types.
All interfaces are types, but not all types are interfaces.
Now let's assume we are dealing in a world with two families of types: classes and interfaces (as in your example).
Instead of working your example directly, I will work a different but clearer example that does not use Alpha, Bravo, Charlie, Epsilon, etc. because this kind of stuff makes it harder to see the meaning.
First, here's the before:
public class Dog
{
public void Bark() { Console.WriteLine("Woof!"); }
public int NumberOfDogLegs { get { return 2; } }
public int NumberOfDogFriends { get; set; } // this can be set
private string SecretsOfDog { get; set; } // this is private
}
public class DoorBell
{
public void Chime() { Console.WriteLine("Ding!"); }
}
To extract the interface of a class, simply, well, extract all the public members of the class to an interface.
public interface IDog
{
void Bark();
int NumberOfDogLegs { get; }
int NumberOfDogFriends { get; set; }
}
public interface IDoorBell
{
void Chime();
}
Now to really make use of OOP, you can find a way to abstract IDog and IDoorBell. What do they have in common? Well, the obvious one is they both make a noise. So we make a new interface, public interface IMakeANoise and say that IDog and IDoorBell both implement it.
public interface IMakeANoise
{
void MakeNoise();
}
public interface IDog : IMakeANoise
{
void Bark();
int NumberOfDogLegs { get; }
int NumberOfDogFriends { get; set; }
}
public interface IDoorBell : IMakeANoise
{
void Chime();
}
And now we have a new method to implement on Dog and DoorBell.
public class Dog : IDog
{
public void Bark() { Console.WriteLine("Woof!"); }
public int NumberOfDogLegs { get { return 2; } }
public int NumberOfDogFriends { get; set; } // this can be set
private string SecretsOfDog { get; set; } // this is private
public void IMakeANoise() { Bark(); }
}
public class DoorBell : IDoorBell
{
public void Chime() { Console.WriteLine("Ding!"); }
public void IMakeANoise() { Chime(); }
}
Now let's say we are actually writing a video game and Dog and DoorBell are both things that we can show on the screen. Well, this makes them a lot bigger because we will need to provide more information like their coordinates, their states, etc.
In this case, Dog and DoorBell may be very different to us but are similar enough to potentially merit sharing a base class. (Really, this is a stretch, but it does get the point across.)
Without adding all those new interfaces and their implementations, let's just do the "sharing a base class" refactoring for what we already have.
public class RenderableThing : IMakeANoise, IDoAThousandOtherThings
{
protected virtual string MyNoiseToMake { get { return ""; } }
public virtual void MakeANoise()
{
Console.WriteLine(MyNoiseToMake);
}
}
public class Dog : RenderableThing, IDog
{
protected override string MyNoiseToMake { get { return "Woof!"; } }
public void Bark() { MakeANoise(); } // see what we did there?
// Notice that I am not declaring the method MakeANoise because it is inherited and I am using it by overriding MyNoiseToMake
public int NumberOfDogLegs { get { return 2; } }
public int NumberOfDogFriends { get; set; } // this can be set
private string SecretsOfDog { get; set; } // this is private
}
public class DoorBell : RenderableThing, IDoorBell
{
public void Chime() { Console.WriteLine("Ding!"); }
public override void MakeANoise()
{
Chime(); Chime(); Chime(); //I'll do it my own way!
}
}
You may wonder, what's the point? So we can do this...
IMakeANoise dogNoiseMaker = new Dog();
IMakeANoise doorBellNoiseMaker = new DoorBell();
IList<IMakeANoise> listOfNoiseMakers = new List<IMakeANoise>();
listOfNoiseMakers.Add(dogNoiseMaker);
listOfNoiseMakers.Add(doorBellNoiseMaker);
foreach (IMakeANoise noiseMaker in listOfNoiseMakers)
{
noiseMaker.MakeANoise();
}
// This will output
// Woof!
// Ding!
// Ding!
// Ding!
I'm going to take a shot in the dark and venture a guess that you don't quite understand what interfaces and inheritance is. I'll start off by explaining what interfaces are:
Interfaces contain only the definitions of methods, properties, events or indexers that an inheriting class must implement.
For example:
interface IExample
{
void HelloWorld();
}
class ExampleClass : IExample
{
public void HelloWorld()
{
Console.WriteLine("Hello world.");
}
}
Now for Inheritance; when you derive a class from a base class the derived class will inherit all members of the base class except for the constructors. Note: Depending on the accessibility of the members in the base class it's children may or may not be able to access the parents members.
public class Animal
{
public string Name { get; set; }
public Animal(string name)
{
Name = name;
}
public void Talk()
{
Console.WriteLine("{0} is talking", Name);
}
}
public class Cat : Animal
{
public Cat(string name) : base(name) { }
}
public class Dog : Animal
{
public string FurColor { get; set; }
public Dog(string name, string furColor) : base(name)
{
FurColor = furColor;
}
public void Greeting()
{
Console.WriteLine("{0} has {1} fur.", Name, FurColor);
}
}
class Program
{
static void Main(string[] args)
{
var cat = new Cat("Rex");
cat.Talk();
var dog = new Dog("Beanie", "Red");
dog.Talk();
}
}
I have the following base class:
public class Base
{
public string LogicalName { get; set; }
public int NumberOfChars { get; set; }
public Base()
{
}
public Base(string logicalName, int numberOfChars)
{
LogicalName = logicalName;
NumberOfChars = numberOfChars;
}
}
and the following derived classes:
public class Derived1 : Base
{
public const string EntityLogicalName = "Name1";
public const int EntityNumberOfChars = 30;
public Derived1() : base(EntityLogicalName, EntityNumberOfChars)
{
}
}
public class Derived2 : Base
{
public const string EntityLogicalName = "Name2";
public const int EntityNumberOfChars = 50;
public Derived2()
: base(EntityLogicalName, EntityNumberOfChars)
{
}
}
and I also have this function that is provided by a service:
public IEnumerable<T> GetEntities<T>(string entityName, int numberOfChars) where T : Base
{
//Some code to get the entities
}
My problem is how can I call this function generically? I want to call it with something that looks like this:
public void TestEntities<T>() where T : Base
{
var entities = GetEntities<T>(T.EntityLogicalName, T.EntityNumberOfChars);
//some other code to test the entities
}
This of course doesn't work because at this point T is not known. How can I accomplish something similar to this? EntityLogicalName and EntityNumberOfChars are characteristics that all Base derived classes have and they never change for each derived class. Can I get them from the Base class without instantiating objects or some other way that I am not seeing?
Replace constants with getter abstract properties
public abstract class Base
{
public abstract string LogicalName { get; }
public abstract int NumberOfChars { get; }
public Base()
{
}
}
public class Derived1 : Base
{
public string LogicalName { get { return "Name1"; } }
public int NumberOfChars { get { return 30; } }
public Derived1() : base()
{
}
}
Also, you will be able to put some logic into overriden getter, e.g. :
...
public string LogicalName { get { return this.EntityMap.Name; } }
...
UPDATE: The fact that you do not want to instantiate object from class but want to be able to get that string in a strongly typed manner can be handled in one more way. It is totally separate from answer above ( Since you can't override static props in c#). Consider the following code. We are adding one more class here, but LocatorInner can be a member of BaseClass. We are using this approach a lot in several existing apps.:
public class Locator
{
public static class LocatorInner<T> where T : BaseClass
{
public static string Name { get; set; }
}
public static string GetName<T>() where T : BaseClass
{
return LocatorInner<T>.Name;
}
public static void SetName<T>(string name) where T : BaseClass
{
LocatorInner<T>.Name = name;
}
}
public class BaseClass
{
}
public class DerivedClass: BaseClass
{
static DerivedClass()
{
Locator.LocatorInner<DerivedClass>.Name = "me";
}
}
public class TestClass<T> where T : BaseClass
{
public void Method()
{
var name = Locator.GetName<T>();
}
}
IMHO, I believe using constants here is a bad design decision.
You can either solve the issue using #vittore approach, but for me it sounds like you should use meta-programming with attributes if you're looking to get data from the T generic argument
For example, what about:
public class LogicalNameAttribute : Attribute
{
public LogicalNameAttribute(string name)
{
Name = name;
}
public string Name { get; private set; }
}
public class NumberOfCharsAttribute : Attribute
{
public NumberOfCharsAttribute (int number)
{
Number = number;
}
public string Number { get; private set; }
}
[LogicalName("Name1"), NumberOfChars(30)]
public class Derived1 : Base
{
public Derived1() : base()
{
}
}
Now your service method can extract attribute metadata as follows:
public void TestEntities<T>() where T : Base
{
LogicalNameAttribute logicalNameAttr = typeof(T).GetCustomAttribute<LogicalNameAttribute>();
NumberOfCharsAttribute numberOfCharsAttr = typeof(T).GetCustomAttribute<NumberOfCharsAttribute >();
Contract.Assert(logicalNameAttr != null);
Contract.Assert(numberOfCharsAttr != null);
string logicalName = logicalNameAttr.Name;
int numberOfChars = numberOfCharsAttr.Number;
// Other stuff
}
There's a performance penalty because you need to use reflection to get attributes applied to T, but you gain the flexibility of not forcing derived classes to provide this static info.
As #vittore mentioned, move the properties to base,pass the hard coded values from derived and in creation use just defautl(T)
public IEnumerable<T> GetEntities<T>(string entityName, int numberOfChars) where T : Base
{
yield return default(T); //Is its always class use new constraint and return new T();
}
we've reached a point where we have no clue how to continue:
SHORT:
We have a generic interface and a collection of the generic interface. Trying to add an implementation of the generic interface to the collection fails. What happens is that I get a compile time exception saying:
cannot convert from TestApp.IState<T>' to TestApp.IState<TestApp.IView>'
LONG [Code example]:
class Program
{
static void Main(string[] args)
{
var coll = new StateCollection();
var state = new SomeState();
coll.AddState(state);
}
}
public class StateCollection
{
private List<StateBase<IView>> _states = new List<StateBase<IView>>();
public void AddState<T>(StateBase<T> state) where T: IView
{
_states.Add(state);
}
}
public class SomeState : StateBase<SomeView>
{
public IView View
{
get;
}
}
public class SomeView : IView
{
}
public abstract class StateBase<T> where T : IView
{
private SomeView _view;
public SomeView View
{
get { return _view; }
}
}
public interface IView
{
}
Why does this happen? In the AddState we mention that T has to be an instance of IState. Could someone help us out with why this happens and how to do what we want to do?
EDIT1:
We also tried:
public void AddState(IState<IView> state)
{
_states.Add(state);
}
But that just moves the compile time error to 'coll.AddState(state)'
So the same thing happens in another place.
EDIT2:
PROBLEM! I didn't give the right example. Out IState is not an interface but an abstract class. Very sorry for that! Changed code to use abstract class
How about these changes:
public class SomeState : IState<SomeView>
{
public SomeView View
{
get;
set;
}
}
Instead of using generic Type T use IView in AddState method
public void AddState(IState<IView> state)
{
_states.Add(state);
}
Make T covariant in IState using out keyword
public interface IState<out T> where T : IView
{
T View { get; }
}
SOLUTION FOR EDIT2:
Don't know if it is ok for you but you can.
public class StateCollection
{
private List<IState<IView>> _states = new List<IState<IView>>();
public void AddState(IState<IView> state)
{
_states.Add(state);
}
}
public class SomeState : StateBase<SomeView>
{
public override SomeView View
{
get { return null; }
}
}
public abstract class StateBase<T> : IState<T> where T : IView
{
public abstract T View { get; }
}
public interface IState<out T> where T : IView
{
T View { get; }
}
First solution
public class StateCollection
{
private readonly List<IState<IView>> _states = new List<IState<IView>>();
public void AddState(IState<IView> state)
{
_states.Add(state);
}
}
as suggested by Nevyn.
To have it to work, mark T as covariant in interface IState<T>
public interface IState<out T> where T:IView
{
IView View { get; }
}
Second solution : (keep change in class StateCollection)
change interface IState<T> to
public interface IState<out T> where T : IView
{
T View { get; }
}
and class SomeState to
public class SomeState : IState<SomeView>
{
public SomeView View{ get;private set; }
}
Solution for Edit2 :
class Program
{
static void Main(string[] args)
{
var coll = new StateCollection();
var state = new SomeState();
coll.AddState(state);
Console.ReadKey();
}
}
public class StateCollection
{
private List<IStateBase<IView>> _states = new List<IStateBase<IView>>();
public void AddState(IStateBase<IView> state)
{
_states.Add(state);
}
}
public class SomeState : StateBase<SomeView>
{
}
public class SomeView : IView
{
}
public interface IStateBase<out T> where T : IView
{
T View { get; }
}
public abstract class StateBase<T> : IStateBase<T> where T : IView
{
public T View { get; set; }
}
public interface IView
{
}
this looks more like a parameter error for the Add function. Have you tried declaring the add function without using the generics? The inheritance itself should allow it. Make the AddState function look like like so:
Edit (as per Edit2):
As mentioned, the inheritance itself should take care of the generics. As long as whatever class you declared properly implements IView, or IState<IView>, then there shouldn't be any issues...
public absract class StateBase
{
public IView view { get; set; }
....
}
public Interface IView
{ ... }
public class StateCollection
{
private List<StateBase> _states = new List<StateBase>();
public void AddState(StateBase state)
{
_states.Add(state);
}
}
public class SomeView : IView
{ ... }
etc etc and so on, as often as needed
public class SomeState : StateBase
{
private SomeView my_view;
public IView view
{
get { return (IView)SomeView; }
set { ; }
}
}
//program remains unchanged
In this case, SomeState is still an IState object, and all IState objects implement IView, and SomeView is an IView object. SomeState implements SomeView internally. Looks the same to me, but I dont know how well the adaption would work with your real code.
Any other classes would follow the same model. The State will implement StateBase, and internally declare a custom View, which itself needs to extend IView. That way the IView cast on the custom view will work.
From comment:
public class BarState : StateBase
{
private BarView my_view;
public IView view
{
get { return (IView)BarView; }
set { ; }
}
}
public class BarView : IView
{ ... }
Add interface IState (non-generic) and inherit it from IState<T>. Then declare _states as List<IState> and method AddState(IState state)
(EDIT: you just need to have your StateBase inherit from the covariant interface. You can't make a class directly covariant, you always have to go through an interface)
Try this:
public class StateCollection
{
private List<IState<IView>> _states = new List<IState<IView>>();
public void AddState(IState<IView> state)
{
_states.Add(state);
}
}
public class SomeState : StateBase<SomeView>
{
}
public class SomeView : IView
{
}
public interface IState<out T> where T : IView // now covariant with T
{
T View { get; }
}
public abstract class StateBase<T> : IState<T> where T : IView
{
public T View { get; set; }
}
public interface IView
{
}