Explicit constructor call in C #

So, today I was thinking about .NET arbitrary build using ILSpy + dotPeek to get a deeper understanding of how IL code works when I stumbled upon this strange part (dummy example):

public class SomeBaseClass { public SomeBaseClass(SomeType[] iExpectACollection) { ... } } public class SomeDerivedClass { public SomeDerivedClass(SomeType onlyOneInstance) { SomeType[] collection; if(onlyOneInstance != null) collection = new SomeType[] { onlyOneInstance }; base.\u002Ector(collection); } } 

As far as I can tell, the derived class does not first call the base constructor, but instead does something with onlyOneInstance and , rather , the base constructor.

My question is: is it possible to explicitly call the base constructor in C # after doing some work? Or is it possible only in IL? I know that this is easy to do, for example. Java using super() , however I never saw it in .NET.


EDIT

I just talked with my boss, and he is fine with posting some real code in the library (this is one of our internal companies):

 **IL PART** .method public hidebysig specialname rtspecialname instance void .ctor ( string contextId, class MyComp.NetStack.BufferManager bufferManager, class MyComp.NetStack.TcpChannelQuotas quotas, class [System]System.Security.Cryptography.X509Certificates.X509Certificate2 clientCertificate, class [System]System.Security.Cryptography.X509Certificates.X509Certificate2[] clientCertificateChain, class [System]System.Security.Cryptography.X509Certificates.X509Certificate2 serverCertificate, class MyComp.NetStack.EndpointDescription endpoint, class MyComp.NetStack.ApplicationThreadPool threadPool ) cil managed { // Method begins at RVA 0x648e0 // Code size 263 (0x107) .maxstack 10 .locals init ( [0] class MyComp.NetStack.EndpointDescription[] ) IL_0000: ldarg.0 IL_0001: ldarg.1 IL_0002: ldarg.2 IL_0003: ldarg.3 IL_0004: ldarg.s serverCertificate IL_0006: ldarg.s clientCertificateChain IL_0008: ldarg.s endpoint IL_000a: brtrue.s IL_000f IL_000c: ldnull IL_000d: br.s IL_0021 IL_000f: ldc.i4.1 IL_0010: newarr MyComp.NetStack.EndpointDescription IL_0015: stloc.0 IL_0016: ldloc.0 IL_0017: ldc.i4.0 IL_0018: ldarg.s endpoint IL_001a: stelem.ref IL_001b: ldloc.0 IL_001c: newobj instance void MyComp.NetStack.EndpointDescriptionCollection::.ctor(class [mscorlib]System.Collections.Generic.IEnumerable`1<class MyComp.NetStack.EndpointDescription>) 
+6
source share
3 answers

You can do it:

 public class SomeDerivedClass : SomeBaseClass { public SomeDerivedClass(SomeType onlyOneInstance) : base(new[] { onlyOneInstance}) { } } 

In other words, you can definitely run the code to the base constructor as part of the construction of the parameters passed to it. In this case, we create an array to go to the base class. You can also invoke static methods, as recursive mentions.


I missed the zero check. Apparently, they want to keep the null pass, not the array containing null. This will be equivalent to:

 public class SomeDerivedClass : SomeBaseClass { public SomeDerivedClass(SomeType onlyOneInstance) : base(onlyOneInstance != null ? new [] { onlyOneInstance} : null) { } } 
+4
source

One of the ways this can happen is through field initialization logic.

Another way you can achieve this is to call static methods on the argument values โ€‹โ€‹of the base constructor.

 class Base { public Base(object value) { Console.WriteLine ("Base constructor"); } } class Child : Base { public Child() : base(DoWorkBeforeBaseConstructor()) { } private static object DoWorkBeforeBaseConstructor() { Console.WriteLine ("doing work"); return null; } } 
+2
source

In addition to other answers:

CLR allows you to run arbitrary material before calling the base class ctor. The CLR ensures that you invoke the ctor base class in all possible paths using the method exactly once. I recently experimented using peverify .

C # applies other restrictions as you noticed. If a different language was used to compile this assembly, only the CLR rules apply. And even those that do not belong to the SkipVerification code, which, I believe, is almost all the code that runs today.

+2
source

Source: https://habr.com/ru/post/982405/


All Articles