简体   繁体   中英

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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