Reputation: 25983
I need a member of my class to be a Control, and for it to implement an interface we define.
If I declare it like this...
public class MyClass
{
public Control MyMember;
}
... then I don't get the interface methods, but if I declare it like this...
public class MyClass
{
public IMyInterface MyMember;
}
...then I don't get the Control methods. Is there a way to specify that MyMember must be initialised to a type that inherits from both? I can't find one on MSDN. Something like...
public class MyClass
{
public Control : IMyInterface MyMember;
}
... or ...
public class MyClass
{
public Control MyMember : IMyInterface;
}
... except that neither of those work. Can I specify interfaces when I declare a member, and if so, how?
Upvotes: 2
Views: 291
Reputation: 81169
Declare an interface ISelf(of out T) [*] which includes a function "Self" that returns T. Have your interface support a non-generic and generic version, where the generic version inherits both the non-generic version and ISelf(of itself). You can then have a class inherit from both ISelf(of Control) and ISelf(of IMyInterface) and declare your field MyMember to be of type ISelf(of IMyInterface(of Control)). MyMember may then by used as an iMyInterface, and MyMember.Self as a Control. Any number of interfaces may be combined in this way.
[*] VB syntax for generics used to avoid having angle-brackets processed as HTML tags.
Upvotes: 0
Reputation: 9863
This sounds fundamentally wrong.
Does IMyInterface declare only the same methods/properties as found in the Control class? If not, what do you hope to accomplish? You cannot implement an interface and ignore the methods declared within it - you must explicitly write out the implementations.
If IMyInterface does, in fact, declare only the same methods/properties as found in the Control class, you will have to create your own MyControl class which inherits from Control and implements IMyInterface. This isn't some stupid quirk of the language. The Control class wasn't defined to be an IMyInterface and C# is a statically typed - not a "duck-typed" - language, so it cannot automatically give Control whatever interface you desire.
Upvotes: -1
Reputation: 169
Since it's very cumbersome to write a wrapper class around Control and a simple generic class:
public class MyGenericClass<T> where T : Control, IMyInterface
{
public T t;
}
might not fit your needs. You could simply use different properties to access the field differently:
public class MyClass
{
private IMyInterface m_field;
public Control FieldAsControl
{
get { return m_field as Control; }
}
public IMyInterface Field
{
get { return m_field; }
set
{
if (m_field is Control)
{
m_field = value;
}
else
{
throw new ArgumentException();
}
}
}
}
Upvotes: 1
Reputation: 19604
Use the power of inheritance on the interface
public interface IMyInterface : Control
{
..
}
Now you say you want a control with some special methods.
EDIT: TcKs is of course right.. you can't inherit an interface from a concrete class.
One way of solving this problem could be to extend the interface with a property or method that returns a control.
Sample:
public interface IMyInterface
{
Control Control { get; }
[..rest of the definition..]
}
and implement it like that:
class MyControl : Control, IMyInterface
{
public Control Control { get { return this; } }
[..rest of the implementation..]
}
Upvotes: -1
Reputation: 11607
You can have your own class derived from control with interface defined like:
class MyControl : Control, IMyInterface
{
}
and then use this class as the member:
public class MyClass
{
public MyControl MyMember;
}
Upvotes: 0
Reputation: 26632
You can use generics with constraints:
public interface MyClass {
public T GetMyControl() where T : Control, IMyInterface { /* ........ */ }
}
Upvotes: 2