mardi 18 février 2020

Create instance using Activator

I got some troubles with Activator.CreateInstance. It throws "Constructor on type not found" exception.

Here is the method where error occurs:

private static void InitializeInternalProperty(Calculation calculation, Type type, IDataProvider dataProvider, params string[] precedants)
{
    PropertiesCollection precedantsCollection = new PropertiesCollection(); //Properties collection implements IKeyedCollection
    foreach (string precedant in precedants)
    {
        precedantsCollection.Add(calculation.properties[precedant]);
    }
    calculation.properties.Add((Property)Activator.CreateInstance(type, dataProvider, precedantsCollection));
}

Here is the constructor, i am trying to use:

internal CountryRiskPremium(IDataProvider dataProvider, PropertiesCollection precedants)
{
    Name = Names.CountryRiskPremium;
    InitLinks(precedants);
    _dataProvider = dataProvider;
}

I also tried:

object[] arguments = new object[2];
arguments[0] = dataProvider;
arguments[1] = precedantsCollection;
calculation.properties.Add((Property)Activator.CreateInstance(type, arguments));




Aucun commentaire:

Enregistrer un commentaire