Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to infer the type of an object and use that type in a generic parameter during construction

Tags:

c#

generics

Is there another way of declaring my ProductController for the logger that is being injected?

public class ProductController : Controller
{
    private readonly LoggingInterface.ILogger<ProductController> _logger;
    private readonly IProductRepository _productRepository;

    public ProductController(LoggingInterface.ILogger<ProductController> logger, IProductRepository productRepository)
    {
        _logger = logger;
        _productRepository = productRepository;
    }
{

Thank you, Stephen

like image 259
Stephen Patten Avatar asked Sep 23 '11 23:09

Stephen Patten


People also ask

What is a generic type parameter?

Generic Methods A type parameter, also known as a type variable, is an identifier that specifies a generic type name. The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

Why to use generics?

Generics enable the use of stronger type-checking, the elimination of casts, and the ability to develop generic algorithms. Without generics, many of the features that we use in Java today would not be possible.

What is difference between object and parameter?

Arguments object in JavaScript is an object, which represents the arguments to the function executing. Here is the difference between rest parameters and the arguments object. Arguments object includes all arguments passed to the function, whereas rest parameters are those, which are not given another name.

How does a generic method differ from a generic type?

From the point of view of reflection, the difference between a generic type and an ordinary type is that a generic type has associated with it a set of type parameters (if it is a generic type definition) or type arguments (if it is a constructed type). A generic method differs from an ordinary method in the same way.


1 Answers

Inference requires the use of an open generic. There are none in this sample

like image 80
JaredPar Avatar answered Nov 08 '22 01:11

JaredPar