Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activator.CreateInstance - MissingMethodException: Constructor on type 'xxx' not found

I have the following code
Driver Code

var provider = DataManager.BuildDatabase<FileDatabase>(config,false,dbPath).Result;

DataManager.cs

 public static async Task<IStorageProvider> BuildDatabase<TStorageProvider>(DbConfig config,
    bool isBuildUniqueAddress = false,params object[] buildParam) where TStorageProvider : IStorageProvider
{
    var t = typeof(TStorageProvider);
    //merging params
    var buildConfig= new DbData(config);
    buildParam= buildParam.ToList().Prepend(buildConfig).ToArray();
    Console.WriteLine($"Building Database of type:{t.FullName}");
   
    IStorageProvider? storageProvider = (IStorageProvider?)Activator.CreateInstance(t,buildParam);
}

and Here is my Constructor for type FileDatabase
FileDatabase.cs

public FileDatabase(DbConfig config,string dbPath)
{
    _dbData = new DbData(config);
    DbPath=dbPath;

    if (!File.Exists(dbPath))
    {
        Flush().RunSynchronously();
    }
}

When I try to execute code above, it gives me:
MissingMethodException: Constructor on type 'assertUpdaterRefactor.StorageProvider.FileDatabase' not found.\

I tried to debug content of variable buildParam enter image description here.
The debugger shows the buildParam is an object array and exactly matches the constructor. I just can't figure out the reason causing this problem. Please help
UPDATE:
When I tried to create a new object array. The issue fixed magically
IStorageProvider? storageProvider = (IStorageProvider?)Activator.CreateInstance(t, new object[]{config,"someStringValue"});
Here is the original code copied from above just for reference
IStorageProvider? storageProvider = (IStorageProvider?)Activator.CreateInstance(t,buildParam);

I want to know the causes and why the original code does not work.

like image 627
Freddyy Avatar asked Dec 29 '25 04:12

Freddyy


1 Answers

The first argument of the FileDatabase is DbConfig, in your BuildDatabase you use a type DbData.

So instead of passing an instance of var buildConfig= new DbData(config); pass the config directly.

Would be something like:

public static async Task<IStorageProvider> BuildDatabase<TStorageProvider>(DbConfig config,
            bool isBuildUniqueAddress = false,params object[] buildParam) where TStorageProvider : IStorageProvider
{

    var t = typeof(TStorageProvider);
    //merging params
    buildParam= buildParam.ToList().Prepend(config).ToArray();
    Console.WriteLine($"Building Database of type:{t.FullName}");
   
    IStorageProvider? storageProvider = (IStorageProvider?)Activator.CreateInstance(t,buildParam);
 
}
like image 125
Jeroen van Langen Avatar answered Dec 31 '25 18:12

Jeroen van Langen