Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetNameWithoutGenericPart Throws SubString exception #26

Closed
frankbryce opened this issue Jan 20, 2016 · 5 comments
Closed

GetNameWithoutGenericPart Throws SubString exception #26

frankbryce opened this issue Jan 20, 2016 · 5 comments
Assignees

Comments

@frankbryce
Copy link

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
Copy link
Member

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

@frankbryce
Copy link
Author

@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
Copy link
Member

ok thanks

@frankbryce
Copy link
Author

In the function,

  • type.Name is Builder
  • type.FullName is "System.Collections.Immutable.ImmutableArray`1+Builder"

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
Copy link

kind-serge commented Jun 22, 2017

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"
   }
}

@scott-xu scott-xu self-assigned this Oct 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants