Interface Rewrite?

G

greenxiar

How to get the function from "rewrited interface implement"?

interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : I {
int I.Value { get { return 2; } }
}

....
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???
....

In B, I implemented 2 times. first in A, then B.
The instance of B alway return I.Value from B implement(2)!

When break in debug mode VS show the b has
base class A, and the interface implement value
is 1(I implement in A = 1) not 2(I implment in B = 2).
It seams i can get the base rewrited interface implement,
But i donot know how.

Someone can give me some tip?

greenxiar
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

greenxiar said:
How to get the function from "rewrited interface implement"?

interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : I {
int I.Value { get { return 2; } }
}

...
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;

You can't do that. The "a" variable is a referece to an instance of the
A class, and it can't hold a reference to an instance of the B class, as
the B class doesn't inherit from the A class.
v = ((I)a).Value; // v = 2 ???
...

In B, I implemented 2 times. first in A, then B.
The instance of B alway return I.Value from B implement(2)!

When break in debug mode VS show the b has
base class A, and the interface implement value
is 1(I implement in A = 1) not 2(I implment in B = 2).
It seams i can get the base rewrited interface implement,
But i donot know how.

It's always the method in the actual class that will be called. You
can't change the behaviour of an object by just changing the type of the
reference.
 
G

greenxiar

The interface implement cannot "override" is a problem. When class function
override or rewrite,
it can still call "base." of the low level function. Interface rewrite hide
all the low level implement.
That means interface is not overridable, the new implement can never call
previous implement function.

But it is strange. The VS Debug can reveal the low level interface detail!
It seams not a formal way.
I donot know how the VS do it. Does the dotnet framework has some mistery
thing of interface issure?
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

greenxiar said:
The interface implement cannot "override" is a problem. When class function
override or rewrite,
it can still call "base." of the low level function. Interface rewrite hide
all the low level implement.
That means interface is not overridable, the new implement can never call
previous implement function.

There is no previous implementation when it comes to interfaces. The
interface is just a contract, and the class that implements it just
fulfills the contract. The class may have a base class, but that has
nothing to do with the interface.
But it is strange. The VS Debug can reveal the low level interface detail!
It seams not a formal way.
I donot know how the VS do it. Does the dotnet framework has some mistery
thing of interface issure?

There is nothing mysterious. Visual Studio just uses reflection to show
the members of the actual object. It does that for any reference, and it
has nothing to do with the interface.
 
G

greenxiar

Sorry, the code have some mistake.

using System;

namespace InterfaceOverride {
interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : A, I {
int I.Value { get { return 2; } }
}

class Program {
static void Main(string[] args) {
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???

}
}
}

My Question is how to query the I.Value = 1 from instance of B class.

Göran Andersson said:
greenxiar said:
The interface implement cannot "override" is a problem. When class
function override or rewrite,
it can still call "base." of the low level function. Interface rewrite
hide all the low level implement.
That means interface is not overridable, the new implement can never call
previous implement function.

There is no previous implementation when it comes to interfaces. The
interface is just a contract, and the class that implements it just
fulfills the contract. The class may have a base class, but that has
nothing to do with the interface.
But it is strange. The VS Debug can reveal the low level interface
detail! It seams not a formal way.
I donot know how the VS do it. Does the dotnet framework has some mistery
thing of interface issure?

There is nothing mysterious. Visual Studio just uses reflection to show
the members of the actual object. It does that for any reference, and it
has nothing to do with the interface.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

greenxiar said:
Sorry, the code have some mistake.

using System;

namespace InterfaceOverride {
interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : A, I {
int I.Value { get { return 2; } }
}

class Program {
static void Main(string[] args) {
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???

}
}
}

My Question is how to query the I.Value = 1 from instance of B class.

You can't.

As you have implemented the interface explicity, you can't use the Value
property from a reference to a class instance, only from a reference to
the interface.

When you cast the reference to an interface reference, it doesn't matter
if it was a reference to an instance of the A class or the B class
before you casted it.
 
G

greenxiar

If you just break and debug, VS shows detail of every hidden interfaces.
You can see I.Value = 1(base A) from B class. It is strange!!!
I think there is another way to get the hidden interface implementation.
Is it a hidden secrete of .NET?

Göran Andersson said:
greenxiar said:
Sorry, the code have some mistake.

using System;

namespace InterfaceOverride {
interface I {
int Value { get; }
}
class A : I {
int I.Value { get { return 1; } }
}
class B : A, I {
int I.Value { get { return 2; } }
}

class Program {
static void Main(string[] args) {
A a = new A();
int v = ((I)a).Value; // v = 1

B b = new B();
int v2 = ((I)b).Value; // v = 2

a = b;
v = ((I)a).Value; // v = 2 ???

}
}
}

My Question is how to query the I.Value = 1 from instance of B class.

You can't.

As you have implemented the interface explicity, you can't use the Value
property from a reference to a class instance, only from a reference to
the interface.

When you cast the reference to an interface reference, it doesn't matter
if it was a reference to an instance of the A class or the B class before
you casted it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top