当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Cast exception in child class

问题描述:

I have two classes ClassA and CLassB. ClassA is an abstract class. ClassB derives from ClassA. I have one DTO named ParentDTO.

public class ParentDTO

{

public int UserId { get; set; }

}

public abstract class ClassA

{

public abstract void CreateUser(ParentDTO dto);

}

public class ClassB : ClassA

{

public override void CreateUser(ParentDTO dto)

{

Console.WriteLine("You are in class ClassB");

}

}

Now I have one DTO(MyDTO), which derives from ParentDTO.

public class MyDTO: ParentDTO

{

public int MyID { get; set; }

}

I have extended ClassB further:

public class ClassC : ClassB

{

public override void CreateUser(ParentDTO dto)

{

var mydto = (MyDTO) dto;// This throws cast exception

mydto.MyID=222;

}

}

I am using the above code as :

 ClassC c = new ClassC();

ParentDTO dto=new ParentDTO();

c.CreateUser(dto);

Can someone please tell me how to cast DTO above in CreateUser method of ClassC. I want to use mydto.MyID in my code. I know I am doing something like casting Animal to Lion instead of Lion to Animal. But is there any way to use child DTO ID? Can someone please tell me what am I doing wrong?

网友答案:

The other answers are correct, but the reason for this behavior would be clearer with a better understanding of the difference between "static type" and "run-time type", which they do not discuss. Static type is the type of a variable in your C# code, while run-time type is the type of an actual object in memory.

We all know that static type and run-time type may differ. Consider:

class A { }
class B : A { }

A a = new B();

The variable a refers to an instance of type B. The static type of the variable is A, but the run-time type of the object is B.

We can also think of a as holding a reference of type A that refers to an instance of type B. This is allowed if and only if type B is derived from (or equal to) type A.

The following, therefore, is not allowed:

B b = new A();

Here, you are attempting to assign a reference to an instance of type A (run-time type) to a variable of (static) type B. A is not derived from or equal to B, so the assignment is not allowed. Similarly, the following assignments are not allowed:

string s = new object();
MyDTO dto = new ParentDTO();

The cast operator complicates things somewhat, but the underlying issue is the same: you are attempting to set a reference to point to an object whose run-time type is not derived from or equal to the static type of the reference:

A a1 = new A();
B b1 = new B();

A a2 = (A)b1;  // fine, because B is derived from A
B b2 = (B)a1;  // not allowed, because A is not derived from B.

Again, this might make more sense if we substitute object for A and string for B:

object a1 = new object();
string b1 = "Hello, World!";

object a2 = (object)b1;  // a2 now points to the string "Hello, World!";
string b2 = (string)a1;  // cast fails: the string variable can't point to an instance of `object`

Similarly, with your types:

ParentDTO a1 = new ParentDTO();
MyDTO     b1 = new MyDTO();

ParentDTO a2 = (ParentDTO)b1;  // works
MyDTO     b2 = (MyDTO)a1;;     // doesn't
网友答案:

you are just using the interface architecture in wrong way. Add get, set to ParentDTO class.

网友答案:

You created an instance of class ParentDTO. Casting it to MyDTO correctly yields this exception as the instance is really of type ParentDTO and not of type MyDTO.
The object instance you have there doesn't have a property with name MyID, it simply doesn't exist, so there is no way to get that - non-existing - value.

Maybe you should describe what you are trying to achieve.

网友答案:

Can someone please tell me how to cast DTO above in CreateUser method of ClassC

No way. You created object of base type using base type in declaration. You cannot just cast it to derived type, it's illegal. Use

ParentDTO dto=new MyDto();

Read this

网友答案:

You can't cast a parent type variable to a child type unless it was initialized as the child type (or another type derived from the child type).

ClassC c = new ClassC();
ParentDTO dto = new MyDTO();
c.CreateUser(dto);
分享给朋友:
您可能感兴趣的文章:
随机阅读: