ninject / Ninject.Extensions.Conventions

Convention based binding for Ninject
Other
76 stars 28 forks source link

GetNameWithoutGenericPart Throws SubString exception #26

Closed frankbryce closed 6 years ago

frankbryce commented 8 years ago

I'm liking the package but I ran into this issue.

On line 102 of file AbstractInterfaceBindingGenerator.cs, the string.SubString() method is called without first making sure that the IndexOf() call did not return -1. The following exception is thrown if the IndexOf() returns -1, which is not very useful when something went wrong with my Ninject bootstrapping.

"System.ArgumentOutOfRangeException: Length cannot be less than zero"

The call that I'm making which ultimately leads to this exception is the following.

kernel.Bind(x => x.FromAssembliesInPath(Directory.GetCurrentDirectory()) .SelectAllClasses().BindDefaultInterfaces());

scott-xu commented 8 years ago

@frankbryce In which case the type is a generic type but index of "`" is -1?

frankbryce commented 8 years ago

@scott-xu I'm not sure, I no longer have that exact code because I worked around the issue. The next chance that I get I'll try to reproduce it and let you know.

scott-xu commented 8 years ago

ok thanks

frankbryce commented 8 years ago

In the function,

Here is sample code where type.IsGenericType is true but index of "`" is -1.

class Program
{
    static void Main()
    {
        var type = typeof (Generic<>.ClassWithinGeneric);
        Console.WriteLine(type.IsGenericType); // True
        Console.WriteLine(type.Name.IndexOf("`")); // -1
    }
}

public class Generic<T>
{
    public class ClassWithinGeneric { }
}
kind-serge commented 7 years ago

Happened to me as well. Here is an example:

public class MyGenericType<T>
{
   public MyGenericType()
   {
        var lambda = (T element) => T == default(T);
       // This lambda will be compiled to a generic class with full name
       //  like "MyGenericType`1+<>c", but short name just "<>c"
   }
}