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

C# 如何从一个类到另一个c类获取泛型类型#,c#,generics,C#,Generics,是否可以将泛型类型从一个类传递到另一个类泛型属性 例如: 组装记录器 namespace Logger { public class GenericLoger<T> { T _genericLog; LogManager _logManager; public GenericLoger(string logName) {

是否可以将泛型类型从一个类传递到另一个类泛型属性

例如:

组装记录器

  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
        }
   }
    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);
                }
            }

        }

    }

}
命名空间记录器
{
公共类通用记录器
{
普通测井;
日志管理器(LogManager);;
公共GenericLoger(字符串日志名)
{
_logManager=新的logManager(logName);
//将泛型类型分配给Log.generilog,我就是这样做的
期待还是通过其他可能的方式?。
Log.genericLog=\u genericLog;
}
公共静态写入(字符串描述)
{
_日志管理器。编写(说明);
}
}
公共静态类日志
{
日志管理器(LogManager);;
静态日志()
{
_logManager=新的logManager();
}
公共静态写入(字符串描述)
{
_日志管理器。编写(说明);
}
//GenericLoger中提供的泛型类型需要传递到此处,
//像这样还是通过其他可能的方式?
公共静态T GenercLog{get;内部集合;}
//此处无法识别T,因为类型在GenericLoger中可用
//我想从总务处传过来
}
}
记录器的程序集主调用者

  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
        }
   }
    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);
                }
            }

        }

    }

}
使用记录器;
命名空间数据处理器
{
内部类特殊记录器
{
私有静态延迟(u)通过;;
已传递公共静态GenericLog
{
得到
{
如果(_passed==null)
{
_已通过=新的延迟(()=>新的GenericLog(“已通过”),true);
}
返回_passed.Value;
}
}
私有静态惰性_失败;
公共静态GenericLog失败
{
得到
{
如果(_failed==null)
{
_failed=new Lazy(()=>new GenericLog(“failed”),true);
}
返回_失败的值;
}
}
}
内部类处理器
{
公营房屋空置率()
{
var trans=dataManager.GetData();
//将在“log.txt”文件中写入日志
Log.write(trans.Count+“找到事务”);
foreach(交易中的var项目)
{
尝试
{
//此处为事务处理代码
//这将在“Passed.txt”文件中写入文本。我希望像这样访问“Passed”属性
Log.generilog.Passed.Write(项);
}
捕获(例外情况除外)
{
//这将在“Failed.txt”文件中写入文本。我希望像这样访问“Failed”属性
Log.generilog.Failed.Write(项);
}
}
}
}
}

注意:在.NET中,对于您的用例,您没有自动类型推断的方法,也没有自动类型替换

不确定这是否是你要找的

您的方法定义应该如下所示

public static T GenerciLog<T> { get; internal set; }
public static T generilog{get;internal set;}
这就是它的名称

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);
}
试试看
{
//此处为事务处理代码
//这将在“Passed.txt”文件中写入文本。我希望像这样访问“Passed”方法
Log.generilog.Passed.Write(项);
}
捕获(例外情况除外)
{
//这将在“Failed.txt”文件中写入文本。我希望像这样访问“Failed”方法
Log.generilog.Failed.Write(项);
}

注意:在.NET中,对于您的用例,您没有自动类型推断的方法,也没有自动类型替换

不确定这是否是你要找的

您的方法定义应该如下所示

public static T GenerciLog<T> { get; internal set; }
public static T generilog{get;internal set;}
这就是它的名称

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);
}
试试看
{
//此处为事务处理代码
//这将在“Passed.txt”文件中写入文本。我希望像这样访问“Passed”方法
Log.generilog.Passed.Write(项);
}
捕获(例外情况除外)
{
//这将在“Failed.txt”文件中写入文本。我希望像这样访问“Failed”方法
Log.generilog.Failed.Write(项);
}

这是一个非常简单的日志类。这类事情你可以做得更多。它都是由log4net提供的,我建议您使用log4net,而不是尝试编写自己的日志。但下面是我如何实现一个简单记录器的开始。它允许您同时登录到多个不同的内容。我很感激下面的内容不能准确回答您的需求,但它表明了如何开始,您可以调整它以满足您的需求

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);

    }
}

这是一个非常简单的日志类。这类事情你可以做得更多。它都是由log4net提供的,我建议您使用log4net,而不是尝试编写自己的日志。但下面是我如何实现一个简单记录器的开始。它允许您同时登录到多个不同的内容。我很感激下面的内容不能准确回答您的需求,但它表明了如何开始,您可以调整它以满足您的需求

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);

    }
}

不,在你的情况下静力学也是不好的。。。只需使用DI和pass接口,如
接口ILogger{WriteFailed(…);WritePassed(…)}
Log
是静态的。您的意思是,有时希望使用一种类型声明
Log.generilog
,有时使用另一种类型声明。假设您有一个
GenericLoger
和一个
GenericLoger
。T在一种情况下是int,在另一种情况下是String。但是您只能用一种类型声明一次
Log.generilog
。您可以将
Log.generilog
声明为
objec