How can I do this:
Class A : DependencyObject {}
Class B : DependencyObject {}
Class C : A , B {}
How can I do this:
Class A : DependencyObject {}
Class B : DependencyObject {}
Class C : A , B {}
You can't, C# does not support multiple inheritance of classes.
If you provide a more concrete example of what you're trying to do, maybe we can give you a better solution (for example, perhaps composition is what you're after, rather than inheritance - of course, I can't tell from this simple example).
You cannot have C
be both an A
and a B
unless one of those inherits from the other.
However, if you want C
to have behaviours from A
and B
that are not common to both of them, use an interface.
C# does not have multiple inheritance, so the line
Class C : A , B {}
will never work. You can do similar things with interfaces though, along the lines of
interface InterfaceA { void doA(); }
class A : InterfaceA { public void doA() {} }
interface InterfaceB { void doB(); }
class B : InterfaceB { public void doB() {}}
class C : InterfaceA, InterfaceB
{
m_A = new A();
m_B = new B();
public void doA() { m_A.doA(); }
public void doB() { m_B.doB(); }
}
You can't do multiple inheritance in C# but you can implement multiple interfaces:
Another way, which does not use composition, uses extension methods. Define interfaces with no methods, and implement them with your C class. Then, write a static class with extension methods that provide implementations against your interface.
public static void MyMethod(this InterfaceA a)
{
// do stuff with a
}
downside of this is that you can only work with those properties of the object that are defined in the interface. This limits you to auto-implemented properties and method calls, basically.
For better or worse C# does not support multiple inheritance. However, I have had limited success in simulating it by using extension methods. The extension method approach might look like this.
public class A
{
public void DoSomething() { }
}
public static class B
{
public static void DoSomethingElse(this A target) { }
}
public class C : A
{
}
The obvious problem here is that C
is definitely not a B
. So the only thing this is good for is avoiding duplicate code in some situations.
On the other hand C# does support implementing multiple interfaces. It would look like this.
public interface IA
{
void DoSomething();
}
public interface IB
{
void DoSomethingElse();
}
public class A : IA
{
void DoSomething() { }
}
public class B : IB
{
void DoSomethingElse() { }
}
public class C : IA, IB
{
private A m_A = new A();
private B m_B = new B();
public void DoSomething() { m_A.DoSomething(); }
public void DoSomethingElse() { m_B.DoSomethingElse(); }
}
The obvious problem here is that while you are inheriting the interface you are not inheriting the implementation. This is good for polymorphism, but bad for avoiding duplicate code.
Sometimes you can use both strategies together to cludge together something resembling multiple inheritance, but it is probably going to be difficult to understand and maintain. If your situation really does call for multiple inheritance then your options are going to limited and certainly not ideal, but they do exist nevertheless.
So I guess it cant be done so ...
class A : DependencyObject
{
public int X
{
get { return (int)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(int), typeof(A), new UIPropertyMetadata(0));
}
class B : DependencyObject
{
public int X
{
get { return (int)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
public static readonly DependencyProperty XProperty = DependencyProperty.Register("X", typeof(int), typeof(B), new UIPropertyMetadata(0));
}
class C : DependencyObject
{
A a = new A();
B b = new B();
public int X
{
get { return a.X; }
set { a.X = value; }
}
public int Y
{
get { return b.X; }
set { b.X = value; }
}
}
Hi,
You can achieve this by Multilevel Inheritance...
public class DependencyObject
{
public void DependencyObjectMethod() { }
}
public class A : DependencyObject
{
public void MethodA() { }
}
public class B : A
{
public void MethodB() { }
}
public class C : B
{
public void MethodC()
{
//Do Something
}
}
By this you can get access to all the methods and properties of those classes.