how to casting a object inherit form Form to another object in c# 2.0

H

harvie wang

Hi,
I want to implement a common Form with special interface, such as MovePoint(double,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterface
{
void MovePoint(double,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public BWindow:AWindow,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public void Test()
{
BWindow oB = new BWindow();
AWindow oA = (AWindow)oB; //casting exception
IMyWindow oI = (IMyWindow)oB; //casting exception
AWindow oA = oB as AWindow; // 0k but it's null
IMyWindow oI = oB as IMyWindow;
//why oA == null and oI == null?????
if(oA != null)
{
oA.MovePoint(1.0d,3.0d);
}
if(oI != null)
{
oI.MovePoint(1.0d,2.3d);
}

}
}

thanks!

harvie

2006-4-29
 
H

harvie wang

using System;
using System.Collections;
public interface IA
{
void Add();
}
public class A:IA
{
public void Add()
{
Console.WriteLine("A:IA out put");
}
}
public class B:IA
{
public void Add()
{
Console.WriteLine("B:IA out put");
}
}
public interface IB
{
void Work();
}

public class C:A,IB
{
new public void Add()
{
Console.WriteLine("C:A,IB out put");
}
public void Work()
{
Console.WriteLine("C:work");
}
}

public class D:System.Windows.Forms.Form,IA
{
public void Add()
{
Console.WriteLine("D out put");
}
}
public class MyClass
{
public static void Main()
{
D d = new D();
IA a = (IA)d;
a.Add();
d.Add();//ok
//d.Show();

A a = new A();
a.Add();
IA ia = (IA)a;
ia.Add();
B b = (B)ia;// compiler error
b.Add();// error
C c = new C();//ok
c.Add();
IA ic = (IA)c;
ic.Add();
IB ib = (IB)c;
ib.Work();
A ca = (A)c;
ca.Add();

RL();
}

#region Helper methods

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}
说明å¯ä»¥å°†å­ç±»ç›´æŽ¥å¼ºåˆ¶è½¬æ¢ä¸ºçˆ¶ç±»ï¼Œå¹¶è°ƒç”¨çˆ¶ç±»çš„接å£ï¼›
we can cast the child class to parent class, and call the parent class's
methods.
但是如果强制转æ¢çˆ¶ç±»ä¸ºå­ç±»çš„è¯ï¼Œå³ä½¿èƒ½å¤Ÿè½¬æ¢ï¼Œä¹Ÿä¼šå‘生问题(这个在C++中大家都明白的é“ç†ï¼‰ï¼›
it's dangerous to cast a child class to parent class.
也å¯ä»¥æŠŠå­ç±»å¼ºåˆ¶è½¬åŒ–为父类的接å£ç±»ï¼Œå¹¶è°ƒç”¨æŽ¥å£ç±»ä¸­å£°æ˜Žçš„接å£ï¼›
we can cast a child class to parent interface, and can invoke the interface.
如果是通过Asemblyæ供的å射机制生æˆçš„类在进行强制转æ¢çš„时候,在C#2中有编译错误,C#1中还没有测试,有兴趣的也å¯ä»¥çœ‹çœ‹ï¼›
it throw compiler error information in C# 2.0, when i cast a class which
created by assembly reflect.
接下æ¥çš„问题就是研究一下plugin技术,看看人家怎么实现的了:)
 
G

Greg Young

public class B:IA ...
A a = new A();
a.Add();
IA ia = (IA)a;
ia.Add();
B b = (B)ia;// compiler error
b.Add();// error

You are trying to convert an A to a B .. an A is not a B and therefor fails
...

B is always an IA
an IA is not always a B

an A is never a B


harvie wang said:
using System;
using System.Collections;
public interface IA
{
void Add();
}
public class A:IA
{
public void Add()
{
Console.WriteLine("A:IA out put");
}
}
public class B:IA
{
public void Add()
{
Console.WriteLine("B:IA out put");
}
}
public interface IB
{
void Work();
}

public class C:A,IB
{
new public void Add()
{
Console.WriteLine("C:A,IB out put");
}
public void Work()
{
Console.WriteLine("C:work");
}
}

public class D:System.Windows.Forms.Form,IA
{
public void Add()
{
Console.WriteLine("D out put");
}
}
public class MyClass
{
public static void Main()
{
D d = new D();
IA a = (IA)d;
a.Add();
d.Add();//ok
//d.Show();

A a = new A();
a.Add();
IA ia = (IA)a;
ia.Add();
B b = (B)ia;// compiler error
b.Add();// error
C c = new C();//ok
c.Add();
IA ic = (IA)c;
ic.Add();
IB ib = (IB)c;
ib.Work();
A ca = (A)c;
ca.Add();

RL();
}

#region Helper methods

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args); }

private static void RL()
{
Console.ReadLine(); }

private static void Break() {
System.Diagnostics.Debugger.Break();
}

#endregion
}
????????????????,????????;
we can cast the child class to parent class, and call the parent class's
methods.
???????????????,??????,??????(???C++?????????);
it's dangerous to cast a child class to parent class.
?????????????????,????????????;
we can cast a child class to parent interface, and can invoke the
interface.
?????Asembly?????????????????????,?C#2??????,C#1??????,?????????;
it throw compiler error information in C# 2.0, when i cast a class which
created by assembly reflect.
????????????plugin??,??????????:)

Hi,
I want to implement a common Form with special interface, such as
MovePoint(double,double).
I create a interface first:
namespace ABC.Test
{
public Interface IMyWindowInterface
{
void MovePoint(double,double);
}
}
then create a Form inherit form MyWindow:
namespace ABC.Test.A
{
public AWindow:Form,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public BWindow:AWindow,ABC.Test.IMyWindow
{
public void MovePoint(double x,double y)
{
//do work
}
}
public void Test()
{
BWindow oB = new BWindow();
AWindow oA = (AWindow)oB; //casting
exception
IMyWindow oI = (IMyWindow)oB; //casting
exception
AWindow oA = oB as AWindow; // 0k but it's
null
IMyWindow oI = oB as IMyWindow;
//why oA == null and oI == null?????
if(oA != null)
{
oA.MovePoint(1.0d,3.0d);
}
if(oI != null)
{
oI.MovePoint(1.0d,2.3d);
}
}
}
thanks!

harvie

2006-4-29
 

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