17,748
社区成员
发帖
与我相关
我的任务
分享namespace AanewB
{
public class A
{
public int a = 1;
}
}namespace AanewB
{
public class B : A
{
public int b = 2;
}
}namespace AanewB
{
public class C
{
public C()
{
A a = new B();
int i = a.a;
}
}
}.class public auto ansi beforefieldinit AanewB.C
extends [mscorlib]System.Object
{
// Methods
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2070
// Code size 20 (0x14)
.maxstack 1
.locals init (
[0] class AanewB.A a
)
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: newobj instance void AanewB.B::.ctor()
IL_000b: stloc.0
IL_000c: ldloc.0
IL_000d: ldfld int32 AanewB.A::a
IL_0012: pop
IL_0013: ret
} // end of method C::.ctor
} // end of class AanewB.C
太绕口了



public C()
{
B b = new B();
A a = b; //or (A)b
a.a = 0;
} .method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2070
// Code size 22 (0x16)
.maxstack 2
.locals init (
[0] class AanewB.B b,
[1] class AanewB.A a
)
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: newobj instance void AanewB.B::.ctor()
IL_000b: stloc.0
IL_000c: ldloc.0
IL_000d: stloc.1
IL_000e: ldloc.1
IL_000f: ldc.i4.0
IL_0010: stfld int32 AanewB.A::a
IL_0015: ret
} // end of method C::.ctor
,就像B里有个对A的引用,导致修改了A里的a后,B里的A的a也就变了,对于整个问题,正确的思路是怎么样的?