Search code examples
c#reflectionildasm

Unable to find Interface in C# using reflection with Name.EndsWith


The name of interface is IService, but when I am trying to find the Interface in reflection in C#, it is not able to find because due to some reason the Interface name changes to Iservice'1

Please have a look to attached screenshot for Ildasm :

enter image description here

Actual Interface is like

 public interface IService<TOutput> 
        where TOutput : class, new()
    {
        Task<List<TOutput>> GetAllAsync(dynamic inputParameter);
    }

Code to find Interface: builder.RegisterAssemblyTypes(Assembly.Load("Services")) .Where(t => t.Name.EndsWith("Service"))

Here it fails to find Service, as IService is having some different name then defined one.

Any Idea why the name looks this way and how to resolve it ?


Solution

  • IService is a generic. In other words, it's IService<T>. When compiling to MSIL, C# generics have their name modified as you noticed.

    You could make the service non-generic, or use .Contains instead of .EndsWith.