简体   繁体   English

如何在变量中存储将用于代替通用类的类的名称?

[英]How to store in a variable the name of the class that will be used in place of a generic class?

Trying to re-factor the code, I've created a base class where I've put all the common functionality. 为了重构代码,我创建了一个基类,其中放置了所有常用功能。 I'm stuck with this one delegate that I want to convert to a method. 我被这个要转换为方法的委托所困扰。

 Func<LogImportTestArgs, IList<TranslationTask>> 
 getParserWithMocks = (args) =>
 {
         return TestParserWithMocks<MyGenericClass>(
                parserConstName: args.ParserConstName,
                logFileName: args.LogFileName,
                srcLocale: args.SrcLocale,
                tgtLocale: args.TgtLocale,
                tlType: args.TranslationType,
                ttype: args.TTtype,
                logMappingID: args.LogMappingID
      );
   };

Everything is straightforward except for the generic class that I don't know how to store in a variable. 除了我不知道如何存储在变量中的泛型类之外,其他所有内容都很简单。 I've tried to store the name of the class as a string, but I'm getting the error when I replace MyGenericClass with a string. 我试图将类的名称存储为字符串,但是用字符串替换MyGenericClass时出现错误。

protected IList<TranslationTask> getParserWithMocks(LogImportTestArgs args)
{
         return TestParserWithMocks<MyGenericClass>(
                parserConstName: args.ParserConstName,
                logFileName: args.LogFileName,
                srcLocale: args.SrcLocale,
                tgtLocale: args.TgtLocale,
                tlType: args.TranslationType,
                ttype: args.TTtype,
                logMappingID: args.LogMappingID
      );
   };

Is there a way to stoire the value of the generic class in a variable? 有没有办法在变量中存储泛型类的值? Otherwise I'm gonna have to copy/paste the same method on every tests. 否则,我将不得不在每个测试中复制/粘贴相同的方法。 I'd like to have a central place where I can modify this method. 我想在中央可以修改此方法。

Thank you. 谢谢。

If you want to parameterize MyGenericClass there, you will need to make the function generic: 如果要在MyGenericClass参数化MyGenericClass ,则需要使函数通用:

protected IList<TranslationTask> GetParserWithMocks<T> (LogImportTestArgs args)
{
    return TestParserWithMocks<T>(
        parserConstName: args.ParserConstName,
        logFileName: args.LogFileName,
        srcLocale: args.SrcLocale,
        tgtLocale: args.TgtLocale,
        tlType: args.TranslationType,
        ttype: args.TTtype,
        logMappingID: args.LogMappingID
    );
};

Then, when you call the function, you will need to pass in the generic type: 然后,当您调用函数时,您将需要传入通用类型:

IList<TranslationTask> tasks = GetParserWithMocks<MyGenericClass>(args);

Not sure I fully understand, but it seems like you want the method to be generic: 不确定我是否完全理解,但似乎您希望该方法是通用的:

protected IList<TranslationTask> getParserWithMocks<T>(LogImportTestArgs args)
{
     return TestParserWithMocks<T>(
            parserConstName: args.ParserConstName,
            logFileName: args.LogFileName,
            srcLocale: args.SrcLocale,
            tgtLocale: args.TgtLocale,
            tlType: args.TranslationType,
            ttype: args.TTtype,
            logMappingID: args.LogMappingID
      );
   };

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

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