简体   繁体   English

如何从一个 class 到另一个 c# 获取泛型类型

[英]How to get generic type from one class to another c#

Is it possible to pass the generic type from one class to other class generic property.是否可以将泛型类型从一个 class 传递给其他 class 泛型属性。

For example:例如:

Assembly Logger装配记录仪

  namespace Logger
    {
        public class GenericLoger<T>
        {
            T _genericLog;
            LogManager _logManager;
            public GenericLoger(string logName)
            {
                _logManager = new LogManager(logName);

                //Assigning the generic type to Log.GenerciLog, this is how I am 
                  expecting or by some other possible way?.
                Log.GenerciLog = _genericLog;
            }
            public static Write(string description)
            {
                _logManager.write(description);
            }
        }

        public static class Log
        {
            LogManager _logManager;
            static Log()
            {
                _logManager = new LogManager();
            }
            public static Write(string description)
            {
                _logManager.write(description);
            }
            //The generic type supplied in GenericLoger need to pass here,
            //like this or by some other possible way?
            public static T GenerciLog { get; internal set; } 
//T is unrecognized here as type is available in GenericLoger 
//I want to pass here from GenericLoger
        }
   }

Assembly Main Caller of Logger记录器的程序集主调用者

    using Logger;
    namespace DataProcessor
{
    internal class SpecialLogger
    {
        private static Lazy<GenericLog<SpecialLogger>> _passed;
        public static GenericLog<SpecialLogger> Passed
        {
            get
            {
                if (_passed == null)
                {
                    _passed = new Lazy<GenericLog<SpecialLogger>>(() => new GenericLog<SpecialLogger>("Passed"), true);
                }
                return _passed.Value;
            }
        }

        private static Lazy<GenericLog<SpecialLogger>> _failed;
        public static GenericLog<SpecialLogger> Failed
        {
            get
            {
                if (_failed == null)
                {
                    _failed = new Lazy<GenericLog<SpecialLogger>>(() => new GenericLog<SpecialLogger>("Failed"), true);
                }
                return _failed.Value;
            }
        }

    }

    internal class Processor
    {
        public void ProcessRate()
        {
            var trans = dataManager.GetData();
            //Will write the log in "Log.txt" file
            Log.write(trans.Count + " transaction found");
            foreach (var item in trans)
            {
                try
                {
                    //transaction process code here

                    //This will write the text in "Passed.txt" file. 'Passed' property I want to access like this
                    Log.GenerciLog.Passed.Write(item);
                }
                catch (Exception ex)
                {
                     //This will write the text in "Failed.txt" file. 'Failed' property I want to access like this
                    Log.GenerciLog.Failed.Write(item);
                }
            }

        }

    }

}

NOTE : In .NET you don't have a way for automatic type inference for use case like yours, also there is no automatic type substitution.注意:在 .NET 中,您没有像您这样的用例进行自动类型推断的方法,也没有自动类型替换。

Not sure if this is what you are looking for不确定这是否是您要找的

Your method definition should look like this您的方法定义应如下所示

public static T GenerciLog<T> { get; internal set; }

and this is how to call it这就是如何称呼它

try
{
    //transaction process code here

    //This will write the text in "Passed.txt" file. 'Passed' method I want to access like this
    Log.GenerciLog<SpecialLogger>.Passed.Write(item);
}
catch (Exception ex)
{
     //This will write the text in "Failed.txt" file. 'Failed' method I want to access like this
    Log.GenerciLog<SpecialLogger>.Failed.Write(item);
}

This is a very simple log class.这是一个非常简单的日志 class。 There is a lot more you could do with this sort of thing.你可以用这种事情做更多的事情。 Its all provided by log4net which I'd recommend using rather than trying to write your own logger.它全部由 log4net 提供,我建议使用而不是尝试编写自己的记录器。 But the below is a start of how I'd implement a simple logger.但下面是我如何实现一个简单记录器的开始。 It allows you to log to several different things at once.它允许您一次登录多个不同的事物。 I appreciate the below doesn't answer exactly what you want but its an indication of how to start and you can adapt it to suit your needs.我很欣赏以下内容并不能准确回答您想要的内容,但它表明了如何开始,您可以对其进行调整以满足您的需求。

public static class Logger
{
    private static List<ILogger> _loggers = new List<ILogger>();
    public static void Log(string message)
    {
        foreach (var logger in _loggers)
            logger.Write(message);
    }
    public static void AddLogger(ILogger logger)
    {
        _loggers.Add(logger);
    }
}

public interface ILogger
{
    void Write(string message);
}

public class SpecialLogger : ILogger
{
    public void Write(string message)
    {
        //special log code here eg
        Console.WriteLine(message);

    }
}

then somewhere do this然后在某个地方做这个

Logger.AddLogger(new SpecialLogger());
Logger.Log("A log message");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM