Use typeof in a generic class

I create a Generic class as follows:

public class Sample<T> where T : class { public DoSomething(); } 

then I create a new class:

 public class Sample2 { Sample<Sample2> obj=new Sample<Sample2>(); } 

Why can't I use the code below to instantiate the Sample class in the Sample2 class?

  Sample<typeof<this>> obj=new Sample<typeof<this>>(); 
+4
source share
1 answer

The answer is simple. General characteristics should be Compile time , but what you are doing is obviously not known during Compile time

+5
source

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


All Articles