Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a desktop application that collects some data and then does some postprocessing to the data. The post processor varies depending on user selection. Each post processer is a class which inherits the IPostProcessor interface which defines the signature of the "CrunchNumbers" method. So far I have 2 post processor classes but I know that more will be added in the future. Currently I have an "if"-"else if" statement that selects the postprocessor based on the class name passed in as a string. Inside the "else if" it calls the appropriate factory which returns the class instance corresponding to the string specified (see code below). The problem is that I want to be able to add new post processor classes without altering the code that instantiates them. So my question is, how can I write code that will intantitate a class at runtime (that inherits a know interface) for a class that won't be written until some time in the future? In other words, I don't want to have to keep coming back and adding more "else if" lines to my code below.


IPostProcessor biasScaleFactorPP;
IPostProcessor AllanVariancePP;
private void LaunchPostProcessor(double[,] twoDArray, string postProcessorName)
{
    if (postProcessorName == BiasScaleFactorString && !closing)
    {
        if (biasScaleFactorPP == null)
        {
            biasScaleFactorPP = PostProcFactory.CreatePostProcessor<BiasScaleFactor>();
            biasScaleFactorPP.Initialize(serialNumbers, dataId, populatedLocations);
        }
        bsfDelegate = new DelegateToMethod(biasScaleFactorPP.CrunchNumbers);
        bsfDelegate(twoDArray, PostProcParams);
    }
    else if (postProcessorName == AllanVarianceString && !closing)
    {
        if (AllanVariancePP == null)
        {
            AllanVariancePP = PostProcFactory.CreatePostProcessor<AllanVariance>();
            AllanVariancePP.Initialize(serialNumbers, dataId, populatedLocations);
        }
        avDelegate = new DelegateToMethod(AllanVariancePP.CrunchNumbers);
        avDelegate(twoDArray, PostProcParams);
    }
    else if (!closing)
    {
        throw new Exception(string.Concat(postProcessorName, " is not recognized as a valid post processor"));
    }
}
Posted

1 solution

This is extremely bad method.

Instead, define some interface, reference its assembly to make it visible. Use Reflection and find out your classes which support this interface. Instantiate using System.Reflection.ConstructorInfo obtained from Type.

Getting the Type based type name is bad because it is based on "hard-coded" string. When somebody decides to rename a class, the application compiles and runs but crashes because the type is not found, all of a sudden. It somebody renames an interface in just one place or one assembly, compiler shows an error which can be fixed immediately.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900