Home > Net >  How to define the <T> type of List from a function
How to define the <T> type of List from a function

Time:11-26

Need to define the T type inside function to call List in type (T) that I want without repeat the same code. Also need to call function for logging set it type???? for each class.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ewmsCsharp.Models
{
    public class Logging
    {
        public static ILogger<object> setLogger(type????)
        {
            return (ILogger<object>)_serviceProvider.GetService<ILoggerFactory>()
                .CreateLogger <type????> ();
        }

        private static ServiceProvider _serviceProvider = new ServiceCollection() .AddLogging(configuration =>{configuration.AddConsole(); configuration.SetMinimumLevel(LogLevel.Debug); }) .BuildServiceProvider();
    
    }
}

CodePudding user response:

What you want to do involves generic functions. I hope that will be useful :)

https://www.tutorialspoint.com/csharp/csharp_generics.htm

CodePudding user response:

Functions can have generics specified, just like classes can, so public static ILogger<T> setLogger<T>() works just fine.

  • Related