Search code examples
c#activator

How to pass type dynamically to Activator.CreateInstance(object)?


I am trying to implement generic solution using Activator.CreateInstace()

Below I have interface,

public interface INewReleaseValidationRule<T> where T : INewReleaseValidationEntity
{
    void Run(CtxNewRelease ctx, IEnumerable<T> entities);
    string GetMessage(string messageName, string fallbackMessage);
}

public interface INewReleaseValidationEntity
{}

My class CustomerAssociation is:

public class CustomerAssociation : INewReleaseValidationEntity
{
 public void Run(Context.Ctx context, IList<INewReleaseValidationEntity> entitiesObject)
    {}
}

Then I have view model which is also implementing INewReleaseValidationEntity.

 public class ForecastViewModel : INewReleaseValidationEntity
{

}

Then,

public partial class ValidationRule
{
public void Run(Ctx context, List<ForecastViewModel > entity)
    {
        var validation = this.GetExecutionType();
        var execution = (INewReleaseValidationRule<entity>)Activator.CreateInstance(validation);
        execution.Run(context, entity.ToArray());
    }
}

In above highlighted statement I am getting error.

If I use,

var execution = (CustomerAssociation)Activator.CreateInstance(validation);

then it works perfectly fine. But I want to provide the explicit type (in this case CustomerAssociation) dynamically.

All my explicit type (that is CustomerAssociation) and others will be inherited from INewReleaseValidationRule<T>.

If I write

var execution = (INewReleaseValidationRule<ForecastViewModel>)Activator.CreateInstance(validation);

then getting runtime error,

Unable to cast object of type 'CustomerAssociation' to type 'INewReleaseValidationRule`1[ForecastEstimateViewModel]'.


Solution

  • It's a bit unclear from the code what the actual intent is, but you can try adjusting your validator's run method to take a generic type like this:

    public partial class ValidationRule
    {
        public void Run<T>(Ctx context, List<ForecastViewModel> entity)
            where T : class, INewReleaseValidationEntity
        {
            var execution = (T)Activator.CreateInstance<T>();
            execution.Run(context, entity.ToArray());
        }
    }
    

    And call it like this:

    new ValidationRule().Run<CustomerAssociation(context, entities);