How to make this type of signature in C #?

3

Is it possible to apply this signature type in C #?

public class Teste {

    public void ver(Class<? extends Teste> tipo) {

    }
}

How?

    
asked by anonymous 05.12.2017 / 18:20

2 answers

2

The closest in C # of the Class class is the Type class.

Class is a generic class: Class<T> .

The expression <? extends Teste> limits the type to those that inherit from Test. The C # equivalent is where T : Teste .

So, in C #, the "equivalent" to this method will be:

public void ver<T>(T objecto) where T : Teste
{
    Type tipo = typeof(T);
    //use tipo como entender
}

As it only needs the type it can be simplified to

public void ver<T>() where T : Teste
{
    Type tipo = typeof(T);
    //use tipo como entender
}
    
05.12.2017 / 19:13
2

Block <? extends Teste> limits type only to classes that inherit from Teste . In C # this expression would be where T : Teste . This T can be anything, it is just an identifier for the type that will be used by the method consumer.

In C # there is nothing like Class<T> of Java. As noted in the ramaral response the closest you can get is using Type .

public class Teste
{
    public void ver<T>(T tipo) where T : Teste
    {
        var tipo = typeof(T);
    }
}

You can see more about the constraints of generics in C # in #.

    
05.12.2017 / 18:28