mardi 14 juin 2022

ArgumentException: Cannot bind to the target method because its signature is not compatible with that of the delegate type

private const string ReferencedAssemblyName = "System.Private.CoreLib";

private delegate IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath);
private delegate void SetDllImportResolverDelegate(Assembly assembly, DllImportResolver resolver);

private static readonly SetDllImportResolverDelegate _setDllImportResolverCallback;

static Program()
{
    Assembly coreLibAssembly = AppDomain.CurrentDomain
        .GetAssemblies()
        .FirstOrDefault((assembly) => assembly.GetName().Name == ReferencedAssemblyName);

    if (coreLibAssembly != null)
    {
        MethodInfo[] methods = coreLibAssembly
            .GetTypes()
            .FirstOrDefault((type) => type.Name == "NativeLibrary")
            .GetMethods(BindingFlags.Public | BindingFlags.Static);

        foreach (MethodInfo method in methods)
        {
            if (method.Name == "SetDllImportResolver")
                _setDllImportResolverCallback = (SetDllImportResolverDelegate)method.CreateDelegate(typeof(SetDllImportResolverDelegate));
        }
    }
}

static void Main()
{
    _setDllImportResolverCallback(typeof(NativeLibrary).Assembly, (libraryName, assembly, searchPath) =>
    {
        return NativeLibrary.Load("Kernel32");
    });
}

Im getting this exception when im trying to create delegate for "SetDllImportResolver" method. My delegate has same structure like in System.Private.CoreLib. How can i make it works?





Aucun commentaire:

Enregistrer un commentaire