C#
.NET
FileNotFoundException
MethodInfo
GetCustomAttribute

FileNotFoundException on MethodInfo.GetCustomAttribute

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If MethodInfo.GetCustomAttribute throws FileNotFoundException, the problem is usually not the method lookup itself. The exception typically means reflection is trying to load an attribute type or one of its dependent assemblies, and that assembly cannot be found at runtime.

Why Reading an Attribute Can Load Assemblies

GetCustomAttribute does more than inspect metadata. It usually instantiates the attribute object, which means the runtime may need to load:

  • the assembly that defines the attribute type
  • assemblies referenced by the attribute constructor arguments
  • assemblies referenced by attribute property types

So code that looks harmless can fail during attribute materialization:

csharp
1using System;
2using System.Reflection;
3
4[AttributeUsage(AttributeTargets.Method)]
5public class DemoAttribute : Attribute
6{
7    public string Name { get; }
8
9    public DemoAttribute(string name)
10    {
11        Name = name;
12    }
13}
14
15public class Sample
16{
17    [Demo("hello")]
18    public void Run() { }
19}
20
21MethodInfo method = typeof(Sample).GetMethod(nameof(Sample.Run))!;
22var attribute = method.GetCustomAttribute<DemoAttribute>();
23Console.WriteLine(attribute?.Name);

If DemoAttribute or one of its dependencies lives in an assembly that is missing, the reflection call can fail with FileNotFoundException.

Common Real Cause

The most common real-world scenario is:

  • your main assembly loads correctly
  • the method exists
  • the custom attribute is declared in another assembly
  • that assembly, or a dependency of that assembly, is missing or version-mismatched

This often happens in plugin systems, test runners, or trimmed deployments where attribute assemblies are not copied with the rest of the application.

Another common case is version drift. The assembly file exists, but the runtime is looking for a different version than the one present.

Use CustomAttributeData When You Only Need Metadata

If you only need to inspect attribute metadata and do not need to instantiate the attribute, CustomAttributeData can be safer:

csharp
1using System;
2using System.Linq;
3using System.Reflection;
4
5MethodInfo method = typeof(Sample).GetMethod(nameof(Sample.Run))!;
6
7foreach (var data in method.CustomAttributes)
8{
9    Console.WriteLine(data.AttributeType.FullName);
10    foreach (var arg in data.ConstructorArguments)
11    {
12        Console.WriteLine(arg.Value);
13    }
14}

This still depends on metadata being readable, but it avoids some of the runtime activation cost of GetCustomAttribute.

That makes it a good debugging tool when you suspect assembly-loading trouble.

How to Diagnose the Missing Assembly

Start by identifying which assembly is missing. The exception message often names it directly. Then verify:

  • the assembly file is present in the application output
  • the requested version matches the deployed version
  • the attribute assembly’s own dependencies are also present

A small diagnostic snippet can help:

csharp
1using System;
2using System.Reflection;
3
4try
5{
6    MethodInfo method = typeof(Sample).GetMethod(nameof(Sample.Run))!;
7    _ = method.GetCustomAttributes(inherit: false);
8}
9catch (Exception ex)
10{
11    Console.WriteLine(ex);
12    Console.WriteLine(ex.InnerException);
13}

In larger systems, assembly load logging, probing-path inspection, or dependency analysis tools are often needed to find the real missing file.

Fix the Deployment, Not the Reflection Call

The real fix is usually one of these:

  • copy the missing assembly to the output
  • correct the package or project reference
  • align assembly versions
  • ensure plugin load contexts can resolve the dependency

Changing GetCustomAttribute to another reflection API may hide the symptom temporarily, but it does not solve a broken deployment or load context.

Common Pitfalls

The biggest mistake is reading the exception as “the method was not found.” MethodInfo already exists at that point. The missing file is typically related to the attribute path, not the method lookup.

Another issue is assuming the attribute assembly is enough by itself. An attribute can depend on other assemblies, and any missing dependency can trigger the same failure.

Developers also sometimes use GetCustomAttribute when they only need metadata for tooling, scanning, or diagnostics. In those cases, CustomAttributeData is often the better choice.

Finally, plugin and test environments deserve special care. Reflection-heavy code often works in the main app and fails only when the assembly load context changes.

Summary

  • 'GetCustomAttribute can throw FileNotFoundException because it may instantiate the attribute and load its dependencies.'
  • The issue is usually a missing or mismatched assembly, not a missing method.
  • 'CustomAttributeData is useful when you only need metadata.'
  • Diagnose which assembly the runtime is trying to load and why it is missing.
  • Fix the reference, deployment, or load context rather than treating it as a reflection API bug.

Course illustration
Course illustration

All Rights Reserved.