I Create Generic class like this:
public class Sample<T> where T : class
{
public DoSomething();
}
then I create new class :
public class Sample2
{
Sample<Sample2> obj=new Sample<Sample2>();
}
why can't i use the below code to create an instance of Sample
class in Sample2
class?
Sample<typeof<this>> obj=new Sample<typeof<this>>();
You can make use of generics, so you can pass in the dynamic type for the serializer.
A generic type is a class or interface that is parameterized over types. We use angle brackets (<>) to specify the type parameter.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.
Answer is simple Generics need to be Compile time
but what you're doing is obviously not known during Compile time
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With