dotnet/machinelearning · error · ArgumentException

Did not find access modifier (Parameter 'methodInfo')

Error message

Did not find access modifier (Parameter 'methodInfo')

What it means

The Accessmodifier extension on MethodInfo maps reflection's visibility flags to the internal AccessModifier enum. If none of the tested flags (IsFamilyAndAssembly/IsFamily/IsPrivate/IsFamilyOrAssembly/IsAssembly/IsPublic) is true, the method's visibility cannot be classified and this ArgumentException is thrown with param name 'methodInfo'.

Source

Thrown at src/Microsoft.ML.Core/ComponentModel/ComponentCatalog.cs:33


internal static class Extension
{
    internal static AccessModifier Accessmodifier(this MethodInfo methodInfo)
    {
        if (methodInfo.IsFamilyAndAssembly)
            return AccessModifier.PrivateProtected;
        if (methodInfo.IsPrivate)
            return AccessModifier.Private;
        if (methodInfo.IsFamily)
            return AccessModifier.Protected;
        if (methodInfo.IsFamilyOrAssembly)
            return AccessModifier.ProtectedInternal;
        if (methodInfo.IsAssembly)
            return AccessModifier.Internal;
        if (methodInfo.IsPublic)
            return AccessModifier.Public;
        throw new ArgumentException("Did not find access modifier", nameof(methodInfo));
    }

    internal static AccessModifier Accessmodifier(this ConstructorInfo constructorInfo)
    {
        if (constructorInfo.IsFamilyAndAssembly)
            return AccessModifier.PrivateProtected;
        if (constructorInfo.IsPrivate)
            return AccessModifier.Private;
        if (constructorInfo.IsFamily)
            return AccessModifier.Protected;
        if (constructorInfo.IsFamilyOrAssembly)
            return AccessModifier.ProtectedInternal;
        if (constructorInfo.IsAssembly)
            return AccessModifier.Internal;
        if (constructorInfo.IsPublic)
            return AccessModifier.Public;
        throw new ArgumentException("Did not find access modifier", nameof(constructorInfo));
    }

View on GitHub (pinned to 7b76e69cf9)

Solutions

  1. Inspect the MethodInfo's attributes (IsPublic, IsPrivate, etc.) before registering the component to confirm it has standard CLR visibility.
  2. Register components from the normal statically compiled assembly instead of dynamic/proxy assemblies.
  3. Catch ArgumentException around component registration and skip unclassifiable members.
  4. If reproducible with a specific assembly, decompile it and check the method's access flags; recompile the component.

Example fix

// before
catalog.RegisterComponent(typeof(MyComponent)); // blindly scans all methods
// after
var ok = typeof(MyComponent).GetMethods().All(m =>
    m.IsPublic || m.IsPrivate || m.IsFamily || m.IsAssembly ||
    m.IsFamilyOrAssembly || m.IsFamilyAndAssembly);
if (ok) catalog.RegisterComponent(typeof(MyComponent));
Defensive patterns

Strategy: validation

Validate before calling

var m = typeof(MyComponent).GetMethod("Train");
bool classifiable = m.IsPublic || m.IsPrivate || m.IsFamily ||
    m.IsAssembly || m.IsFamilyOrAssembly || m.IsFamilyAndAssembly;
if (classifiable) catalog.RegisterComponent(typeof(MyComponent));

Type guard

static bool HasStandardVisibility(MethodInfo m) =>
    m.IsPublic || m.IsPrivate || m.IsFamily ||
    m.IsAssembly || m.IsFamilyOrAssembly || m.IsFamilyAndAssembly;

Try / catch

try
{
    catalog.RegisterComponent(candidateType);
}
catch (ArgumentException ex) when (ex.ParamName == "methodInfo")
{
    logger.LogWarning($"Skipped {candidateType}: non-standard method visibility");
}

Prevention

When it happens

Trigger: ComponentCatalog scans candidate methods and calls Accessmodifier(methodInfo) on a MethodInfo whose visibility flags match none of the expected combinations — essentially unreachable with a healthy reflection surface, but triggered by exotic/proxy/dynamic-method types or a method added by a compiler emitting non-standard visibility.

Common situations: Registering components in a ComponentCatalog from dynamically generated assemblies (Reflection.Emit, proxies) whose methods have unusual visibility; corrupted/duplicated assemblies loaded into the AppDomain.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11). Data as JSON: /api/errors/6b5617bd30b249b6. Report an issue: GitHub.