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
.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With