public void IvtVirtualCall2() { var source1 = @" [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm2"")] [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm4"")] public class A { internal virtual void M() { } internal virtual int P { get { return 0; } } internal virtual event System.Action E { add { } remove { } } } "; var source2 = @" [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm3"")] public class B : A { internal override void M() { } internal override int P { get { return 0; } } internal override event System.Action E { add { } remove { } } } "; var source3 = @" [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm4"")] public class C : B { internal override void M() { } internal override int P { get { return 0; } } internal override event System.Action E { add { } remove { } } } "; var source4 = @" using System; using System.Linq.Expressions; public class D : C { internal override void M() { } void Test() { D d = new D(); d.M(); int x = d.P; d.E += null; } void TestET() { D d = new D(); Expression<Action> expr = () => d.M(); } } "; var comp1 = CreateCompilationWithMscorlib(source1, compOptions: TestOptions.Dll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm1"); comp1.VerifyDiagnostics(); var ref1 = new CSharpCompilationReference(comp1); var comp2 = CreateCompilationWithMscorlib(source2, new[] { ref1 }, compOptions: TestOptions.Dll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm2"); comp2.VerifyDiagnostics(); var ref2 = new CSharpCompilationReference(comp2); var comp3 = CreateCompilationWithMscorlib(source3, new[] { ref1, ref2 }, compOptions: TestOptions.Dll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm3"); comp3.VerifyDiagnostics(); var ref3 = new CSharpCompilationReference(comp3); var comp4 = CreateCompilationWithMscorlib(source4, new[] { SystemCoreRef, ref1, ref2, ref3 }, compOptions: TestOptions.Dll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm4"); comp4.VerifyDiagnostics(); // Note: calls C.M, not A.M, since asm2 is not accessible (stops search). // Confirmed in Dev11. var verifier = CompileAndVerify(comp4, emitOptions: EmitOptions.CCI); verifier.VerifyIL("D.Test", @" { // Code size 27 (0x1b) .maxstack 2 .locals init (D V_0, //d int V_1) //x IL_0000: newobj ""D..ctor()"" IL_0005: stloc.0 IL_0006: ldloc.0 IL_0007: callvirt ""void C.M()"" IL_000c: ldloc.0 IL_000d: callvirt ""int C.P.get"" IL_0012: stloc.1 IL_0013: ldloc.0 IL_0014: ldnull IL_0015: callvirt ""void C.E.add"" IL_001a: ret }"); verifier.VerifyIL("D.TestET", @" { // Code size 87 (0x57) .maxstack 3 .locals init (D.<>c__DisplayClass0 V_0, //CS$<>8__locals1 System.Linq.Expressions.Expression<System.Action> V_1) //expr IL_0000: newobj ""D.<>c__DisplayClass0..ctor()"" IL_0005: stloc.0 IL_0006: ldloc.0 IL_0007: newobj ""D..ctor()"" IL_000c: stfld ""D D.<>c__DisplayClass0.d"" IL_0011: ldloc.0 IL_0012: ldtoken ""D.<>c__DisplayClass0"" IL_0017: call ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)"" IL_001c: call ""System.Linq.Expressions.ConstantExpression System.Linq.Expressions.Expression.Constant(object, System.Type)"" IL_0021: ldtoken ""D D.<>c__DisplayClass0.d"" IL_0026: call ""System.Reflection.FieldInfo System.Reflection.FieldInfo.GetFieldFromHandle(System.RuntimeFieldHandle)"" IL_002b: call ""System.Linq.Expressions.MemberExpression System.Linq.Expressions.Expression.Field(System.Linq.Expressions.Expression, System.Reflection.FieldInfo)"" IL_0030: ldtoken ""void C.M()"" IL_0035: call ""System.Reflection.MethodBase System.Reflection.MethodBase.GetMethodFromHandle(System.RuntimeMethodHandle)"" IL_003a: castclass ""System.Reflection.MethodInfo"" IL_003f: ldc.i4.0 IL_0040: newarr ""System.Linq.Expressions.Expression"" IL_0045: call ""System.Linq.Expressions.MethodCallExpression System.Linq.Expressions.Expression.Call(System.Linq.Expressions.Expression, System.Reflection.MethodInfo, params System.Linq.Expressions.Expression[])"" IL_004a: ldc.i4.0 IL_004b: newarr ""System.Linq.Expressions.ParameterExpression"" IL_0050: call ""System.Linq.Expressions.Expression<System.Action> System.Linq.Expressions.Expression.Lambda<System.Action>(System.Linq.Expressions.Expression, params System.Linq.Expressions.ParameterExpression[])"" IL_0055: stloc.1 IL_0056: ret }"); }
public void IvtVirtual_ParamsAndDynamic() { var source1 = @" [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm2"")] public class A { internal virtual void F(params int[] a) { } internal virtual void G(System.Action<dynamic> a) { } [System.Obsolete(""obsolete"", true)] internal virtual void H() { } internal virtual int this[int x, params int[] a] { get { return 0; } } } "; // use IL to generate code that doesn't have synthesized ParamArrayAttribute on int[] parameters: // [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm3"")] // public class B : A // { // internal override void F(int[] a) { } // internal override void G(System.Action<object> a) { } // internal override void H() { } // internal override int this[int x, int[] a] { get { return 0; } } // } var source2 = @" .assembly extern asm1 { .ver 0:0:0:0 } .assembly extern mscorlib { .publickeytoken = (B7 7A 5C 56 19 34 E0 89 ) // .z\V.4.. .ver 4:0:0:0 } .assembly asm2 { .custom instance void [mscorlib]System.Runtime.CompilerServices.InternalsVisibleToAttribute::.ctor(string) = ( 01 00 04 61 73 6D 33 00 00 ) // ...asm3.. } .class public auto ansi beforefieldinit B extends [asm1]A { .custom instance void [mscorlib]System.Reflection.DefaultMemberAttribute::.ctor(string) = ( 01 00 04 49 74 65 6D 00 00 ) // ...Item.. .method assembly hidebysig strict virtual instance void F(int32[] a) cil managed { nop ret } .method assembly hidebysig strict virtual instance void G(class [mscorlib]System.Action`1<object> a) cil managed { nop ret } .method assembly hidebysig strict virtual instance void H() cil managed { nop ret } .method assembly hidebysig specialname strict virtual instance int32 get_Item(int32 x, int32[] a) cil managed { ldloc.0 ret } .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { ldarg.0 call instance void [asm1]A::.ctor() ret } .property instance int32 Item(int32, int32[]) { .get instance int32 B::get_Item(int32, int32[]) } }"; var source3 = @" public class C : B { void Test() { C c = new C(); c.F(); c.G(x => x.Bar()); c.H(); var z = c[1]; } } "; var comp1 = CreateCompilationWithMscorlib(source1, new[] { SystemCoreRef }, compOptions: TestOptions.Dll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm1"); comp1.VerifyDiagnostics(); var ref1 = new CSharpCompilationReference(comp1); var ref2 = CompileIL(source2, appendDefaultHeader: false); var comp3 = CreateCompilationWithMscorlib(source3, new[] { SystemCoreRef, ref1, ref2 }, compOptions: TestOptions.Dll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm3"); comp3.VerifyDiagnostics( // (7,9): error CS7036: There is no argument given that corresponds to the required formal parameter 'a' of 'B.F(int[])' Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "F").WithArguments("a", "B.F(int[])").WithLocation(7, 11), // (8,20): error CS1061: 'object' does not contain a definition for 'Bar' and no extension method 'Bar' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) Diagnostic(ErrorCode.ERR_NoSuchMemberOrExtension, "Bar").WithArguments("object", "Bar").WithLocation(8, 20), // (10,17): error CS7036: There is no argument given that corresponds to the required formal parameter 'a' of 'B.this[int, int[]]' Diagnostic(ErrorCode.ERR_NoCorrespondingArgument, "c[1]").WithArguments("a", "B.this[int, int[]]").WithLocation(10, 17)); }
public void IvtVirtualCall1() { var source1 = @" [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm2"")] public class A { internal virtual void M() { } internal virtual int P { get { return 0; } } internal virtual event System.Action E { add { } remove { } } } "; var source2 = @" [assembly: System.Runtime.CompilerServices.InternalsVisibleTo(""asm3"")] public class B : A { internal override void M() { } internal override int P { get { return 0; } } internal override event System.Action E { add { } remove { } } } "; var source3 = @" using System; using System.Linq.Expressions; public class C : B { internal override void M() { } void Test() { C c = new C(); c.M(); int x = c.P; c.E += null; } void TestET() { C c = new C(); Expression<Action> expr = () => c.M(); } } "; var comp1 = CreateCompilationWithMscorlib(source1, options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm1"); comp1.VerifyDiagnostics(); var ref1 = new CSharpCompilationReference(comp1); var comp2 = CreateCompilationWithMscorlib(source2, new[] { ref1 }, options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm2"); comp2.VerifyDiagnostics(); var ref2 = new CSharpCompilationReference(comp2); var comp3 = CreateCompilationWithMscorlib(source3, new[] { SystemCoreRef, ref1, ref2 }, options: TestOptions.ReleaseDll.WithStrongNameProvider(DefaultProvider), assemblyName: "asm3"); comp3.VerifyDiagnostics(); // Note: calls B.M, not A.M, since asm1 is not accessible. var verifier = CompileAndVerify(comp3, emitOptions: EmitOptions.CCI); verifier.VerifyIL("C.Test", @" { // Code size 25 (0x19) .maxstack 2 IL_0000: newobj ""C..ctor()"" IL_0005: dup IL_0006: callvirt ""void B.M()"" IL_000b: dup IL_000c: callvirt ""int B.P.get"" IL_0011: pop IL_0012: ldnull IL_0013: callvirt ""void B.E.add"" IL_0018: ret }"); verifier.VerifyIL("C.TestET", @" { // Code size 85 (0x55) .maxstack 3 IL_0000: newobj ""C.<>c__DisplayClass0..ctor()"" IL_0005: dup IL_0006: newobj ""C..ctor()"" IL_000b: stfld ""C C.<>c__DisplayClass0.c"" IL_0010: ldtoken ""C.<>c__DisplayClass0"" IL_0015: call ""System.Type System.Type.GetTypeFromHandle(System.RuntimeTypeHandle)"" IL_001a: call ""System.Linq.Expressions.ConstantExpression System.Linq.Expressions.Expression.Constant(object, System.Type)"" IL_001f: ldtoken ""C C.<>c__DisplayClass0.c"" IL_0024: call ""System.Reflection.FieldInfo System.Reflection.FieldInfo.GetFieldFromHandle(System.RuntimeFieldHandle)"" IL_0029: call ""System.Linq.Expressions.MemberExpression System.Linq.Expressions.Expression.Field(System.Linq.Expressions.Expression, System.Reflection.FieldInfo)"" IL_002e: ldtoken ""void B.M()"" IL_0033: call ""System.Reflection.MethodBase System.Reflection.MethodBase.GetMethodFromHandle(System.RuntimeMethodHandle)"" IL_0038: castclass ""System.Reflection.MethodInfo"" IL_003d: ldc.i4.0 IL_003e: newarr ""System.Linq.Expressions.Expression"" IL_0043: call ""System.Linq.Expressions.MethodCallExpression System.Linq.Expressions.Expression.Call(System.Linq.Expressions.Expression, System.Reflection.MethodInfo, params System.Linq.Expressions.Expression[])"" IL_0048: ldc.i4.0 IL_0049: newarr ""System.Linq.Expressions.ParameterExpression"" IL_004e: call ""System.Linq.Expressions.Expression<System.Action> System.Linq.Expressions.Expression.Lambda<System.Action>(System.Linq.Expressions.Expression, params System.Linq.Expressions.ParameterExpression[])"" IL_0053: pop IL_0054: ret } "); }